面试题
- (instancetype)init
{
if (self = [super init]) {
NSLog(@"[self class] = %@", [self class]);
NSLog(@"[self superclass] = %@", [self superclass]);
NSLog(@"[super class] = %@", [super class]);
NSLog(@"[super superclass] = %@", [super superclass]);
}
return self;
}
@end
2021-06-23 14:22:30.124579+0800 super[15478:176287] [self class] = Dog
2021-06-23 14:22:30.124700+0800 super[15478:176287] [self superclass] = Animal
2021-06-23 14:22:30.124805+0800 super[15478:176287] [super class] = Dog
2021-06-23 14:22:30.124903+0800 super[15478:176287] [super superclass] = Animal
上述代码中可以发现无论是self还是super调用class或superclass的结果都是相同的。
super底层实现
使用下面命令将 Dog.m转换成c++
xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc -fobjc-arc -fobjc-runtime=ios-8.0.0 Dog.m
实现如下
static instancetype _I_Dog_init(Dog * self, SEL _cmd) {
if (self = ((Dog *(*)(__rw_objc_super *, SEL))(void *)objc_msgSendSuper)((__rw_objc_super){(id)self, (id)class_getSuperclass(objc_getClass("Dog"))}, sel_registerName("init"))) {
NSLog((NSString *)&__NSConstantStringImpl__var_folders_dw_4ylsshdj70nf81ndx_765tfh0000gn_T_Dog_def1c4_mi_0, ((Class (*)(id, SEL))(void *)objc_msgSend)((id)self, sel_registerName("class")));
NSLog((NSString *)&__NSConstantStringImpl__var_folders_dw_4ylsshdj70nf81ndx_765tfh0000gn_T_Dog_def1c4_mi_1, ((Class (*)(id, SEL))(void *)objc_msgSend)((id)self, sel_registerName("superclass")));
NSLog((NSString *)&__NSConstantStringImpl__var_folders_dw_4ylsshdj70nf81ndx_765tfh0000gn_T_Dog_def1c4_mi_2, ((Class (*)(__rw_objc_super *, SEL))(void *)objc_msgSendSuper)((__rw_objc_super){(id)self, (id)class_getSuperclass(objc_getClass("Dog"))}, sel_registerName("class")));
NSLog((NSString *)&__NSConstantStringImpl__var_folders_dw_4ylsshdj70nf81ndx_765tfh0000gn_T_Dog_def1c4_mi_3, ((Class (*)(__rw_objc_super *, SEL))(void *)objc_msgSendSuper)((__rw_objc_super){(id)self, (id)class_getSuperclass(objc_getClass("Dog"))}, sel_registerName("superclass")));
}
return self;
}
objc_msgSendSuper函数内传递了两个参数。__rw_objc_super结构体和sel_registerName("class")方法名。
__rw_objc_super结构体内传入的参数是self和class_getSuperclass(objc_getClass("Dog"))
objc_msgSendSuper的底层结构
/// Specifies the superclass of an instance.
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_msgSendSuper的底层结构可以看到
super调用方法的消息接受者receiver仍然是self,只是从父类的类对象开始去查找方法。
super汇编实现
上图中可以发现super底层其实调用的是objc_msgSendSuper2函数,我们来到源码中查找一下objc_msgSendSuper2函数的底层实现,我们可以在汇编文件中找到其相关底层实现。
/********************************************************************
* id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
*
* struct objc_super {
* id receiver;
* Class cls; // SUBCLASS of the class to search
* }
********************************************************************/
ENTRY _objc_msgSendSuper2
ldr r9, [r0, #CLASS] // class = struct super->class
ldr r9, [r9, #SUPERCLASS] // class = class->superclass
CacheLookup NORMAL, _objc_msgSendSuper2
// cache hit, IMP in r12, eq already set for nonstret forwarding
ldr r0, [r0, #RECEIVER] // load real receiver
bx r12 // call imp
CacheLookup2 NORMAL, _objc_msgSendSuper2
// cache miss
ldr r9, [r0, #CLASS] // class = struct super->class
ldr r9, [r9, #SUPERCLASS] // class = class->superclass
ldr r0, [r0, #RECEIVER] // load real receiver
b __objc_msgSend_uncached
END_ENTRY _objc_msgSendSuper2
ENTRY _objc_msgLookupSuper2
ldr r9, [r0, #CLASS] // class = struct super->class
ldr r9, [r9, #SUPERCLASS] // class = class->superclass
CacheLookup NORMAL, _objc_msgLookupSuper2
// cache hit, IMP in r12, eq already set for nonstret forwarding
ldr r0, [r0, #RECEIVER] // load real receiver
bx lr
CacheLookup2 NORMAL, _objc_msgLookupSuper2
// cache miss
ldr r9, [r0, #CLASS]
ldr r9, [r9, #SUPERCLASS] // r9 = class to search
ldr r0, [r0, #RECEIVER] // load real receiver
b __objc_msgLookup_uncached
END_ENTRY _objc_msgLookupSuper2
struct objc_super {
id receiver;
Class cls; // SUBCLASS of the class to search
}
从汇编实现可以看到,消息接受者是receiver,另外一个参数是当前类,然后通过class->superclass,去父类中cache中搜索方法,搜索的起始位置是父类