initWithCoder 什么时候被调用?
这将加载一个数组,
- (id)initWithCoder:(NSCoder*) coder
{
self = [super initWithCoder: coder];
if (self) {
myArray=[coder decodeObjectForKey:@"myArray"];
}
return self;
}
调用此函数以便加载数组的代码是什么?
This will load an array
- (id)initWithCoder:(NSCoder*) coder
{
self = [super initWithCoder: coder];
if (self) {
myArray=[coder decodeObjectForKey:@"myArray"];
}
return self;
}
What is the code that will call this function so that the array can be loaded?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
initWithCoder:
方法用于使用 NSCoding 协议,例如通过 [NSKeyedUnarchiver unarchiveObjectWithFile:]。有关详细信息,请参阅 档案和序列化编程指南,尤其是编码和解码对象部分。The
initWithCoder:
methods are used for deserializing using NSCoding protocol, e.g. via [NSKeyedUnarchiver unarchiveObjectWithFile:]. For details see the Archives and Serializations Programming Guide, especially the Encoding and Decoding Objects section.正如 DarkDust 所说,它在使用 NSUnarchiver 或 NSKeyedUnarchiver 时被调用。然而,这并不一定是其自身的情况。实际上可以实现自定义 NSCoder 并根据 NSDecoder .. 例如编码/解码 yaml 等...
最常见的用例是加载 nib 文件时,因为这些内容已存档。
As DarkDust said, it's called when a NSUnarchiver or a NSKeyedUnarchiver is used. However, this is not necessarily the own case. One could actually implement a custom NSCoder and according NSDecoder .. e.g. to encode/decode yaml etc...
The most common use case is when loading nib files, as those contents are archived.