I have been following a number of tutorials and i am falling down with self. Can anyone help please?
I have the following init, which is an instance method.
- (id) initWithScore:(int) s {
self = [super init];
if (self) {
score = s;
}
return self;
}
Now reading through the code i set the self to the super init, hence self is now pointing to super. I then check if self is valid and set score equal to the value i sent on my InitWIthScore. I have got this so far.
But now i return self which points to the super, so how am i returning my subclass?
Hence, lets say that somebody calls my class passing in 100, my code is returning the super not the class so how does it work? How does the calling code have value of 100 in score?
of course, yes it does work but i have no idea why :-(
Super states that in making a vocational choice individuals are expressing their self-concept, or understanding of self, which evolves over time. People seek career satisfaction through work roles in which they can express themselves and further implement and develop their self-concept.
Super's theory is a combination of stage development and social role theory (Super et. al, 1996), which posits that people progress through five stages during the career development process, including growth, exploration, establishment, maintenance, and disengagement.
Super's Theory of Vocational Choice – 1954 Super recognized that the self-concept changes and develops throughout people's lives as a result of experience. People successively refine their self-concept(s) over time and application to the world of work creates adaptation in their career choice.
This construct allows the initializer (both the super
initializer and the one you are writing) with a way of failing - nil
is returned if the object cannot be initialized. That is also why you check for nil
in your init
after calling super
.
Both self
and super
point to the same data structure in memory, namely your object that you have allocated. The difference is in the language semantics of these special pointers:
When self
is used, the Objective-C compiler will start method and overload resolution at your current type.
When super
is used, the Objective-C compiler will start method and overload resolution at the immediate super type of your class. In other words, super
is just semantics for being able to call a method in your super class even though the same method exists in your current class.
In other words (slightly simplified), super
treats the memory location as if it were an instance of the super class (e.g. the type you subclassed, often NSObject
), while self
treats the memory location as a regular pointer to the type you have defined.
If you print super
and self
in the debugger, you will see they point to the same memory location.
The whole object creation call looks a bit like this:
SomeClass *foo = [[SomeClass alloc] initWithScore:100];
Now the first thing that happens is calling [SomeClass alloc]
. This will create a structure for SomeClass
in memory and return a pointer to it. Next comes the initialization stuff, where self
is the pointer from the previous step. Therefore self
points to an object of class SomeClass
.
The assignment looks a bit strange, though:
self = [super init];
In most cases [super init]
won’t change self
, it will just return the pointer created by [SomeClass alloc]
. But the assignment pattern allows for two extra options:
The superclass init
can signal a construction error by returning nil
, in which case all following initializers bail out and you get a nil
object.
The superclass initializer might decide to return a different self
pointer, for example if there’s some elaborate caching being done. The pointer should again point to an object of SomeClass
type, so that your code works (you can access score
etc).
I suggest that you read The How and Why of Cocoa Initializers by Mike Ash. And yes, it’s very good to realize that self
and super
both point to the same object, as noted by driis.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With