Objective-c language--self and Super keyword parsing

Source: Internet
Author: User



Look at the code:


 
@implementation Son : Father - (id)init{

    self = [super init]; if (self){
    } return self;
}


What is self and what is super ?



In dynamic methods : Self stands for "object";
In static methods : Self stands for "class";
One sentence summary is: Selfrepresents the caller of the current method;



Self and super are the two reserved words provided by objective-c. But there is a fundamental difference, self is the hidden parameter variable of the class, pointing to the object that is currently calling the method (the class is also the object, the class object), and the other hidden parameter is _cmd, which represents the selector of the current class method. Super is not a hidden parameter, it's just a "compiler indicator".



What did [Super Init] Do



Send Message :


 
Class  A - reposition{  
     ...  
     [self setOrigin:someX :someY];  
     ...  
}


A a= [A.. init]; [A reposition];
The compiler in the method body will
[Self SetOrigin:someX:someY];
Convert it to



Objc_msgsend (id Self,sel _cmd, ...). Self-a
At this point, self refers to the A object, and the method starts looking in a method schedule table corresponding to a class structure, and if it is not found, the inheritance chain is looked for in the parent class.



Similarly, if reposition is a class method, self refers to the class A object.


 
Class  A -reposition{  
     ...  
     [super setOrigin:someX :someY];  
     ...  
}


[A reposition]; The compiler in the method body will



[Super SetOrigin:someX:someY];
Convert it to



ID objc_msgsendsuper (struct objc_super *super, SEL op, ...)



The first parameter is a objc_super structure, the second parameter is similar to the selector of the class method above, first look at objc_super what this struct is:



struct Objc_super {
ID receiver;
Class superclass;
};



You can see that this struct contains two members, one is receiver, this is similar to the first parameter receiver of the above Objc_msgsend, the second member is what is the parent class that records the Super class, take the code above as an example, when the compiler encounters A



[Super SetOrigin:someX:someY]
, start doing these things:



Constructs the structure of the Objc_super, at which point the first member variable receiver of the struct is a, which is the same as self. The second member variable, superclass, refers to the superclass of Class A.



Call the Objc_msgsendsuper method to make this struct and



Setorigin
The SEL passes past. The function inside is doing something like this: from the Superclass method list of the objc_super structure to find Setorigin selector, find and then objc_super->receiver to call this selector, The Objc_msgsend function may also be used, but the first parameter thereceiver is Objc_super->receiver, and the second argument is found from Objc_super->superclass Selector



Why should self = [super init];



Conforms to the OC Inheritance Class initialization specification Super too, [Super Init] goes to self's super to call Init super to call Supersuper's init. Until init in the root class NSObject, init in the root class is responsible for initializing the memory area to add some necessary properties to the inside, returning the memory pointer, so that the memory pointer that is initialized by the inheritance chain is passed from top to bottom, and the necessary attributes of the subclass are added to the block memory in different subclasses until our A To get the memory pointer in the class, assign a value to the Slef parameter in the IF (Slef) {//Add a property}



Here's a look at this:


 
@implementation Son : Father - (id)init
{
    self = [super init]; if (self)
    {
        NSLog(@"%@", NSStringFromClass([self class]));
        NSLog(@"%@", NSStringFromClass([super class]));
    } return self;
} @end


It should not be difficult to analyze the printed result:



Son
Son



When sending a class message, whether self or super, the message body remains self, meaning that self and super point to the same object. Just find the location difference of the method, one from this class, and one from the superclass of this class.
In general, the class method is defined only in the root class NSObject, and very few subclasses rewrite the class method.
So [Slef class] and [Super class] Both find the method implementation in the root class, and the message receiving body is both a
If the override may be different.
Naturally print out Son



In an example:


#import <Foundation / Foundation.h>

@interface EngineSuper: NSObject
-(void) printCurrentClass;
@end

#import "EngineSuper.h"

@implementation EngineSuper
-(void) printCurrentClass {

     NSLog (@ "= EngineSuper =======% @", [self class]);
}
@end



@interface Engine: EngineSuper
-(void) printSupClass;
@end


@implementation Engine

-(void) printSupClass {
    [super printCurrentClass];
}

//transfer:
Engine * engine = [[Engine alloc] init];
[engine printCurrentClass]; // Call the parent class method directly, the engine does not overload it

[engine printSupClass]; // Indirectly call the parent class method, 


Printing is of course:



Engine
Engine



The self in the method body always refers to the recipient of the method and the object engine. ,
Replace with NSLog (@ "=enginesuper=======%@", [Super class]); The result is the same.



Super is a fake hair, compiler symbol, it can be replaced by [Slef class], but the method is to start from the Super class self to find.






Turn from: here



Objective-c language--self and Super keyword parsing


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.