『ios』我所知道的category分类底层实现
2021-06-30 本文已影响0人
butterflyer
温故而知新,可以为师矣。
你学到的东西,最终可能需要你写出来,才能表示你掌握。
category我们用的很多。

我们来看下category的底层结构
//声明结构体
struct _category_t {
const char *name; //类的名字(name)
struct _class_t *cls; //类(cls)
const struct _method_list_t *instance_methods;//category中所有给类添加的实例方法的列表(instanceMethods)
const struct _method_list_t *class_methods;//category中所有添加的类方法的列表(classMethods)
const struct _protocol_list_t *protocols; //category实现的所有协议的列表(protocols)
const struct _prop_list_t *properties; //category中添加的所有属性(instanceProperties)
};
//对结构体赋值
static struct _category_t _OBJC_$_CATEGORY_NSObject_$_Test __attribute__ ((used, section ("__DATA,__objc_const"))) =
{
"NSObject",
0, // &OBJC_CLASS_$_NSObject,
(const struct _method_list_t *)&_OBJC_$_CATEGORY_INSTANCE_METHODS_NSObject_$_Test,
(const struct _method_list_t *)&_OBJC_$_CATEGORY_CLASS_METHODS_NSObject_$_Test,
(const struct _protocol_list_t *)&_OBJC_CATEGORY_PROTOCOLS_$_NSObject_$_Test,
0,
};
然后我们可以看apple的源码,查找方法的顺序如下。
objc-os.mm
_objc_init
map_images
map_images_nolock
objc-runtime-new.mm
_read_images
remethodizeClass
attachCategories
attachLists
realloc、memmove、 memcpy
对于attachCategories方法的内部实现
// cls 类
// 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_t,method_t]
[method_t,method_t]
]
*/
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(); //拿到类的class_rw_t
prepareMethodLists(cls, mlists, mcount, NO, fromBundle);
rw->methods.attachLists(mlists, mcount); //把方法通过attachLists方法放到rw的方法中
free(mlists);
if (flush_caches && mcount > 0) flushCaches(cls);
rw->properties.attachLists(proplists, propcount);
free(proplists);
rw->protocols.attachLists(protolists, protocount);
free(protolists);
}
然后我们再来分析一下attachLists这个方法
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;
//array()->lists:原来类对象的方法列表
//内存移动
memmove(array()->lists + addedCount, array()->lists,
oldCount * sizeof(array()->lists[0]));
//addedLists:所有分类的方法列表
//内存拷贝
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这两个方法。
memmove(array()->lists + addedCount, array()->lists,
oldCount * sizeof(array()->lists[0]));
memmove这个方法,是把之前类的实例方法或者是类方法的内存地址向右移几个单位,这个addedCount的数量就是你分类的数量。
memcpy(array()->lists, addedLists,
addedCount * sizeof(array()->lists[0]));
memcpy这个方法是把你要添加的方法拷贝到一个地址去。
我们从这里就可以看出来,为什么我们调用同样一个在类中存在和分类中存在的方法,会先执行分类中的方法。因为分类中的方法被添加到了前面。
这里需要特别注意啊。分类中的方法不是把本类中的方法覆盖了,只是添加到了他的前面。而category的加载顺序跟编译顺序有关,后编译的方法在前面,这样我们通过objc_msgsend就会先找到分类的方法。