0

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.

Artur Kucaj
  • 1,071
  • 1
  • 11
  • 16
  • yes, there is a clear description provided by JeremyP, anyway seems like all important init stuff should be done only in designated initializer – Artur Kucaj Jan 24 '14 at 12:04

3 Answers3

0

The reason why you need to check is that you don't know what happens in your superclass. When you call

self = [super init];

your super class can correctly initialise your object or if something occurs can return "nil". If it returns nil, of course you don't want to proceed with any further initialisation on a nil pointer. That's why you check.

self = [super init];
if (self){
   //my further initialisation only if my super class didn't return nil
}
return self; //which is nil if my superclass failed the initialisation
tanz
  • 2,557
  • 20
  • 31
0

alloc method only allocate a memory for you class instance. After alloc you have a pointer to memory (for example, 0x8f25050), allocated for your instance. But it's not yet initialized. It have no value. So, you have to call init method, and check is your instance not nil.

Valentin Shamardin
  • 3,569
  • 4
  • 34
  • 51
0

Initialization is lead to initialize created object. Refer Object Initialization to know about necessary of initialization. Before read this document, just know about Object Allocation alone(not with Initialization)

In this line self = [super init];, your created object's property of superclass will initialized.

Answer to first question: for what reason should I insert self checking in the designated initializer?

Some people may create object with new comment. Then call initilization separately, in between, they may release objects due to reference count that's why, check with self.

Mani
  • 17,549
  • 13
  • 79
  • 100