When does initWithCoder get called?
Asked Answered
T

2

6

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?

Tristatristam answered 18/9, 2010 at 13:55 Comment(1)
Not that you have to retain the array to take ownership.Apian
B
4

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.

Bifilar answered 18/9, 2010 at 14:3 Comment(0)
W
1

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.

Whipstock answered 18/9, 2010 at 14:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.