self super 到底代表什么

在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]);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值