When does initWithCoder get called?

∥☆過路亽.° 提交于 2019-12-23 09:29:54

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/3741946/when-does-initwithcoder-get-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!