iOS进阶

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

2020-06-14  本文已影响0人  光强_上海

OC中的isa指针和superclass指针

OC中的对象分为哪一类

对象存放信息包含哪些

isa指向

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

更多文章

上一篇 下一篇

猜你喜欢

热点阅读