何时调用encodeWithCoder?
这将保存一个数组(我认为)。
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:myArray forKey:@"myArray"];
}
当我想保存数组时是否必须直接调用此方法还是必须执行其他操作?
This will save an array (I think).
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:myArray forKey:@"myArray"];
}
Do I have to directly call this method when I want to save an array or do I have to do something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不直接调用此方法。如果需要序列化该对象,则由
NSCoder
子类调用。如果要对对象图进行编码,请使用NSKeyedArchiver
的类方法archivedDataWithRootObject:
或archiveRootObject:toFile:
。这反过来将调用对象的encodeWithCoder:
方法。另请注意,数组中的每个对象都必须实现 NSCoding 协议。You don’t call this method directly. It’s called by a
NSCoder
subclass if it needs to serialize that object. If you want to encode an object graph use the class methodsarchivedDataWithRootObject:
orarchiveRootObject:toFile:
ofNSKeyedArchiver
. This in turn will call theencodeWithCoder:
method of your objects. Also note that every object in your array has to implement theNSCoding
protocol.