IOS Self and Super detailed implementation principles and differences _ios

Source: Internet
Author: User

Self and Super Difference

1, self calls its own method, super calls the parent class method
2, Self is class, super is precompiled instruction
3, "Self class" and "Super class" output is the same

Self and Super Bottom implementation principles

1. When the method is invoked using self, it is searched from the list of methods of the current class, if not, from the parent class, and when super is used, the method of the parent class is called from the list of methods of the parent class.

2. When invoked with self, the Objc_msgsend function is used: ID objc_msgsend (ID thereceiver, SEL theselector, ...). The first parameter is the message receiver, the second parameter is the selector of the specific class method that is invoked, followed by the variable parameters of the selector method. In the case of [self setname:] As an example, the compiler replaces the function call to call Objc_msgsend, where Thereceiver is the Self,theselector is @selector (setname:), which is the selector from the current Self's class method list starts looking for the setname, and when found, transmits the corresponding selector to the past.

3. When using the super call, the Objc_msgsendsuper function is used: ID objc_msgsendsuper (struct objc_super *super, SEL op, ...) The first argument is a objc_super structure, and the second argument is the selector of the class method above,

struct Objc_super {  
   id receiver;  
   Class superclass; 
}; 

When the compiler encounters [Super SetName:], start doing these things:

1 constructs a objc_super structure, at which point the first member variable of the struct receiver is a subclass, same as self. The second member variable, superclass, refers to the method by which the parent class invokes the Objc_msgsendsuper and passes the sel of the struct and setname to the past.

2 function In doing something like this: from the objc_super structure points to the superclass list of methods to start looking for SetName selector, find and then objc_super->receiver to call this selector

Thank you for reading, I hope to help you, thank you for your support for this site!

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.