runtime之self super _cmd

2020-05-18  本文已影响0人  火柴盒环游记
代表当前类方法的 selector
self是类的隐藏的参数,指向当前调用方法的这个类的实例
/// Specifies the superclass of an instance. 
struct objc_super {
    /// 指定类的实例
    __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 */
};

通过super调用方法,编译器会自动转成objc_msgSendSuper

objc_msgSendSuper(struct objc_super * _Nonnull super, SEL _Nonnull op, ...)
'super'只是一个“编译器指示符”,根据`super`结构体可知'super'和 'self' 
指向的是相同的消息接收者(receiver),不同的是查找方法的时候,`self`是从
当前类查找,'super'是从父类查找

例:不论是用 [self setName] 还是 [super setName],接收"setName"
这个消息的接收者都是 PersonMe* p 这个对象。不同的是,'super' 告诉编译器,
当调用 setName 的方法时,要去调用父类的方法,而不是本类里的。
上一篇下一篇

猜你喜欢

热点阅读