前奏
self 是什么? super 是什么?
动态方法中:self 是代表 “对象”,
静态方法中:self 是代表 “类”。
self 就代表当前方法的调用者,
super 相当于调用父类的方法。
这个例子我就不写了。
如果在 init 中调用 self 和 super 是否会有不同?
新建一个类叫 Son 继承 NSObject
- (id)init {
if (self = [super init] ) {
NSLog(@"%@", NSStringFromClass([self class])); // Son
NSLog(@"%@", NSStringFromClass([super class])); // Son
NSLog(@"%@", NSStringFromClass([self superclass])); // Son
}
return self;
}
结果如下
2017-09-24 13:25:11.109332+0800 Demo[22406:879505] Son
2017-09-24 13:25:11.109567+0800 Demo[22406:879505] Son
2017-09-24 13:25:11.109681+0800 Demo[22406:879505] NSObject
- [self class] 和 [super class] 为什么输出的类型 是一样的?
为什么?
- self 是类隐藏的参数,指向当前调用方法的类,另外一个隐藏参数是_cmd,代表当前类方法的 selector。
- super 并不是隐藏参数,只是一个 “编译器指示符”,它和 self 指向的是相同的消息接收者。所以上面不论是 [self class] 和 [super class] 输出的类型是一样的。
- 不同之处: super 会告诉编译器,当调用 setXXX 的方法时,要去调用父类的方法,而不是本类的。self 会从当前类的方法列表中开始找,没有的话再去父类中找。
又做了一个实验:
- 又创建了一个继承于 Son 的类 叫 Bom
- 分别在 Son 和 Bom 实现了一个 + 号方法 eat。
- 在 Bom 的 init 方法里面写以下代码
- (id)init {
if (self = [super init]) {
[[super superclass] eat];
[[self superclass] eat];
[[super class] eat];
[[self class] eat];
}
return self;
}
-
输出为下面
2017-09-25 09:32:36.390684+0800 Demo[31497:1089563] 执行了 Son 的 class 2017-09-25 09:32:36.390756+0800 Demo[31497:1089563] 执行了 Son 的 class 2017-09-25 09:32:36.390838+0800 Demo[31497:1089563] 执行了 Bom 的 class 2017-09-25 09:32:36.390920+0800 Demo[31497:1089563] 执行了 Bom 的 class
结果和我们得到的结论是一模一样的。
原理:
-
这种机制底层是怎么回事?
-
Apple 是这样说的
When it encounters a method invocation, the compiler might generate a call to any of several functions to perform the actual message dispatch, depending on the receiver, the return value, and the arguments. You can use these functions to dynamically invoke methods from your own plain C code, or to use argument forms not permitted by NSObject’s perform… methods. These functions are declared in /usr/include/objc/objc-runtime.h.objc_msgSend sends a message with a simple return value to an instance of a class.
objc_msgSend_stret sends a message with a data-structure return value to an instance of a class.
objc_msgSendSuper sends a message with a simple return value to the superclass of an instance of a class.
objc_msgSendSuper_stret sends a message with a data-structure return value to the superclass of an instance of a class.
-
-
这里我们只需要关注 以下两个方法
id objc_msgSend(id theReceiver, SEL theSelector, ...) id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
msgSend 方法
id theReceiver :消息接收者。
-
SEL theSelector:调用具体类方法的 selector
[self setXXX]; 编译器会替换成 objc_msgSend 的函数调用。消息的接收者是 self,所有后面的 selector 会从当前 self 的方法表里面找 setXXX,找到后把对应的 selector 传过去。
msgSendSuper 方法
struct objc_super :是 objc_super 的结构体
-
super, SEL op :类似于 selector
struct objc_super 结构体如下
struct objc_super { id receiver; Class superClass; };
- receiver 相当于消息接收者
- Class superClass 相当于父类,比如在上面的例子 Bom 中调用 就是他的父类 Son
大概机制就这样了。
总结:
比如在 Bom 中调用 [self class]
使用 objc_msgSend 第一个参数是 self 也就是 Bom *bom 这个实例。
第二个参数 找到这个 class 的 selector。 先去 Bom 这个类去找,没有就去父类 Son 中找, 也没有的话, 就去 Son 的父类 NSObject 中找,最终在 NSObject 中找到了 这个 class 方法, 然后 返回 receiver 类别, 所以输出 为 Bom,-
当调用的是 [super class]
使用 objc_msgSendSuper 的方法
第一个参数:结构体- 第一个成员变量: self
- 第二个成员变量:Son
第二个参数:去 superClass(Son) 中找 class 方法,没有,然后去NSObject 中找,找到了, 然后内部使用函数 objc_msgSend(objc_super->receiver, @selector(class)) 去调用,这样就和上面的 [self class] 调用相同了。所以 此时的 receiver 还是 Bom *bom,所以返回的也是 Bom。