iphone / Objective C / 如何编码 double[] 以进行存档
我使用 double[] 而不是 NSArray。有人知道如何对其进行编码以进行存档吗
I am using double[] instead of NSArray. Would anyone know how to encode it for archiving
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想到了两个选择:
NSNumber
对象的NSArray
。NSValue
或NSData
对象。Two options come to mind:
NSArray
ofNSNumber
objects.NSValue
orNSData
object.如果您不在平台之间传输编码数据(可能会遇到字节顺序和数据大小问题),则可以使用
直接存储字节,然后
对其进行解码。正如 NSCoder 头文件指出的那样,decodeBytesForKey:returnedLength: 方法返回不可变字节,因此您需要将返回的数组复制到 malloced double 数组中。由于返回的数组是 const,我假设解码器拥有该数组,并将在解码器释放时释放它。
这不像将数组放入 NSData 对象并存档那么方便,但它确实避免了创建临时对象的开销。
If you're not transferring the encoded data between platforms, where you might run into problems with endianness and data size, you can use
to store the bytes directly, and then
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.