Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding 'self' and setting self to super

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 :-(

like image 703
Martin Avatar asked Aug 25 '12 09:08

Martin


People also ask

What is self-concept according to super?

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.

What are the 5 stages of goals in Super's work theory?

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.

What is Super's theory called?

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.


2 Answers

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.

like image 182
driis Avatar answered Sep 22 '22 02:09

driis


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:

  1. 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.

  2. 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.

like image 35
zoul Avatar answered Sep 18 '22 02:09

zoul