iOS底层原理探究

探究Objective-C中self 与super的区别与底层实

2021-05-19  本文已影响0人  huangjinsheng

1、self和super关键字解释?

(1)官方文档中self相关解释:

Whenever you’re writing a method implementation, you have access to an important hidden value, self. Conceptually, self is a way to refer to “the object that’s received this message.” It’s apointer, just like the greeting value above, and can be used to call a method on the current receiving object.

(2)官方文档中super相关解释:

There’s anotherimportant keyword available to you in Objective-C, called super. Sending a message to super is a way to call through to a method implementation defined by a superclass further up the inheritance chain. The most common use of super is when overriding a method.

苹果官方文档中明确说明self调用自身的方法,super调用父类的方法,也就是说,用self进行方法调用,会先从自身的方法列表中开始查找,如果没有再从父类的方法里列表中查找,super则一开始就从父类的方法列表中开始查找;由此可见super调用在某种情况下会更加快速
注意,self和super进行方法调用只是在查找方法的起始顺序不同,但消息的接收者是一样的,都是当前的self;下面我们来进行探究验证:

2.探究self调用方法与super调用方法的区别?

//JSPerson.m的实现
@implementation JSPerson
-(instancetype)init{
    self = [super init];
    if (self) {
        NSLog(@"%@-%@",[self class],[super class]);
    }
    return self;
}

//main.m的实现
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        JSPerson *person = [[JSPerson alloc] init];
    }
    return 0;
}

要探究其底层实现,我们可以利用clang命令将其编译成.cpp文件
进入当前JSPerson.m文件目录
终端执行:clang -rewrite-objc JSPerson.m -o JSPerson.cpp
生成JSPerson.cpp的C++文件,部分代码如下:

// @implementation JSPerson

static instancetype _I_JSPerson_init(JSPerson * self, SEL _cmd) {
    self = ((JSPerson *(*)(__rw_objc_super *, SEL))(void *)objc_msgSendSuper)((__rw_objc_super){(id)self, (id)class_getSuperclass(objc_getClass("JSPerson"))}, sel_registerName("init"));
    if (self) {
        NSLog((NSString *)&__NSConstantStringImpl__var_folders_q9_m967z0vn5js5qqnp7gbfdwq80000gn_T_JSPerson_7c19d1_mi_0,((Class (*)(id, SEL))(void *)objc_msgSend)((id)self, sel_registerName("class")),
        ((Class (*)(__rw_objc_super *, SEL))(void *)objc_msgSendSuper)((__rw_objc_super){(id)self, (id)class_getSuperclass(objc_getClass("JSPerson"))}, sel_registerName("class")));
    }
    return self;
}

[self class] 对应C++的代码: ((Class (*)(id, SEL))(void *)objc_msgSend)((id)self, sel_registerName("class"))

[super class]对应C++的代码:((Class (*)(__rw_objc_super *, SEL))(void *)objc_msgSendSuper)((__rw_objc_super){(id)self, (id)class_getSuperclass(objc_getClass("JSPerson"))}, sel_registerName("class")

查看OC源码中objc_msgSend和objc_msgSendSuper方法的声明如下:
objc_msgSend(void /* id self, SEL op, ... */ )
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
    
OBJC_EXPORT void
objc_msgSendSuper(void /* struct objc_super *super, SEL op, ... */ )
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);

由此可见编译器对super关键字的处理是构建的一个objc_super的结构体,查看objc_super结构体的源码如下:

struct objc_super {
    /// Specifies an instance of a class.
    __unsafe_unretained _Nonnull id receiver;

    /// Specifies the particular superclass of the instance to message. 
#if !defined(__cplusplus)  &&  !__OBJC2__
    /* For compatibility with old objc-runtime.h header */
    __unsafe_unretained _Nonnull Class class;
#else
    __unsafe_unretained _Nonnull Class super_class;
#endif
    /* super_class is the first class to search */
};

可以看到objc_super结构体内部有一个receiver和super_class的成员,receiver存储消息的接收者,而我们查看[super class]对应的C++代码中发现objc_msgSendSuper的第一个参数是(__rw_objc_super){(id)self, (id)class_getSuperclass(objc_getClass("JSPerson"))}结构体,也就是super_class,其第一个参数接收的是(id)self,赋值给内部的receiver成员,
从这里可以明确分析出objc_msgSendSuper的第一个参数是super_class结构体指针,而super_class内部结构的第一个成员变量receiver存储的是从外部接收进来的(id)self,根据指针的访问我们可以知道objc_msgSendSuper的接收者也是self

 结论:NSLog(@"%@-%@",[self class],[super class])打印的结果为:JSPerson-JSPerson
 
 苹果为什么要实现这样的super逻辑?
    . 个人观点是可以加快方法的查找,iOS中方法的查找是从子类到父类再到NSObject根类的一个流程,使用super可以直接定位到父类的方法开始查找,省去了子类的查找环节
上一篇下一篇

猜你喜欢

热点阅读