简单了解Category

305 阅读8分钟

简单了解

OC2.0之后添加的语言特性,主要为已经存在的类添加方法。 Apple推荐的两个使用场景:

  1. 把类的实现分开在不同的文件里面。
    1. 可以减少单个文件的体积
    2. 可以把不同的功能组织到不同的Category里
    3. 可以由多个开发者共同开发一个类
    4. 可以按需加载想要的Category
  2. 声明私有方法

衍生的使用场景:

  1. 模拟多继承
  2. 把framework的私有方法公开

和extension区别

  1. extension在编译器决定,它就是类的一部分,在编译期和头文件里的@interface和实现文件里的@implementation一起形成一个完整的类。它伴随类的产生而产生,也随类一起消亡。
  2. extension一般用来隐藏类的私有信息,你必须有一个类的源码才能为一个类添加extension,我们无法为系统类比如NSString添加extension。
  3. category是在运行期决定的。
  4. category无法添加实例变量,而extension可以。因为在运行期,对象的内存布局已经确定,如果添加实例变量就会破坏类的内部布局。

内部探究

分类结构

所有的OC类和对象,在runtime层都是用struct来表示的,category也不例外,才runtime层,category用结构体category_t(objc-runtime-new.h中)表示。

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);
};
  1. 类名(name)
  2. 类(cls)
  3. category中所有给类添加的实例方法的列表(instanceMethods)
  4. category中所有添加的类方法的列表(classMethods)
  5. category中实现的所有协议的列表(protocols)
  6. category中添加的所有属性(instanceProperties)
  7. category中添加的所有类属性(classProperties),这里说明了不是在所有的版本中都有。因为类属性这个概念也是后来才加上去的。
  8. 获取方法列表函数,根据传入的参数是否是元类,来决定返回类方法还是实例方法。
  9. 获取属性列表函数,第二个参数还没弄明白是干什么的。

从定义上可以看出,category可以为类添加实例方法、类方法、实现协议、添加属性、添加类属性,但是无法添加实例变量。

用clang转换一下代码

我们先写一个分类

// .h
@interface MyClass : NSObject

- (void)printName;

@end

@interface MyClass (MyAddition)

@property (nonatomic, copy) NSString *name;

- (void)printName;

@end

// .m
@implementation MyClass

- (void)printName {
    NSLog(@"%@", @"MyClass");
}

@end

@implementation MyClass (MyAddition)

- (void)printName {
    NSLog(@"%@", @"MyAddition");
}

@end

然后用clang指令:clang -rewrite-objc MyClass.m 生成.cpp文件,打开翻到最后面的Category部分

static struct /*_method_list_t*/ {
	unsigned int entsize;  // sizeof(struct _objc_method)
	unsigned int method_count;
	struct _objc_method method_list[1];
} _OBJC_$_CATEGORY_INSTANCE_METHODS_MyClass_$_MyAddition __attribute__ ((used, section (“__DATA,__objc_const”))) = {
	sizeof(_objc_method),
	1,
	{{(struct objc_selector *)”printName”, “v16@0:8”, (void *)_I_MyClass_MyAddition_printName}}
};

static struct /*_prop_list_t*/ {
	unsigned int entsize;  // sizeof(struct _prop_t)
	unsigned int count_of_properties;
	struct _prop_t prop_list[1];
} _OBJC_$_PROP_LIST_MyClass_$_MyAddition __attribute__ ((used, section ("__DATA,__objc_const"))) = {
	sizeof(_prop_t),
	1,
	{{"name","T@\"NSString\",C,N"}}
};

extern "C" __declspec(dllexport) struct _class_t OBJC_CLASS_$_MyClass;

