Inquisitive objective-c Runtime (1)-Self & Super

Source: Internet
Author: User

Inquisitive objective-c Runtime (1)-Self & Super-chun Tips

Chun TipsFocus on iOS development Inquisitive objective-c Runtime (1)-Self & Super Preface

About OBJECTIVE-C Runtime a good document: Understanding the OBJECTIVE-C Runtime

The address is: http://blog.cocoabit.com/blog/2014/10/06/yi-li-jieobjective-cruntime/

OBJECTIVE-C Runtime source is open source, for: http://opensource.apple.com/tarballs/objc4/

Exercise content

@ Tang Qi _boy on Weibo to share their technical seminar on OBJC runtime discussion exercises, exercises from sunnyxx, his blog address is http://blog.sunnyxx.com.

The following is the exercise content (photo from @ Tang Qi _boy Weibo):

I finished these questions, also by the way to review some of the OBJECTIVE-C runtime knowledge, now tidy up and share to everyone.

This note is divided into four articles:

    • Inquisitive objective-c Runtime (1)-Self & Super
    • Inquisitive objective-c Runtime (2)-Object & class & Meta class
    • Inquisitive objective-c Runtime (3)-Messages and category
    • Inquisitive objective-c Runtime (4)-member variables and properties
Inquisitive

What does the following code output?

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

Answer: All output Son

2014-11-05 11:06:18.060 Test[8566:568584] NSStringFromClass([self class]) = Son2014-11-05 11:06:18.061 Test[8566:568584] NSStringFromClass([super class]) = Son

Question: This topic mainly examines the understanding of self and Super in OBJC.

Self is a hidden parameter to the class that points to an instance of the class that is currently calling the method. While Super is a Magic Keyword, it is essentially a compiler identifier, and self is the same message recipient that the self is pointing to. The above example either invokes the [self class] or super class, and the object that accepts the message is the current Son *xxx object. The difference is that super is telling the compiler to call the method of class to go to the parent's method, not in this class.

When the method is called with self, it is looked up from the list of methods in the current class, if not, from the parent class, and when super is used, it starts from the list of methods in the parent class. Then call this method of the parent class.

Is that really the case? Keep looking:

Use the clang rewrite command:

$ clang -rewrite-objc test.m

The above code was found to be converted to:

    NSLog((NSString *)&__NSConstantStringImpl__var_folders_gm_0jk35cwn1d3326x0061qym280000gn_T_main_a5cecc_mi_0, NSStringFromClass(((Class (*)(id, SEL))(void *)objc_msgSend)((id)self, sel_registerName("class"))));    NSLog((NSString *)&__NSConstantStringImpl__var_folders_gm_0jk35cwn1d3326x0061qym280000gn_T_main_a5cecc_mi_1, NSStringFromClass(((Class (*)(__rw_objc_super *, SEL))(void *)objc_msgSendSuper)((__rw_objc_super){ (id)self, (id)class_getSuperclass(objc_getClass("Son")) }, sel_registerName("class"))));

From the code above, we can see that when called [self class] , it is converted into a objc_msgSend function. Look at the function definition:

id objc_msgSend(id self, SEL op, ...)

We self pass in as the first parameter.

[super class]when called, it is converted into a objc_msgSendSuper function. Look at the function definition:

id objc_msgSendSuper(struct objc_super *super, SEL op, ...)

The first parameter is objc_super such a struct, which is defined as follows:

struct objc_super {   __unsafe_unretained id receiver;   __unsafe_unretained Class super_class;};

The struct has two members and the first member is receiver, similar to the above Objc_msgsend function, the first parameter , self. The second member is a record of what the parent class of the current class is.

So, when called [self class] , the actual first call is the objc_msgSend function, the first argument is the Son current instance, and then in Son this class to find - (Class)class this method, no, go to the parent class to Father find, there is no, finally found in the NSObject class this method. - (Class)classthe implementation is to return to the category of self, so the above output is Son.

OBJC Runtime Open source code for the - (Class)class implementation of the method:

- (Class)class {    return object_getClass(self);}

When called [super class] , it is converted into a objc_msgSendSuper function. The first step is to construct the objc_super structure, and the first member of the struct is self. The second member is (ID) class_getsuperclass (objc_getclass ("Son")) , which actually outputs the result as Father. The second step is to go to Father This class to find - (Class)class , no, then go to the NSObject class to find, found. Finally the internal is used objc_msgSend(objc_super->receiver, @selector(class)) to call, at this time and the [self class] call is the same, so the above output is still returned to Son.

下一篇博客的主要分享的内容是关于Objective-C Runtime中的Object & Class & Meta Class学习笔记。

    • This article is about objective C Runtime's learning notes. If there are any wrong places, please correct me.
    • Thanks @ Tang Qi _boy and @sunnyxx share the topic.
    • This article was published by @chun in Chun Tips.
    • Copyright Disclaimer: Free Reprint-Non-commercial-non-derivative-retain attribution | Creative Commons by-nc-nd 3.0

Share to:

Posted by Chun Ye Nov 5th, 5:11 am objective-c Runtime

Inquisitive objective-c Runtime (2)-Object & Class & Meta class»

Comments

Inquisitive objective-c Runtime (1)-Self & 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.