Jsdodgers
Jsdodgers

Reputation: 5312

Custom Object Initializers

I've been creating custom initializers for my objects just because it feels like better practice than setting their variables in other ways. In these initializers I usually set the variables of the object then return a call to the main init.

So, for example, in a UIViewController subclass my code would look something like this:

-(id)initWithValue:(int)val {
    self.value = val;
    return [self initWithNibName:nil bundle:nil];
}

where value is an integer that belongs to that ViewController subclass, and there are usually more values than that.

However, recently I started setting self first because I thought that the self = [self init...] would replace the current instance of the class and thus I would lose that instance of self. So, I have started doing:

-(id)initWithValue:(int)val {
    self = [self initWithNibName:nil bundle:nil];
    self.value = val;
    return self;
}

I then recently checked the original version and realized that everything does work properly and the change was unneccessary.

So, my question is this:

  1. What does the [super initWithNibName:bundle:] do which is causing it to create an object but not replace the original object?
  2. Is one of the two versions better than the other to use or are they both equivalent? If one is better, which should be used?

Thanks in advanced!

Upvotes: 1

Views: 263

Answers (4)

Bhanu Prakash
Bhanu Prakash

Reputation: 1483

The first code which you have written won't store the value, because before creation of the object you are trying to store data. But, the second code, needed a small modification for better practice i.e, like this...

-(id)initWithValue:(int)val {
    self = [self initWithNibName:nil bundle:nil];

    if(self)
    _value = val;

    return self;
}

Hope this is useful to you...:-)

Upvotes: 0

Nick C
Nick C

Reputation: 645

You should do it the following way:

- (id)initWithValue:(int)val {
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        _value = val;
    }
    return self;
}

In iOS, a common pattern is to return nil if the parameters sent to init methods are invalid. The value will be one of 2 things: the current pointer to self or nil. If the call to super returns nil then the object was not setup properly so you should also return nil. Doing self = [super initWithNibName:nil bundle:nil]; just makes it easier to respect the possible nil value returned by super

Upvotes: 2

Siverchen
Siverchen

Reputation: 379

the [super initWithNibName:bundle:] actually invoke the method of the superclass. if you use [self initWithNibName:bundle:] actually it will call the rewriting of initWithNibName:bundle: of course you must have rewrote it,otherwise it will also call the superclass method. so if you want to do some initialize in the rewrite of initWithNibName then you can use [self initWithNibName:bundle:] but if you don't need do the extra initialize there is no difference between the to method;

Upvotes: 0

Vinodh
Vinodh

Reputation: 5268

Please use the following code to override init method

 -(id)initWithValue:(int)val 
 {
   self = [super init];
   if(self)
     {
    self.value = val;
    }

   return self;
 }

Upvotes: 1

Related Questions