Wil Shipley recommends this (since 2009):
- (id)init; { if (!(self = [super init])) return nil; // other stuff return self; }
But why appropriate super init return to itself?
Matt Gallagher article is trying to explain this ...
- Quote:
If you remember at the beginning, I said that initWithString: part of the typical [[MyClass alloc] initWithString: @ "SomeString"] call is converted to an objc_msgSend call:
MyClass *myObject2 = objc_msgSend(myObject1, initSelector, @"someString");
So, by the time we get inside the method, it already matters; its value is myObject1 (ie, the selected object, as returned from the [MyClass alloc] call. This is important because without it, a super call would be impossible - the value is used by the compiler to send the Call:
[super init]
becomes:
objc_msgSendSuper(self, @selector(init));
Yes, self already matters when your initializer starts. In fact, it is almost guaranteed to be the correct, final value.
- Unquote
In fact, I think that many people are confused about the fact that each method of initialization of the "I" indicates exactly, up the chain of the superclass.
The answer to this puzzle is implied in Apple Objective-C Programming Document Language in the section "Assigned Initializers:
Note that the B version of init sends a message to itself to invoke the initWithName: method. Therefore, when the receiver is an instance of class B, it calls version B of initWithName :, and when the receiver is an instance of class C, it calls version C.
Or, in other words, the variable "I" indicates our instance, which is initialized. Again, to emphasize again, all of these initialization methods through the superclass chain are inherited by our instance, and as such, the self variable in them points to our instance (unless explicitly modified).
I'm right? Sure!