TheLearner
TheLearner

Reputation: 19507

What does the assignment of super init to self do?

Given this piece of code:

-(id) init
{
    self = [super init];
    if (self != nil)
    {
       ...
    }
    return self;
}

I understand that we are checking that the super init did not fail but otherwise it makes no sense to me and I would appreciate an explanation for a noob.

Why would I want to assign the result of super init to self. If I delete this method from my class, the super init method is going to be called anyway and there is going to be no assignment.

Upvotes: 2

Views: 713

Answers (1)

Gareth McCaughan
Gareth McCaughan

Reputation: 19971

In principle (and, surprisingly, even in practice) [super init] can return a different object! See http://mikeash.com/pyblog/the-how-and-why-of-cocoa-initializers.html and http://cocoawithlove.com/2009/04/what-does-it-mean-when-you-assign-super.html for more information.

Upvotes: 5

Related Questions