Reputation: 1328
I have seen self = [super init]
in init methods. I don't understand why. Wouldn't [super init]
return the superclass? And if we point self = [super init]
, are we not getting self = superclass
?
Here's an example code fragment
- (id)init
{
if (self = [super init]) {
creationDate = [[NSDate alloc] init];
}
return self;
}
Hope someone can clarify this for me. Thank you.
Upvotes: 24
Views: 11214
Reputation: 4728
Self = [super init];
According to JAVA, this mean a pointer to instance itself so object can message itself.
Same meainng of Self here in objective C,
According to JAVA, Super mean that allow to access base or parent class
Same meainng of Super here in objective C,
Now init instance to to complete the initialization process.
Upvotes: 2
Reputation: 1657
Every method that you declare has two hidden parameters: self and _cmd.
The following method:
- (id)initWithString:(NSString *)aString;
is converted by the compiler to the following function call:
id initWithString(id self, SEL _cmd, NSString *aString);
see this link for more:
http://www.cocoawithlove.com/2009/04/what-does-it-mean-when-you-assign-super.html
Upvotes: 2
Reputation: 3763
From Apple's Documentation:
Because an init... method might return nil or an object other than the one explicitly allocated, it is dangerous to use the instance returned by alloc or allocWithZone: instead of the one returned by the initializer. Consider the following code:
id myObject = [MyClass alloc];
[myObject init];
[myObject doSomething];
The init method in the example above could have returned nil or could have substituted a different object. Because you can send a message to nil without raising an exception, nothing would happen in the former case except (perhaps) a debugging headache. But you should always rely on the initialized instance instead of the “raw” just-allocated one. Therefore, you should nest the allocation message inside the initialization message and test the object returned from the initializer before proceeding.
id myObject = [[MyClass alloc] init];
if ( myObject ) {
[myObject doSomething];
} else {
// error recovery...
}
Upvotes: 1
Reputation: 552
As you have Question self = [super init]
in the if Condition suggest a specific meaning.
First of all [super init]
gives the initialization of the superclass of the existing class which is in use currently. Using [super init]
gives the super class initialization which shows that object exist of the class.
Now when you use self = [super init]
that means you are assigning the class to the self for the further utilization of the same class.
And at the end you put it in if condition as if(self = [super init])
this means you are checking whether the object of the class exist of not to prevent the foul behavior of the application.
I think it is clear now!!!
Upvotes: 6
Reputation: 48085
@MartinR has a very good answer. But do you ever wonder why "[super init] calls the superclass implementation of init with the same (hidden) self argument. (This might be the point that you understood wrongly.)" works in his 3rd point ?
Here is the excerpt from Big Nerd Ranch guide 3rd edition, chapter 2 Objective C that clarifies this point
“How does super work? Usually when you send a message to an object, the search for a method of that name starts in the object’s class. If there is no such method, the search continues in the superclass of the object. The search will continue up the inheritance hierarchy until a suitable method is found. (If it gets to the top of the hierarchy and no method is found, an exception is thrown.)”
“When you send a message to super, you are sending a message to self, but the search for the method skips the object’s class and starts at the superclass.”
This code shows how iOS Runtime performs this task
objc_msgSendSuper(self, @selector(init));
Upvotes: 4
Reputation: 539725
Assuming that MyClass
is a subclass of BaseClass
, the following happens when
you call
MyClass *mc = [[MyClass alloc] init];
[MyClass alloc]
allocates an instance of MyClass
.init
message is sent to this instance to complete the initialization process.self
(which is a hidden argument to all Objective-C methods) is
the allocated instance from step 1.[super init]
calls the superclass implementation of init
with the same (hidden)
self
argument.
(This might be the point that you understood wrongly.)In the init
method of BaseClass
, self
is still the same instance of MyClass
.
This superclass init method can now either
self
and return self
, orself
and allocate/initialize and return a different object.Back in the init
method of MyClass
: self = [super init]
is now either
MyClass
object that was allocated in step 1, orself
returned by the superclass init).So, if I understood your question correctly, the main point is that
[super init]
calls the superclass implementation of init
with the self
argument,
which is a MyClass
object, not a BaseClass
object.
Upvotes: 45
Reputation: 6280
[super init]
is the same as [self superclass_variant_of_init]
If you want to send a message to superclass, there is another approach (without using runtime library):
[[self superclass] init];
Upvotes: 1
Reputation: 11597
I would think of it as, init'ing all the supers variables etc, then you get to init your extended classes variables before it is returned.
Upvotes: 1