Understanding the self and setting yourself to super

I follow several textbooks and I fall with myself. Can anyone help?

I have the following init, which is an instance method.

- (id) initWithScore:(int) s {
    self = [super init];

    if (self) {
        score = s;

    }

    return self;
}

Now, after reading the code, I set self to super init, so self now points to super. Then I check to see if self is valid and sets the value to the value that I sent to my InitWIthScore. I still have it.

But now I return self, which points to super, so how do I return my subclass?

Therefore, let's say that someone calls my class, passing through 100, my code returns super not a class, so how does it work? How does the call code have a value of 100 in the account?

Of course, yes, it really works, but I have no idea why: - (

+5
2

( super, , ), - nil, . nil init super.

self super , . :

self, Objective-C .

super, Objective-C . , super - , .

( ), super , (, , , NSObject), self , .

super self , , .

+5

:

SomeClass *foo = [[SomeClass alloc] initWithScore:100];

, , - [SomeClass alloc]. SomeClass . , self - . self SomeClass.

:

self = [super init];

[super init] self, , [SomeClass alloc]. :

  • init , nil, , nil.

  • self, , . SomeClass, ( score ..).

The How Why Cocoa . , , self super , driis.

+3

All Articles