美文网首页
OC类对象结构体中的class_rw_t

OC类对象结构体中的class_rw_t

作者: 郭小弟 | 来源:发表于2018-08-30 09:39 被阅读27次

前言

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

前面文章有提到类对象的结构,本篇文章只是简单的看一下类的内部结构,为后面的文章做铺垫,只有了解了类的结构,后面写到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
这个是可写的,因为后期可能会有多个分类需要合并到类的方法列表中,还有可能动态添加方法
下面两个和方法列表差不多,就不列举了;

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

相关文章

  • OC类对象结构体中的class_rw_t

    前言 发布此文章主要是对自己所学知识的总结 通过文章的方式可以让自己对所学知识加深印象 方便日后需要的时候查看,如...

  • OC总结篇 - OC对象及方法

    1.OC对象 = 结构体id对象 = objc_object结构体继承类对象 = objc_class结构体 ob...

  • iOS:弄懂OC中的类与元类

    首先,来看看什么是类。类在OC中其实是一个指向objc_class的结构体指针,结构体的构造为: OC中对象的定义...

  • OC中类的本质

    OC中的对象的本质 对象本身是一个含有isa指针的结构体,isa指针指向对象所属的类类的本质:类本质也是一个结构体...

  • 52个有效方法(14) - 理解“类对象”的用意

    在OC中,类,对象和方法其实都是一个C的结构体。 描述Objective-C对象所用的数据结构定义 每个对象结构体...

  • OC中实例对象、类、元类

    对象 OC是C语言的扩展,OC中的对象是由C语言中结构体实现的。2006年苹果发布Objc 2.0 对象,类,元类...

  • Runtime

    oc对象 OC类对象元类对象编译后的结构如下结构体 isa arm64架构之前isa只是一个指针,指向类对象或者元...

  • OC底层-类和对象

    类和对象 OC中的类 OC中.类基于C/C++的结构体. 通过查看NSObject的类定义,可以看到内部有一个Cl...

  • 类与结构体

    在 OC 中:1.结构体只能封装属性,而类不仅可以封装属性还可以封装方法.2.结构体变量分配在栈,OC 对象分配在...

  • isa理解

    由类生成对象。对象的结构体实例通过isa这个成员变量来保持类的结构体实例指针,建立类与对象间的关系。oc运行时为每...

网友评论

      本文标题:OC类对象结构体中的class_rw_t

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