Peter Lapisu
Peter Lapisu

Reputation: 21005

Calling init in init, before self = [super init]

It is a common init pattern to do self = [super init]; which should assign the self pointer

But can i forward the init like this?

- (id)initWithObject:(id)object {
    return [self initWithObject:object scrollTo:nil];
}

The code works, but not sure if it is Kosher... and also how can it work without self = [super init]

moving further, is this ok?

- (id)initWithObject:(id)object {
    self = [self initWithObject:object scrollTo:nil]; // NOT super
    if (self) {
       //...
    }
    return self;
}

Upvotes: 2

Views: 85

Answers (3)

Alexander Kalashnikov
Alexander Kalashnikov

Reputation: 11

It is absolutely legitimate only if in return operator you call designated initializer or initializer which calls the one. Make sure that one way or other the designated initializer is called.

Upvotes: 1

Pablosproject
Pablosproject

Reputation: 1424

Yes, sure you can! Note that initWithObject:scrollTo has to return a valid self object.

Upvotes: 0

Glorfindel
Glorfindel

Reputation: 22651

Yes, this is fine. I have done this myself a couple of times without problems, and I found a code example in the Apple documentation (scroll down to "Multiple Initializers and the Designated Initializer").

Upvotes: 1

Related Questions