Lets consider simple class X:
@implementation X
/* primary convenience initializer */
- (id)init {
NSLog(@"init: self=%@", self);
return [self initWithValue:0];
}
/* secondary convenience initializer */
- (id)initWithValue:(int)val {
NSLog(@"initWithValue: self=%@", self);
return [self initWithValue:val andText:@""];
}
/* designated initializer */
- (id)initWithValue:(int)val andText:(NSString*)text {
self = [super init];
if (self) {
// some initial stuff
}
NSLog(@"initWithValue: andText: self=%@", self);
return self;
}
@end
and then instantiate a class X:
X *x = [[X alloc] init];
Log result is following:
init: self=<X: 0x8f25050>
initWithValue: self=<X: 0x8f25050>
initWithValue: andText: self=<X: 0x8f25050>
No surprise, of course. Instance address is same in all initializers. But my question is: for what reason should I insert self checking in the designated initializer?
if (self) {
// some initial stuff
}
Since the self already exists at this point. Moreover, just a line before is a call a super initializer:
self = [super init];
What does it mean? Is it possible that address of self can change? Any hints?
IMHO, using self address for delegates or for any other purposes, could lead to strange issues if a self could change in the initializers.