3

On the CS193p course they says that in an init method there should be an if statement to check if the [super init] works:

if (self = [super init]) {
    self.someProperty = parameter;    
}
return self;

I don't understand why this is done, as if the [super init] returns nil, the method itself will also return nil, no matter the outcome of the if statement?

EDIT: The question is; why put self = [super init] inside an if statement. (Not: Why have self = [super init] at all)

Todd Hopkinson
  • 6,803
  • 5
  • 32
  • 34
Jonathan.
  • 53,997
  • 54
  • 186
  • 290
  • `[[super alloc] init]` doesn’t make sense. You want `[super init]` instead. –  Feb 23 '11 at 22:25
  • possible duplicate of [Why should I call self=\[super init\]](http://stackoverflow.com/questions/2956943/why-should-i-call-self-super-init) –  Feb 23 '11 at 22:26
  • yes sorry,I was typing it directly into stackoverflow. And my questions is why put it in an if statement, unlike the question you linked to which is why have it at all, so please take off your vote to close. – Jonathan. Feb 23 '11 at 22:29
  • The accepted answer to that question contains the answer to your question: ‘No. Instance variables are accessed relative to the self pointer (…) self has clearly got to point to the right block of memory’. –  Feb 23 '11 at 22:31
  • @Bavarious: Ok but I wouldn't say exact duplicate. (Detail: Stackoverflow as a reference point from search engines) – Jonathan. Feb 23 '11 at 22:34

6 Answers6

4

That's make sense because in some cases [super init] can return nil, and in this case if you try to access some ivar you'll get crash.

Example:

-(id) init {
    self = [super init];
    some_ivar = [[NSObject alloc] init]; //compiler treats this as self->some_ivar
    //if self == nil you'll get EXC_BAD_ACCESS

    return self;
}
Max
  • 16,679
  • 4
  • 44
  • 57
1

Apple explains this particular concept in detail in there Objective-c programming guide. (Scroll down to the section "Handling Initialization Failure")

However Apple example code often keeps the self assignment outside the if statement

self = [super init];
if (self) {
    //init stuff...
}
return self;

And if you are using the LLVM 2.0 compiler, it will give you a warning and tell you to wrap your statement in parentheses, too. Like this:

if ((self = [super init])) {
    //init stuff...
}
return self;
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Lucas
  • 11
  • 1
0

It should be if(self = [super init]){.... init is an instance method and thus can only be called on an object that has already been alloc'd.

Ben Mosher
  • 13,251
  • 7
  • 69
  • 80
0

You need to call [super init] so that the superclass can do any one-time initialization that it needs to do. The init method returns a pointer representing the object that was initialized.

Assigning the result of [super init] back to self is a standard Objective-C convention. It's done in case the superclass, as part of its initialization work, returns a different object than the one originally created. NSString's, for example, do this

So it's self = [super init]

I have never seen [[super alloc] init], i've always used [super init], and as far as my knowledge, that's the convention

Omar
  • 1,574
  • 1
  • 9
  • 14
0

It doesn't have to be in an if statement.
you can very well have it like

self = [super init];
if ( self ) {
    self.someProperty = parameter;    
}
return self;

the if statement is there to check that self was initialized properly and it is safe to do self.someProperty = parameter

filipe
  • 3,370
  • 1
  • 16
  • 22
  • Surely it would be perfectly safe to do `self.someProperty = parameter` as it's the same as `[self setSomeProperty:parameter]` and you can send messages to nil objects. – Jonathan. Feb 23 '11 at 22:35
  • 1
    not the case. Accessing properties with nil object won't crash. The same as you send [nil_object someMethod]; – Max Feb 23 '11 at 22:36
  • @Max I never said it would crash. – filipe Feb 23 '11 at 22:43
  • I thought 'it is safe to do ...' implies that – Max Feb 23 '11 at 22:44
  • I meant it more in a 'it will have the desired effect if you do...', meaning that you do have an instance and your initialization code will effectively initialize something. – filipe Feb 23 '11 at 22:49
-1

self = [[super alloc] init]; creates a base class and the runtime will choke when you try to invoke subclass-only methods later on.

self = [super init]; is a common sight in Objective-C when the base class needs to initialize variable values or instantiate composite members, otherwise they are created with Nil or (0) values.

Chris Reid
  • 460
  • 4
  • 9