Category分析

文章目录
  1. 1. 分类是如何加载的

当给类添加方法的时候,一般会想到使用 category,给类添加方法

oc 的在runtime 的时候

分类是如何加载的

查看 objc 源码调用过程

  1. objc-os.mm
    _objc_init
    map_images
    map_images_nolock

  2. objc-runtime-new.mm
    _read_images
    remethodizeClass
    attachCategories
    attachLists
    realloc、memmove、 memcpy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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);
};