static struct _category_t _OBJC_$_CATEGORY_MyClass_$_MyAddition __attribute__ ((used, section ("__DATA,__objc_const"))) = 
{
	"MyClass",
	0, // &OBJC_CLASS_$_MyClass,
	(const struct _method_list_t *)&_OBJC_$_CATEGORY_INSTANCE_METHODS_MyClass_$_MyAddition,
	0,
	0,
	(const struct _prop_list_t *)&_OBJC_$_PROP_LIST_MyClass_$_MyAddition,
};
static void OBJC_CATEGORY_SETUP_$_MyClass_$_MyAddition(void ) {
	_OBJC_$_CATEGORY_MyClass_$_MyAddition.cls = &OBJC_CLASS_$_MyClass;
}
#pragma section(“.objc_inithooks$B”, long, read, write)
__declspec(allocate(“.objc_inithooks$B”)) static void *OBJC_CATEGORY_SETUP[] = {
	(void *)&OBJC_CATEGORY_SETUP_$_MyClass_$_MyAddition,
};
static struct _class_t *L_OBJC_LABEL_CLASS_$ [1] __attribute__((used, section ("__DATA, __objc_classlist,regular,no_dead_strip")))= {
	&OBJC_CLASS_$_MyClass,
};
static struct _category_t *L_OBJC_LABEL_CATEGORY_$ [1] __attribute__((used, section ("__DATA, __objc_catlist,regular,no_dead_strip")))= {
	&_OBJC_$_CATEGORY_MyClass_$_MyAddition,
};
  1. 首先编译器生成了实例方法列表 _OBJC_$_CATEGORY_INSTANCE_METHODS_MyClass_$_MyAddition 和属性列表 _OBJC_$_PROP_LIST_MyClass_$_MyAddition 都遵循了公共前缀+类名+Category名字的命名方式,所以这里的Category名是不能重复的,否则就会出现编译错误。 我们可以在里面看到printName方法和name属性。
  2. 然后生成了_OBJC_$_CATEGORY_MyClass_$_MyAddition,它的类型是_category_t也就是Category本身。用了:
    • 类名”MyClass”
    • 0
    • 实例方法 (const struct _method_list_t *)&_OBJC_$_CATEGORY_INSTANCE_METHODS_MyClass_$_MyAddition)
    • 类方法0
    • 实现协议0
    • 属性(const struct _prop_list_t *)&_OBJC_$_PROP_LIST_MyClass_$_MyAddition) 这6个参数来初始化分类。
  3. 最后,编译器在DATA段下的__objc_catlist section里保存了一个大小为1的category_t的数组L_OBJC_LABEL_CATEGORY_$(如果有多个Category,会生成对应长度的数组),用于运行期Category的加载。

运行期加载

我们知道,OC的运行是依赖OC的runtime,而OC的runtime和其它系统库一样,是OS X和iOS通过dyld动态加载的。 看一下OC运行时的入口方法:

/***********************************************************************
* _objc_init
* Bootstrap initialization. Registers our image notifier with dyld.
* Called by libSystem BEFORE library initialization time
引导程序初始化。用dyld注册我们的图像通知。在库初始化之前由libSystem调用
**********************************************************************/

void _objc_init(void)
{
    static bool initialized = false;
    if (initialized) return;
    initialized = true;
    
    // fixme defer initialization until an objc-using image is found?
    environ_init();
    tls_init();
    static_init();
    lock_init();
    exception_init();

    _dyld_objc_notify_register(&map_images, load_images, unmap_image);
}

category被附加到类上是在map_images的时候发生的

/***********************************************************************
* map_images
* Process the given images which are being mapped in by dyld.
* Calls ABI-agnostic code after taking ABI-specific locks.
*
* Locking: write-locks runtimeLock
**********************************************************************/
void
map_images(unsigned count, const char * const paths[],
           const struct mach_header * const mhdrs[])
{
    mutex_locker_t lock(runtimeLock);
    return map_images_nolock(count, paths, mhdrs);
}

map_images_nolock函数是定义在objc-os.mm文件中,在最后调用了_read_images函数,我们去objc-runtime-new.mm文件中可以看到_read_images函数。这个函数很长,找到关于Category的部分。

