Category实现原理

709 阅读2分钟

Category的原理

  • Category编译之后的底层结构是struct category_t,里面存储着分类的对象方法类方法属性协议信息
  • 在程序运行的时候,runtime会将Category的数据,合并到类信息中(类对象、元类对象中)。

无论你创建了多少个分类,分类中有多少对象方法或类方法,程序运行时, 通过runtime动态将分类

  • 对象方法都统一合并到类中;
  • 类方法都统一合并到元类中。

Category的底层结构:

1、当程序编译的时候,Category都会变成如下结构体:

struct _category_t {
	const char *name;
	struct _class_t *cls;
	const struct _method_list_t *instance_methods;
	const struct _method_list_t *class_methods;
	const struct _protocol_list_t *protocols;
	const struct _prop_list_t *properties;
};
  • 文件目录下,终端执行xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc OC源文件 -o 输出的CPP文件

2、源码

struct category_t {
    const char *name;
    classref_t cls;
    struct method_list_t *instanceMethods;
    struct method_list_t *classMethods;
    struct protocol_list_t *protocols;
    struct property_list_t *instanceProperties;
    // Fields below this point are not always present on disk.
    struct property_list_t *_classProperties;

    method_list_t *methodsForMeta(bool isMeta) {
        if (isMeta) return classMethods;
        else return instanceMethods;
    }

    property_list_t *propertiesForMeta(bool isMeta, struct header_info *hi);
};

Category的加载处理过程:

  1. 通过Runtime加载某个类的所有Category数据;

  2. 把所有Category方法属性协议数据,合并到一个大数组中,(后面参与编译的Category数据,会在数组的前面)。

  3. 将合并后的分类数据(方法属性协议),插入到类原来数据的前面

所以,如果Category中重写了类中的方法,那该方法的调用顺序 ?

  • 只调用分类中重写了的方法。
  • 且众多分类中,只会调用最后编译(编译顺序,Xcode: Build Phases --> Compile Sources)的分类中的方法。

原理:

  • 方法的实现是消息发送机制,objc_msgSend([Object Class], @selector(test));
  • 以类方法为例,消息发送机制是通过isa找到元类对象,在元类对象类方法列表中(包含了分类方法的列表)按顺序遍历查找方法,顺序就是: 1.Category数据在插到类的前面; 2.Category谁最后编译,谁在前。

⚠️:分类和类中的+load方法都会调用


Category(分类)和Extension(类扩展)的区别:

  • 类扩展里的内容是编译的时候,就已经合并到类中去了;
  • 而分类里的内容是程序运行时,通过Runtime将内容合并到类中。

Category可以添加成员变量吗 ?

  • 不可以;
  • 因为分类的底层结构中,没有用来存放成员变量的list。
  • 如果给分类添加属性,只会生成声明,set和get方法不会实现。

如何实现给分类添加"成员变量"?