Possible Duplicate:
In Objective-C why should I check if self = [super init] is not nil?
I'm new at Ob-C, and am having a hard time understanding why the value returned is non-nil as tested by the "if statement".
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
This method invokes the parent initializer first. Executing the parent's initializer ensures that any inherited instance variables are properly initialized. You must assign the result of executing the parent's init method back to self because an initializer has the right to change the location of the object in memory (meaning its reference will change). If the parent's initialization succeeds, the value returned will be non-nil, as tested by the if statement. As the comment indicates, inside the block that follows is where you can put your own custom initialization code for your object. This will often involve allocating and initializing instance variables that are in your class.
Pasted code and text from From Stephen Kochan "Programming in Objective-C, Fourth Edition"