开发者

iphone / objective C / how to encode a double[] for archiving

I am using double[] instead of NSArray. Would anyone 开发者_StackOverflow社区know how to encode it for archiving


Two options come to mind:

  1. Convert it into an NSArray of NSNumber objects.
  2. Shoehorn it into an NSValue or NSData object.


If you're not transferring the encoded data between platforms, where you might run into problems with endianness and data size, you can use

- (void)encodeBytes:(const uint8_t*)bytesp length:(NSUInteger)lenv forKey:(NSString*)key;

to store the bytes directly, and then

- (const uint8_t*)decodeBytesForKey:(NSString*)key returnedLength:(NSUInteger*)lengthp;

to decode them. As the NSCoder header file points out, the decodeBytesForKey:returnedLength: method returns immutable bytes, so you'll want to copy the returned array into your malloced double array. Since the returned array is const, I'm assuming the decoder owns that array and will free it when the decoder is dealloced.

This isn't as convenient as just putting the array into an NSData object and archiving that, but it does avoid the overhead of creating a temporary object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