Super in object initialization

Possible duplicate:
Why is it necessary to call the same init function of a superclass to initialize subclasses?

I really can't understand the role of super in initializing an object. For example, having this (example - not written by me) code:

@implementation MyObject - (id) init { if([super init]){ return self; } else { return nil; } } @end 

What is [super init] actually doing? I'm confused, can't understand the meaning

+4
source share
3 answers

You must ensure that the inherited instance variables from the MyObject superclass are correctly initialized.

+1
source

Since Objective-C is object oriented, you can inherit other classes. When you inherit other classes, you can intercept messages and decide whether you pass them to the class that you inherit. In the case of init, it is almost important to always do self = [super init] or use the assigned init method for the class to make sure the object is created correctly. Imagine if in MyObject in the init method you create an NSMutableArray that uses your class, but init was never called because someone else inherited your class and never called [super init] . Then you will have references to nil or a bad pointer where you tried to use NSMutableArray . The reason why it is important to set self equal to [super init] means the value of self, which can change, for example, when recovering errors.

 //this is valid -(id)init { if((self = [super init])) { if(someInitializationFails) { [self release]; self = nil; } } return self; } 
+2
source

Wil Shipley recommends this (since 2009):

 - (id)init; { if (!(self = [super init])) return nil; // other stuff return self; } 

But why appropriate super init return to itself?

Matt Gallagher article is trying to explain this ...

- Quote:

If you remember at the beginning, I said that initWithString: part of the typical [[MyClass alloc] initWithString: @ "SomeString"] call is converted to an objc_msgSend call:

 MyClass *myObject2 = objc_msgSend(myObject1, initSelector, @"someString"); 

So, by the time we get inside the method, it already matters; its value is myObject1 (ie, the selected object, as returned from the [MyClass alloc] call. This is important because without it, a super call would be impossible - the value is used by the compiler to send the Call:

 [super init]; 

becomes:

 objc_msgSendSuper(self, @selector(init)); 

Yes, self already matters when your initializer starts. In fact, it is almost guaranteed to be the correct, final value.

- Unquote

In fact, I think that many people are confused about the fact that each method of initialization of the "I" indicates exactly, up the chain of the superclass.

The answer to this puzzle is implied in Apple Objective-C Programming Document Language in the section "Assigned Initializers:

Note that the B version of init sends a message to itself to invoke the initWithName: method. Therefore, when the receiver is an instance of class B, it calls version B of initWithName :, and when the receiver is an instance of class C, it calls version C.

Or, in other words, the variable "I" indicates our instance, which is initialized. Again, to emphasize again, all of these initialization methods through the superclass chain are inherited by our instance, and as such, the self variable in them points to our instance (unless explicitly modified).

I'm right? Sure!

+1
source

All Articles