/***********************************************************************
* _read_images
* Perform initial processing of the headers in the linked 
* list beginning with headerList. 
*
* Called by: map_images_nolock
*
* Locking: runtimeLock acquired by map_images
**********************************************************************/
void _read_images(header_info **hList, uint32_t hCount, int totalClasses, int unoptimizedTotalClasses) {
	// …
    for (EACH_HEADER) {
	    category_t **catlist = _getObjc2CategoryList(hi, &count);
        bool hasClassProperties = hi->info()->hasCategoryClassProperties();
		// 往下
    }
	// …
}

先忽略掉所有的print打印相关的代码。然后慢慢来看,首先会拿到所有的category_t数组,具体怎么获取的这里先不管。然后遍历category_t数组。

for (i = 0; i < count; i++) {
    category_t *cat = catlist[i];
    Class cls = remapClass(cat->cls);

    if (!cls) {
        // Category's target class is missing (probably weak-linked).
		  // Disavow any knowledge of this category.
        catlist[i] = nil;
		  continue;
     }
	   // 往下

这里首先会拿到分类的class,如果为空就continue。因为Category的目标类可能是弱引用的关系而丢失了。 往下看

// Process this category. 
// First, register the category with its target class. 
// Then, rebuild the class's method lists (etc) if 
// the class is realized. 
bool classExists = NO;
if (cat->instanceMethods || cat->protocols ||  cat->instanceProperties) {
    addUnattachedCategoryForClass(cat, cls, hi);
    if (cls->isRealized()) {
        remethodizeClass(cls);
        classExists = YES;
    }
}

首先把这个category注册到目标类上,然后如果实现了类就重建它的方法列表。这里我们要跳过去看一下addUnattachedCategoryForClass这个函数。不是很长。

/***********************************************************************
* addUnattachedCategoryForClass
* Records an unattached category. 记录一个未附加的category
* Locking: runtimeLock must be held by the caller.
**********************************************************************/
static void addUnattachedCategoryForClass(category_t *cat, Class cls, header_info *catHeader)
{
    runtimeLock.assertLocked();

    // DO NOT use cat->cls! cls may be cat->cls->isa instead
    NXMapTable *cats = unattachedCategories();
    category_list *list;

    list = (category_list *)NXMapGet(cats, cls);
    if (!list) {
        list = (category_list *)
            calloc(sizeof(*list) + sizeof(list->list[0]), 1);
    } else {
        list = (category_list *)
            realloc(list, sizeof(*list) + sizeof(list->list[0]) * (list->count + 1));
    }
    list->list[list->count++] = (locstamped_category_t){cat, catHeader};
    NXMapInsert(cats, cls, list);
}

先获取未附加的category,创建数组,分配内存空间,NXMapInsert这里的作用是,把类和category、list做一个关联映射。所以真正的添加还不是在这里,我们在回到外层函数去。 如果cls->isRealized()也就是说这个类被实现了,那么就执行remethodizeClass(cls)重新排列类。找到类的实现如下:

/***********************************************************************
* remethodizeClass
* Attach outstanding categories to an existing class.
将未完成的category附加到现有类。
* Fixes up cls's method list, protocol list, and property list.
修复类的方法列表、协议列表、属性列表。
* Updates method caches for cls and its subclasses.
更新类和子类的方法缓存。
* Locking: runtimeLock must be held by the caller
**********************************************************************/
static void remethodizeClass(Class cls)
{
    category_list *cats;
    bool isMeta;

    runtimeLock.assertLocked();

    isMeta = cls->isMetaClass();

    // Re-methodizing: check for more categories
    if ((cats = unattachedCategoriesForClass(cls, false/*not realizing*/))) {
        attachCategories(cls, cats, true /*flush caches*/);        
        free(cats);
    }
}

先插播一个unattachedCategoriesForClass函数

/***********************************************************************
* unattachedCategoriesForClass
* Returns the list of unattached categories for a class, and 
* deletes them from the list. 
* The result must be freed by the caller. 
返回类的未附加类别列表,并从列表中删除它们。调用者必须释放结果。
* Locking: runtimeLock must be held by the caller.
**********************************************************************/
static category_list *
unattachedCategoriesForClass(Class cls, bool realizing)
{
    runtimeLock.assertLocked();
    return (category_list *)NXMapRemove(unattachedCategories(), cls);
}

就是从map表中获取列表,然后再移除。

好,接下来我们看关键函数:attachCategories(cls, cats, true /*flush caches*/);

// Attach method lists and properties and protocols from categories to a class.
// Assumes the categories in cats are all loaded and sorted by load order, 
// oldest categories first.
// 将类别中的方法列表、属性和协议附加到类中。假设cats中的类别都是按加载顺序加载和排序的,最早的类别是第一个。
static void 
attachCategories(Class cls, category_list *cats, bool flush_caches)
{
    if (!cats) return;
    if (PrintReplacedMethods) printReplacements(cls, cats);

    bool isMeta = cls->isMetaClass();

    // fixme rearrange to remove these intermediate allocations
    method_list_t **mlists = (method_list_t **)
        malloc(cats->count * sizeof(*mlists));
    property_list_t **proplists = (property_list_t **)
        malloc(cats->count * sizeof(*proplists));
    protocol_list_t **protolists = (protocol_list_t **)
        malloc(cats->count * sizeof(*protolists));

    // Count backwards through cats to get newest categories first
    int mcount = 0;
    int propcount = 0;
    int protocount = 0;
    int i = cats->count;
    bool fromBundle = NO;
    while (i--) {
        auto& entry = cats->list[i];

        method_list_t *mlist = entry.cat->methodsForMeta(isMeta);
        if (mlist) {
            mlists[mcount++] = mlist;
            fromBundle |= entry.hi->isBundle();
        }

        property_list_t *proplist = 
            entry.cat->propertiesForMeta(isMeta, entry.hi);
        if (proplist) {
            proplists[propcount++] = proplist;
        }

        protocol_list_t *protolist = entry.cat->protocols;
        if (protolist) {
            protolists[protocount++] = protolist;
        }
    }

    auto rw = cls->data();

    prepareMethodLists(cls, mlists, mcount, NO, fromBundle);
    rw->methods.attachLists(mlists, mcount);
    free(mlists);
    if (flush_caches  &&  mcount > 0) flushCaches(cls);

    rw->properties.attachLists(proplists, propcount);
    free(proplists);

    rw->protocols.attachLists(protolists, protocount);
    free(protolists);
}

也就是说,按加载顺序,最先添加的category会先被添加。 首先分别创建三个大列表:mlists, proplists, protolists。从后往前遍历,获取每一个category的方法列表,属性列表和协议列表,放到对应列表中。 遍历完成后,开始最后的附加列表操作。

void attachLists(List* const * addedLists, uint32_t addedCount) {
    if (addedCount == 0) return;

    if (hasArray()) {
        // many lists -> many lists
        uint32_t oldCount = array()->count;
        uint32_t newCount = oldCount + addedCount;
        setArray((array_t *)realloc(array(), array_t::byteSize(newCount)));
        array()->count = newCount;
        memmove(array()->lists + addedCount, array()->lists, 
                oldCount * sizeof(array()->lists[0]));
        memcpy(array()->lists, addedLists, 
               addedCount * sizeof(array()->lists[0]));
    }
    else if (!list  &&  addedCount == 1) {
        // 0 lists -> 1 list
        list = addedLists[0];
    } 
    else {
        // 1 list -> many lists
        List* oldList = list;
        uint32_t oldCount = oldList ? 1 : 0;
        uint32_t newCount = oldCount + addedCount;
        setArray((array_t *)malloc(array_t::byteSize(newCount)));
        array()->count = newCount;
        if (oldList) array()->lists[addedCount] = oldList;
        memcpy(array()->lists, addedLists, 
               addedCount * sizeof(array()->lists[0]));
    }
}

注意这里的memmove和memcpy操作,它是把原来的方法列表先放到后面,再把新的方法列表放在前面,所以:

  1. 其实category附加完成之后,如果category和原来类都有methodA,那么类的方法列表里面会有两个methodA;
  2. 运行时在查找方法列表时,会按顺序查找,就只会找到category的方法。这就是我们平常说的category的方法会“覆盖”原来类的方法。

到这里,关于category的运行时加载算是结束了。