美文网首页iOS 开发 Objective-C
iOS 底层 day03 isa superclass

iOS 底层 day03 isa superclass

作者: 望穿秋水小作坊 | 来源:发表于2020-08-24 23:10 被阅读0次

    isasupperclass 我们会基于下面的代码定义了两个类进行分析:
    Person 继承自 NSObject,遵从 NSCopying 协议,有一个成员变量 _age,一个属性 personName,有一个实例方法和一个类方法。
    Student 继承自 Person,遵从 NSCopying 协议,,有一个成员变量 _no,一个属性 studentName,有一个实例方法和一个类方法。

    // Person
    @interface Person : NSObject <NSCopying>
    {
        @public
        int _age;
    }
    @property(nonatomic, assign) int personName;
    
    - (void)personInstanceMethod;
    + (void)personClassMethod;
    
    @end
    @implementation Person
    - (void)personInstanceMethod{
        NSLog(@"personInstanceMethod %p", self);
    }
    + (void)personClassMethod{
        NSLog(@"personClassMethod %p", self);
    }
    - (nonnull id)copyWithZone:(nullable NSZone *)zone {
        return self;
    }
    
    @end
    
    // Student
    @interface Student : Person <NSCoding>
    {
        @public
        int _no;
    }
    @property(nonatomic, assign) int stuName;
    - (void)studentInstanceMethod;
    + (void)studentClassMethod;
    
    @end
    @implementation Student
    - (void)studentInstanceMethod{
        NSLog(@"studentInstanceMethod %p", self);
    }
    + (void)studentClassMethod{
        NSLog(@"studentClassMethod %p", self);
    }
    - (void)encodeWithCoder:(nonnull NSCoder *)coder {
        
    }
    
    - (nullable instancetype)initWithCoder:(nonnull NSCoder *)coder {
        return self;
    }
    
    - (nonnull id)copyWithZone:(nullable NSZone *)zone {
        return self;
    }
    
    @end
    

    一、 isa

    isa 指针

    二、 superclass

    superclass

    三、 isa 和 supperclass 最经典的图,没有之一

    最经典的图,要求能自己手绘

    由上图我们能得出哪些结论?

    1. 从对象的角度来看,Objective-C 里面的对象分为哪三种?
    1. 实例对象(Instance Object)
    2. 类对象 (Class Object)
    3. 元类对象 (Meta-class Object)
    2. 从 isa 角度看
    • 所有实例对象的 isa 都指向 类对象
    • 所有类对象 的 isa 都指向 元类对象
    • 所有元类对象的 isa 都指向 根元类对象
    3. 从 superclass 角度看
    • 实例对象没有 superclass 指针,只有类对象元类对象有 superclass 指针
    • 类对象的 superclass 指向 父类的类对象,最终指向 nil
    • 元类对象的 superclass 指向 父类的元类对象,在根元类对象时会指向 根类对象,最终指向 nil
    4. 从 method 调用角度看
    • 实例方法通过实例对象的 isa 指针找到类对象,然后通过 superclass 往父类的类对象上找,直到找到对应方法或者抛出 unrecognized selector sent to instance 异常信息

    • 类方法通过类对象的 isa 指针找到元类对象,然后通过 superclass 往父类的元类对象上找,直到找到对应方法或者抛出 unrecognized selector sent to instance 异常信息

    • 代码角度讲,Objective-C方法调用:isa → superclass → superclass → superclass ... → superclass → nil

    四、 实践出真知,我们去 Xcode 中验证我们的猜想

    1. 证实 Student实例的 isa 指向 Student的类对象
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            Student *student = [[Student alloc] init];
            Class studentClass = [Student class];
            Class studentMetaClass = object_getClass(studentClass);
            NSLog(@"打断点位置");
        }
        return 0;
    }
    

    然后我们在控制台,打印如下调试信息:

    (lldb) p/x student->isa
    (Class) $7 = 0x001d800100002589 Student
    (lldb) p/x studentClass
    (Class) $8 = 0x0000000100002588 Student
    (lldb) p/x 0x001d800100002589 & 0x00007ffffffffff8
    (long) $9 = 0x0000000100002588 
    
    • 我们发现 student->isa 的值并不等于 studentClass 的地址值
    • 这是因为在64bit之后,isa的地址需要 &ISA_MASK 才能获取真正的地址值
    • 我们在 objc4 源码中找到x86架构下的 //# define ISA_MASK 0x00007ffffffffff8
    • 0x001d800100002589 & 0x00007ffffffffff8 = 0x0000000100002588 证实了student->isa 确实指向 studentClass 的地址值
    2. 证实 Student的类对象 指向 Student的元类对象
    • 上面的例子中的代码,我们继续调试
    (lldb) p/x studentClass->isa
    error: member reference base type 'Class' is not a structure or union
    
    • 它告诉我们,不认识 Class的isa指针,这是因为 OBJC2.0 之后,对Class结构体的定义变了,隐藏了内部细节。我们可以从源码中发现这些问题:
    OBJC2.0 的objc_class OBJC2.0 的objc_object
    • 我们可以看到,isa已经变成私有变量,所以我们在Xcode中无法直接打印
    • 所以我们需要自创一个一样的结构体,进行强制类型转换,我们即可获得isa的值
    struct sp_objc_class {
        Class isa;
    };
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            Student *student = [[Student alloc] init];
            struct sp_objc_class *studentClass =(__bridge struct sp_objc_object *)[Student class];
            struct sp_objc_class *studentMetaClass = (__bridge struct sp_objc_object *)object_getClass([Student class]);
            NSLog(@"打断点位置");
        }
        return 0;
    }
    
    • 然后我们进行断点调试
    (lldb) p/x student->isa 
    (Class) $3 = 0x001d800100002589 Student
    (lldb) p/x 0x001d800100002589 & 0x00007ffffffffff8
    (long) $4 = 0x0000000100002588
    (lldb) p/x studentClass
    (sp_objc_object *) $5 = 0x0000000100002588
    (lldb) p/x studentClass->isa
    (Class) $6 = 0x001d800100002561
    (lldb) p/x 0x001d800100002561 & 0x00007ffffffffff8
    (long) $7 = 0x0000000100002560
    (lldb) p/x studentMetaClass
    (sp_objc_object *) $8 = 0x0000000100002560
    
    • 我们得到 isa 指向的充分的代码证明
    3. 代码证实 superclass 的指向
    • 同上一个例子,我们改造一下结构体
    struct sp_objc_class {
        Class isa;
        Class superclass;
    };
    
    //#define ISA_MASK        0x00007ffffffffff8ULL
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            Student *student = [[Student alloc] init];
            struct sp_objc_class *studentClass =(__bridge struct sp_objc_object *)[Student class];
            struct sp_objc_class *studentMetaClass = (__bridge struct sp_objc_object *)object_getClass([Student class]);
            
            Person *person = [[Person alloc] init];
            struct sp_objc_class *personClass =(__bridge struct sp_objc_object *)[Person class];
            struct sp_objc_class *personMetaClass = (__bridge struct sp_objc_object *)object_getClass([Person class]);
            
            NSLog(@"打断点位置");
        }
        return 0;
    }
    
    • 然后我们进行断点调试
    (lldb) p/x studentClass->superclass
    (Class) $6 = 0x0000000100002540 Person
    (lldb) p/x personClass
    (sp_objc_class *) $7 = 0x0000000100002540
    (lldb) p/x studentMetaClass->superclass
    (Class) $8 = 0x0000000100002518
    (lldb) p/x personMetaClass
    (sp_objc_class *) $9 = 0x0000000100002518
    
    • 我们发现 Student类对象 的superclass指向 Person类对象
    • Student元类对象 的superclass指向 Person的元类对象
    4. 代码证实 Class 的内部结构,以及里面各个部分存放的内容
    新版objc_class的结构体
    • 从结构体我们发现我们需要的数据,很可能存放在 bits 中,而 bits.data 返回一个 class_rw_t 数据结构
    • class_rw_trw 一般是 readwrite 的含义,t 一般是 table 的含义
    class_rw_t 的内部结构
    • class_rw_t 的内部结构中我们发现,methods 很可能是我们的方法列表
    • property 很可能是我们的属性列表
    • protocols 很可能是我们的协议列表
    • 那还差一个 成员变量列表 ,很有可能是 class_ro_t 中, ro 的含义一般是 readonly
    class_ro_t
    • 我们发现有个 ivar,从字面意思很可能就是我们想找的成员变量列表

    • 接下来的任务就有点艰难了,因为这些结构体只在源码中有,我们日常代码中并不能访问他们,我们要像前面一样,重写这些结构体。

    • 将 main.m 重命名成 main.mm 文件

    • 添加如下结构体头文件 MJClassInfo.h

    #import <Foundation/Foundation.h>
    
    #ifndef MJClassInfo_h
    #define MJClassInfo_h
    
    # if __arm64__
    #   define ISA_MASK        0x0000000ffffffff8ULL
    # elif __x86_64__
    #   define ISA_MASK        0x00007ffffffffff8ULL
    # endif
    
    #if __LP64__
    typedef uint32_t mask_t;
    #else
    typedef uint16_t mask_t;
    #endif
    typedef uintptr_t cache_key_t;
    
    struct bucket_t {
        cache_key_t _key;
        IMP _imp;
    };
    
    struct cache_t {
        bucket_t *_buckets;
        mask_t _mask;
        mask_t _occupied;
    };
    
    struct entsize_list_tt {
        uint32_t entsizeAndFlags;
        uint32_t count;
    };
    
    struct method_t {
        SEL name;
        const char *types;
        IMP imp;
    };
    
    struct method_list_t : entsize_list_tt {
        method_t first;
    };
    
    struct ivar_t {
        int32_t *offset;
        const char *name;
        const char *type;
        uint32_t alignment_raw;
        uint32_t size;
    };
    
    struct ivar_list_t : entsize_list_tt {
        ivar_t first;
    };
    
    struct property_t {
        const char *name;
        const char *attributes;
    };
    
    struct property_list_t : entsize_list_tt {
        property_t first;
    };
    
    struct chained_property_list {
        chained_property_list *next;
        uint32_t count;
        property_t list[0];
    };
    
    typedef uintptr_t protocol_ref_t;
    struct protocol_list_t {
        uintptr_t count;
        protocol_ref_t list[0];
    };
    
    struct class_ro_t {
        uint32_t flags;
        uint32_t instanceStart;
        uint32_t instanceSize;  // instance对象占用的内存空间
    #ifdef __LP64__
        uint32_t reserved;
    #endif
        const uint8_t * ivarLayout;
        const char * name;  // 类名
        method_list_t * baseMethodList;
        protocol_list_t * baseProtocols;
        const ivar_list_t * ivars;  // 成员变量列表
        const uint8_t * weakIvarLayout;
        property_list_t *baseProperties;
    };
    
    struct class_rw_t {
        uint32_t flags;
        uint32_t version;
        const class_ro_t *ro;
        method_list_t * methods;    // 方法列表
        property_list_t *properties;    // 属性列表
        const protocol_list_t * protocols;  // 协议列表
        Class firstSubclass;
        Class nextSiblingClass;
        char *demangledName;
    };
    
    #define FAST_DATA_MASK          0x00007ffffffffff8UL
    struct class_data_bits_t {
        uintptr_t bits;
    public:
        class_rw_t* data() {
            return (class_rw_t *)(bits & FAST_DATA_MASK);
        }
    };
    
    /* OC对象 */
    struct mj_objc_object {
        void *isa;
    };
    
    /* 类对象 */
    struct mj_objc_class : mj_objc_object {
        Class superclass;
        cache_t cache;
        class_data_bits_t bits;
    public:
        class_rw_t* data() {
            return bits.data();
        }
        
        mj_objc_class* metaClass() {
            return (mj_objc_class *)((long long)isa & ISA_MASK);
        }
    };
    
    #endif /* MJClassInfo_h */
    
    
    • main.mm 文件代码内容
    #import <Foundation/Foundation.h>
    #import <objc/runtime.h>
    #import "MJClassInfo.h"
    
    // Person
    @interface Person : NSObject <NSCopying>
    {
    @public
        int _age;
    }
    @property(nonatomic, assign) int personName;
    
    - (void)personInstanceMethod;
    + (void)personClassMethod;
    
    @end
    @implementation Person
    - (void)personInstanceMethod{
        NSLog(@"personInstanceMethod %p", self);
    }
    + (void)personClassMethod{
        NSLog(@"personClassMethod %p", self);
    }
    - (nonnull id)copyWithZone:(nullable NSZone *)zone {
        return self;
    }
    
    @end
    
    // Student
    @interface Student : Person <NSCoding>
    {
    @public
        int _no;
    }
    @property(nonatomic, assign) int stuName;
    - (void)studentInstanceMethod;
    + (void)studentClassMethod;
    
    @end
    @implementation Student
    - (void)studentInstanceMethod{
        NSLog(@"studentInstanceMethod %p", self);
    }
    + (void)studentClassMethod{
        NSLog(@"studentClassMethod %p", self);
    }
    - (void)encodeWithCoder:(nonnull NSCoder *)coder {
        
    }
    
    - (nullable instancetype)initWithCoder:(nonnull NSCoder *)coder {
        return self;
    }
    
    - (nonnull id)copyWithZone:(nullable NSZone *)zone {
        return self;
    }
    
    @end
    
    
    //#define ISA_MASK        0x00007ffffffffff8ULL
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            Student *stu = [[Student alloc] init];
            mj_objc_class *studentClass = (__bridge mj_objc_class *)([Student class]);
            mj_objc_class *personClass = (__bridge mj_objc_class *)([Person class]);
            
            class_rw_t *studentClassData = studentClass->data();
            class_rw_t *personClassData = personClass->data();
            
            class_rw_t *studentMetaClassData = studentClass->metaClass()->data();
            class_rw_t *personMetaClassData = personClass->metaClass()->data();
            NSLog(@"打断点位置");
        }
        return 0;
    }
    
    
    类对象的`实例方法列表`,属性列表,协议列表 类对象的成员变量列表 元类对象的`类方法列表`
    • 至此,我们用代码证明了Class的结构体中的信息。

    相关文章

      网友评论

        本文标题:iOS 底层 day03 isa superclass

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