OC类对象结构体中的class_rw_t

前言

  • 发布此文章主要是对自己所学知识的总结
  • 通过文章的方式可以让自己对所学知识加深印象
  • 方便日后需要的时候查看,如果有不对的地方欢迎指出
  • 文笔不行,多多见谅

前面文章有提到类对象的结构,本篇文章只是简单的看一下类的内部结构,为后面的文章做铺垫,只有了解了类的结构,后面写到cache和分类的时候才明白到底是怎么回事
源码-723可自行下载分析

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 *data() { 
        return bits.data();
    }
}
struct class_data_bits_t {
     class_rw_t* data() {
        return (class_rw_t *)(bits & FAST_DATA_MASK);
    }
}

本次主要探究一下class_rw_t里面都有些什么鬼

struct class_rw_t {
    // Be warned that Symbolication knows the layout of this structure.
    uint32_t flags;
    uint32_t version;

    const class_ro_t *ro;

    method_array_t methods;
    property_array_t properties;
    protocol_array_t protocols;
}

const class_ro_t *ro:保存类的原始数据(不包含分类内容和动态添加的方法)
methods :方法列表(如果是类对象存储的是对象方法,元类对象存储的是类方法)
properties :属性列表
protocols :协议列表

const class_ro_t *ro

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;
    }
};

只读结构体,储存了类的初始信息,不包括分类和后来动态添加的东西
method_list_t:数组,包含了多个method_t,每个method_t也是一个结构体

struct method_t

struct method_t {
    SEL name;//方法名
    const char *types;//方法参数
    IMP imp;//方法的实现

    struct SortBySELAddress :
        public std::binary_function<const method_t&,
                                    const method_t&, bool>
    {
        bool operator() (const method_t& lhs,
                         const method_t& rhs)
        { return lhs.name < rhs.name; }
    };
};

每个method_t代表一个方法,包括:
name:函数名
types:包含了返回值类型,参数类型
imp : 指向函数的指针(方法的实现)
简单说下types:
- (int)test:(int)age height:(float)height;
以上面那行代码为例
(const char *) types = 0x0000000101759dbf "i24@0:8i16f20"

i24@0:8i16f20
第一个i代表返回值的类型是int
24:代表这个函数的大小, 代表这个函数的大小,每个函数还有两个隐藏的参数(id)self,(SEL)_cmd,这两个都是指针类型,每个占用8个字节,(int)age4个字节,(float)height4个字节,加一起就是24
@ : id类型,第一个参数self
0 : 从多少个字节开始
: 第二个参数_cmd
8 : 从第8个字节开始,前面只有一个self,占用8个字节
i : int类型age
16 : 从第16个字节开始
f : float类型
20 : 从第20个字节开始

method_array_t methods

主要用来存储对象方法,包含了动态添加的方法和分类的方法

class method_array_t : 
    public list_array_tt<method_t, method_list_t> 
{
    typedef list_array_tt<method_t, method_list_t> Super;

 public:
    method_list_t **beginCategoryMethodLists() {
        return beginLists();
    }
    
    method_list_t **endCategoryMethodLists(Class cls);

    method_array_t duplicate() {
        return Super::duplicate<method_array_t>();
    }
};

二维数组,外层是method_list_t,每个method_list_t又包含了多个method_t
这个是可写的,因为后期可能会有多个分类需要合并到类的方法列表中,还有可能动态添加方法
下面两个和方法列表差不多,就不列举了;

公司不忙的时候就是爽啊,自己想干点啥干点啥,不用每天去开会什么的,做点自己想做的事是多么幸福的一件事

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

推荐阅读更多精彩内容