objective-c - NSCoder - 使用多级嵌套数组对数组进行编码

标签 objective-c ios xcode saving-data nscoder

我有一个 mainObjectArray (NSMutableArray),其中填充了自定义类的实例。每个实例本身就是一个数组,每个数组中的对象是 NSDates、NSStrings、BOOL 以及更多包含类似对象的数组。

我无法确定的是,在

- (void)encodeWithCoder:(NSCoder *)encoder 

方法,就这么说吧:

[encoder encodeWithObject:mainObjectArray];

或者必须分别对每个实例中的每个对象进行编码?这会有点痛苦......

非常感谢您的帮助。

最佳答案

只需在自定义类中实现编码和解码方法即可。这样就可以了。一些样本,

- (void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeObject:[NSNumber numberWithInt:pageNumber] forKey:@"pageNumber"];
    [encoder encodeObject:path forKey:@"path"];
    [encoder encodeObject:array forKey:@"array"];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if(self = [super init]) 
    {
        self.pageNumber = [[aDecoder decodeObjectForKey:@"pageNumber"] intValue];
        self.path = [aDecoder decodeObjectForKey:@"path"];
        self.array = [aDecoder decodeObjectForKey:@"array"];
    }
}

您可以看到正在编码和解码的总共三种数据类型 - int、string、array。

希望这对您有所帮助。

关于objective-c - NSCoder - 使用多级嵌套数组对数组进行编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9343570/

相关文章:

objective-c - 比较两个数组忽略顺序

ios - 在代码中重新加载标签后,uiview 回到 Storyboard中的第一个位置

ios - 在 Cocoa 中读取 iOS 上的图片

ios - 带字节的字符串 : length: encoding: is producing the wrong length of string

xcode - 设置具有正确详细信息的自定义单元格样式,但它不显示详细信息文本标签?

ios - 应用程序扩展 iOS9 : How can I send a string/URL from the share view to my the app?

ios - 自定义 UITableViewCell - 我设置了背景图像,但如何设置边框颜色?

objective-c - NSWindow 阴影轮廓

ios - 如何使用谷歌方向 API 在苹果 map 上快速绘制路线?

xcode - 通过短信发送当前位置