什么时候encodeWithCoder被调用? [英] When does encodeWithCoder get called?
问题描述
这将保存数组(我认为)。
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?
推荐答案
你不直接调用这个方法。如果需要序列化该对象,它由 NSCoder
子类调用。如果要对对象图进行编码,请使用 archivedDataWithRootObject:
或 archiveRootObject:toFile:
c> NSKeyedArchiver 。这将调用对象的 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 methods archivedDataWithRootObject:
or archiveRootObject:toFile:
of NSKeyedArchiver
. This in turn will call the encodeWithCoder:
method of your objects. Also note that every object in your array has to implement the NSCoding
protocol.
这篇关于什么时候encodeWithCoder被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!