When does initWithCoder get called?

node ninja picture node ninja · Sep 18, 2010 · Viewed 7.2k times · Source

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?

Answer

DarkDust picture DarkDust · Sep 18, 2010

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.