OC中 Self 与 Super 的区别

前奏

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。

有错误的理解欢迎指出。共同进步!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,757评论 0 9
  • 两瓶热水冷却的时间恰好 是你打完点滴的时间 是的,恰好 早春二月的冷暖有别于冬日的严寒 梦想春的枝头 有少女时的腼...
    江城妖怪阅读 145评论 3 1
  • 今天是我裸辞的第28天。 3月已接近尾声,我的工作已经空窗快1个月了,这是我从2012年3月实习以来休的最长时间的...
    桃筱夭阅读 311评论 0 0
  • 今天晨读介绍的书名我很喜欢,人生总会有办法。 有时候人们往往会思维惯性,纠结着一个问题不放,但是往往得到的答案却不...
    奔跑的丸子啊阅读 424评论 2 49
  • 我想为你唱支歌,姑娘 夜色美好,如你一般模样 你在那个城市过得好么,姑娘 会不会有人抚慰你的忧伤 我过得挺不错的,...
    知亦不知阅读 205评论 0 0