Understanding Objective-c Runtime (vi) Super

Source: Internet
Author: User

Super

In Objective-c, if we need to invoke a method of the parent class in a method of a class, we usually use Super, as follows:

@interface Myviewcontroller:uiviewcontroller @end @implementation Myviewcontroller-(void) viewdidload {    [super Viewdidload];     Do something ...    } @end

How to use super we all know. The question now is, how does it work?

The first thing we need to know is that super is different from self. Self is a hidden parameter to the class, and the first argument to the implementation of each method is the. Super is not a hidden parameter, it's actually just a "compiler identifier," which tells the compiler to call the method of the parent class, not the method in this class, when calling the Viewdidload method. And it actually points to the same message receiver as self. To understand this, let's look at the definition of super:

struct Objc_super {ID receiver; Class superclass; };

There are two members of this structure:

Receiver: The actual recipient of the message

Superclass: Pointer to the parent class of the current class

When we use super to receive messages, the compiler generates a objc_super struct. For the above example, receiver of this struct is the Myviewcontroller object, the same as self, and superclass to the parent class Uiviewcontroller of Myviewcontroller.

Next, when sending a message, instead of calling the Objc_msgsend function, call the Objc_msgsendsuper function, which declares the following:

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

The first parameter of the function is the objc_super struct that was generated earlier, and the second parameter is the selector of the method. The actual operation of this function is to find the selector of viewdidload from the list of methods of the superclass that the objc_super struct points to, and then objc->receiver to call this selector. At this point the operation flow is the following way.

Objc_msgsend (Objc_super->receiver, @selector (viewdidload))

Since Objc_super->receiver is self itself, the method is actually the same as the following call:

Objc_msgsend (Self, @selector (viewdidload))

For ease of understanding, we look at the following examples:

@interface myclass:nsobject @end @implementation MyClass-(void) test {    NSLog (@ "Self class:%@", self.class);    NSLog (@ "Super class:%@", Super.class);} @end

After calling the test method of MyClass, its output is:

2015-06-13 15:55:03.256 [824:209297] self class:myclass2015-06-13 15:55:03.256 [824:209297] Super Class:myclass

As you can see from the above example, both outputs are MyClass. You can use the content described above to analyze it.

Understanding Objective-c Runtime (vi) Super

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.