Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does initWithCoder get called?

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?

like image 893
node ninja Avatar asked Sep 18 '10 13:09

node ninja


2 Answers

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.

like image 156
DarkDust Avatar answered Oct 15 '22 05:10

DarkDust


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.

like image 44
Max Seelemann Avatar answered Oct 15 '22 05:10

Max Seelemann