iOS的super

作者: GeniusWong | 来源:发表于2018-09-21 02:15 被阅读3次

    isKindOfClass

    isMemberClass

    @implementation Teacher
    
    -(id)init
    {
          self = [super init];
          if (self) {
            [self class];//  Teacher
            [self superclass]; // NSObject
    // 这两块,打印输出是一样的?
            [super class];  // Teacher
            [super superclass];//  NSobject
            // objc_msgSuper( { self, 父类 } ,  selector()) ;
          }
    }
    
    @end
    
    • 这时候要看一下 objc_super的源代码了
      • [super class] [self class]class 的方法实现都在NSObject里
      • -(Class)class 的实现,objc_getClass(self),返回值取决于 self是谁,取决于消息机制
      • 消息接收者仍然子类对象,只不过是从父类里开始查找方法,objc_msgSuper( { self, 父类 } , selector()) ;
    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 */
    };
    

    相关文章

      网友评论

        本文标题:iOS的super

        本文链接:https://www.haomeiwen.com/subject/vdionftx.html