美文网首页iOS进阶
02-OC中对象的isa指针和superclass指针

02-OC中对象的isa指针和superclass指针

作者: 光强_上海 | 来源:发表于2020-06-14 19:31 被阅读0次

OC中的isa指针和superclass指针

OC中的对象分为哪一类

  • instance对象(实例对象)
  • class对象 (类对象)
  • meta-class对象 (元类对象)

对象存放信息包含哪些

  • instance实例对象中存放的信息?

  • class类对象中存放的信息?

  • meta-class元类对象中存放的信息?

isa指向

  • instance对象的isa指向哪里?

  • class对象的isa指向哪里?

  • meta-class对象的isa指向哪里?

superclass指向

  • class对象的supereclass指向哪里?

  • meta-class对象的supreclass指向哪里?

  • 基类meta-class对象的superclass指向哪里?

isa和superclass指针指向问题如下图所示:

image

类对象和元类对象在内存中的存储的信息

类对象的源码分析路径:objc4库 -> 搜索struct objc_class -> objc-runtime-new.h -> struct objc_class : objc_object {

类对象\元类对象在内存中的结构如下:

// objc_class结构体:
struct objc_class : objc_object {
     Class isa;
    Class superclass;
    cache_t cache;             // formerly cache pointer and vtable
    class_data_bits_t bits;    // class_rw_t * plus custom rr/alloc flags
    ...
}

// class_rw_t结构体:
struct class_rw_t {
    uint32_t flags;
    uint32_t version;

    const class_ro_t *ro;

    method_array_t methods;
    property_array_t properties;
    protocol_array_t protocols;

    Class firstSubclass;
    Class nextSiblingClass;
    char *demangledName;
    ...
}

// class_ro_t结构体
struct class_ro_t {
    uint32_t flags;
    uint32_t instanceStart;
    uint32_t instanceSize;
#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;

    method_list_t *baseMethods() const {
        return baseMethodList;
    }
};

结构图如图所示:

image

更多文章

  • ReactNative开源项目OneM(1200+star):https://github.com/guangqiang-liu/OneM:欢迎小伙伴们 star
  • 简书主页:包含多篇iOS和RN开发相关的技术文章http://www.jianshu.com/u/023338566ca5 欢迎小伙伴们:多多关注,点赞
  • ReactNative QQ技术交流群(2000人):620792950 欢迎小伙伴进群交流学习
  • iOS QQ技术交流群:678441305 欢迎小伙伴进群交流学习

相关文章

网友评论

    本文标题:02-OC中对象的isa指针和superclass指针

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