Frage

I started to look into the code done by our senior, I found the init method always have code -(id)init method. They used the code with the following ways. The code below is used for all viewControllers.

    self = [super initWithNibName:@"ViewController" bundle:[NSBundle mainBundle]];
    return self;

What is the use of the if(self) and self in this part?

    //And in some viewcontroller contains.
    self = [super initWithNibName:@"ViewController" bundle:[NSBundle mainBundle]];
    if (self) {
        //Do some stuff
    }
    return self;
War es hilfreich?

Lösung

When you access instance variables in your methods the code is equivalent to addressing them via self pointer:

- (void)method {
  iVar = 1;
  // actually the following line will be executed:
  self->iVar = 1;
}

So if for some reason self pointer is nil and you access your ivars in init method, your application will crash as you will try to derefenrence null pointer. e.g. the following simple example will crash:

@implementation TestObj {
  int a;
}

- (id) init {
  self = [super init];
  self = nil;

  a = 1; // Crash
  return self;
}
@end

Also if your [super init] method returned nil for any reason, that might be indicator that something went wrong and you should not proceed with object initialization and any other required work with it.

Andere Tipps

The code regarding the check if self exists :

if (self) {
   //Do some stuff
}
return self;

Is only necessary, if you want to set initial property values. This is why there is the comment

// Do some stuff

If you are not going to set any initial values to variables or properties, no check for self is necessary and it is okay to return self immediately.

if (self) is checking that the call to super actually returns an object after initializing. If something goes wrong it makes no sense to continue, and it would also generate runtime errors.

Keep in mind that the init method is supposed to initialize an object of the type you are creating a class for, and return that object. Your init method overrides the super method, and that is why you call super, so that the code from Apple is run first, and then you do some stuff with the object returned from the init method on i.e. UIViewController.

Then, of course, YOUR init method must also return that object (self).

There's always the possibility that the [super initWith...] method failed, thus returning a self that is nil because it's not been created.

In this eventuality, it's a good practice to test if self has been correctly initialized, before operating on it.

The if (self) is actually a "shortcut" to if (self != nil), as any valid object will have a non-null memory address, and thus the if test will succedd with anything different than 0 (null).

if(self) is checking to make sure that whatever 'self' is exists before it actually starts doing things to it.

self is an object, so if it doesnt exist then self will be undefined and if(self) will evaluate to false, skipping the code block.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top