简体   繁体   中英

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

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

  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM