在ios的方法中,自带两个参数self,_cmd(SEL)
1.self 指代什么 self指代当前消息的接受者
2.在方法内,如果想要调用父类的方法,需要使用super 调用,但此时不代表消息的接受者是父类
原理:在<objc/message.h>中有两个函数
1.objc_msgSend(id _Nullable self, SEL _Nonnull op, …)
2.objc_msgSendSuper(struct objc_super * _Nonnull super, SEL _Nonnull op, …)
3.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 */
};
#endif
引用文档中的一句话,Messages sent to an object’s superclass (using the super keyword) are sent using objc_msgSendSuper; other messages are sent using objc_msgSend.在使用super调用父类方法时会使用objc_msgSendSuper函数,该函数第一个参数是一个结构体super,super里有两个变量,一个是类的实例对象,另一个是该实例对象的父类,因此在使用super时,消息的接受者还是该self指代的对象
一个简单的例子:
-(void)test{
NSLog(@"%@--%@",self.class,[super class]);
}