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:
- Convert it into an
NSArray
ofNSNumber
objects. - Shoehorn it into an
NSValue
orNSData
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.
精彩评论