Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do I use the initWithCoder: method?

When am I supposed to use the initWithCoder: method?

like image 762
Christian Gossain Avatar asked Nov 17 '10 20:11

Christian Gossain


2 Answers

Yes, if you are using a custom class in IB, then those objects are instantiated with the initWithCode: method. So, in your class you would override:

-(id) initWithCoder:(NSCoder*)aDecoder {
    if (! (self = [super initWithCoder:aDecoder]))
        return nil;

    // object has been created from IB... do initialization stuff here

    return self;
}
like image 80
Brandon Williams Avatar answered Oct 25 '22 16:10

Brandon Williams


You are supposed to use the initWithCoder: method when you are working with objects that have been archived. For example when you specifically use NSKeyedUnarchiver to create these archived objects or when you need to add custom initialization code to objects that are coming from a xib file.

like image 42
Stefan Arentz Avatar answered Oct 25 '22 16:10

Stefan Arentz