事由:以前去面试,而后面试官问了我一些关于
runtime
的用法,我有说到Method Swizzling
。经过在category
的load
中去修改咱们调用的方法,来达到全局修改的目的。随后面试官问到关于category
的实现,哇! 尴尬,我好像历来没有想过这个问题。如今有时间就给整理一下,水平有限,确定会有不少不足。但愿你们多多指点!多谢 zzz面试
category
是Objective-C 2.0
以后添加的语言特性. 通常咱们使用它有如下两种场景设计模式
组合
的设计模式把类的实现分开成多个category
在几个不一样的文件里面
category
里category
,达到业务分离关于问题(由于确实不知道该从什么地方开始看起,因此强迫试的给本身定了几个问题。让本身去弄明白)数组
category
是什么东西category
是怎样加载的category
方法为何能够覆盖宿主类的方法category
的属性跟方法是怎么添加到宿主类的category
为何能够添加属性,方法,协议。缺不能添加成员变量category
是什么东西objc
全部类和对象都是c
结构体,category
也同样。咱们能够经过clang
去看一下安全
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;
};
复制代码
_category_t
里面有名字、宿主类的对象、实例方法列表、类方法列表、协议方法列表、属列表性。能够看到是没有成员变量列表的,由于category
是依赖runtime
的,而在运行时对象的内存布局已经肯定,若是添加实例变量就会破坏类的内部布局,这对编译型语言来讲是灾难性的。这就是为何咱们没有在_category_t
里面找到成员变量列表和category
不能够添加成员变量的缘由bash
category
是怎样加载的上面咱们提到过category
是依赖runtime
的。那咱们来看一下runtime
的加载过程。下面用到的runtime
源码都来自于点这! 我下载的是723
最新的版本app
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);
}
复制代码
开始是一些初始化方法函数
map_images
方法表示将文件中的二进制文件映射到内存,category被添加到宿主类就发生在这个方法里面。咱们来看一下这个方法实现布局
void
map_images(unsigned count, const char * const paths[],
const struct mach_header * const mhdrs[])
{
// 加锁操做 保证在映射到dyld过程调用ABI是安全的
rwlock_writer_t lock(runtimeLock);
//函数在加锁后就转向了 map_images_nolock 函数
return map_images_nolock(count, paths, mhdrs);
}
复制代码
map_images_nolock
方法代码太长,我就不粘过来了。它主要作的操做是在函数中,它检查传入的每一个image
,若是image
有须要的信息,就将它记录在hList
中,并将hCount
加一,最终判断hCount>0
来调用_read_images
读取 image 中的数据 。学习
咱们再来看_read_images
,方法有点长。我给跟category
相关代码截取出来了ui
for (EACH_HEADER) {
// 获取 镜像中的全部分类
category_t **catlist =
_getObjc2CategoryList(hi, &count);
bool hasClassProperties = hi->info()->hasCategoryClassProperties();
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; if (PrintConnecting) { _objc_inform("CLASS: IGNORING category \?\?\?(%s) %p with " "missing weak-linked target class", cat->name, cat); } continue; } // 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;
// 从这开始,正式对category开始处理
if (cat->instanceMethods || cat->protocols
|| cat->instanceProperties)
{
//为类添加未依附的分类,把Category和类关联起来
addUnattachedCategoryForClass(cat, cls, hi);
if (cls->isRealized()) {
remethodizeClass(cls);
classExists = YES;
}
if (PrintConnecting) {
_objc_inform("CLASS: found category -%s(%s) %s",
cls->nameForLogging(), cat->name,
classExists ? "on existing class" : "");
}
}
if (cat->classMethods || cat->protocols
|| (hasClassProperties && cat->_classProperties))
{
//为类添加未依附的分类,把Category和metaClass关联起来。由于类方法是存在元类中的
addUnattachedCategoryForClass(cat, cls->ISA(), hi);
if (cls->ISA()->isRealized()) {
remethodizeClass(cls->ISA());
}
if (PrintConnecting) {
_objc_inform("CLASS: found category +%s(%s)",
cls->nameForLogging(), cat->name);
}
}
}
}
复制代码
哇! 终于 终于 到了最关键的方法了
首先。咱们调用remethodizeClass
来调用category
的幕后大佬----attachCategories
方法,从名字就能够看出来它的做用,添加category
plz show me the code !!!
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
/*
#warning attachCategories
category的加载顺序是经过编译顺序决定的
这样倒序遍历,保证先将数组内元素(category)的方法从后往前添加到新数组
这样编译在后面的category方法会在数组的前面
*/
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);
}
复制代码
获得从新组合的数组在调用
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;
//C 库函数 void *realloc(void *ptr, size_t size) 尝试从新调整以前调用 malloc 或 calloc 所分配的 ptr 所指向的内存块的大小
setArray((array_t *)realloc(array(), array_t::byteSize(newCount)));
array()->count = newCount;
/*
1.memmove
函数原型:void *memmove(void *dest, const void *source, size_t count)
返回值说明:返回指向dest的void *指针
参数说明:dest,source分别为目标串和源串的首地址。count为要移动的字符的个数
函数说明:memmove用于从source拷贝count个字符到dest,若是目标区域和源区域有重叠的话,memmove可以保证源串在被覆盖以前将重叠区域的字节拷贝到目标区域中。
2.memcpy
函数原型:void *memcpy(void *dest, const void *source, size_t count);
返回值说明:返回指向dest的void *指针
函数说明:memcpy功能和memmove相同,可是memcpy中dest和source中的区域不能重叠,不然会出现未知结果。
3.二者区别
函数memcpy() 从source 指向的区域向dest指向的区域复制count个字符,若是两数组重叠,不定义该函数的行为。
而memmove(),若是两函数重叠,赋值仍正确进行。
memcpy函数假设要复制的内存区域不存在重叠,若是你能确保你进行复制操做的的内存区域没有任何重叠,能够直接用memcpy;
若是你不能保证是否有重叠,为了确保复制的正确性,你必须用memmove。
*/
/*
这样就完成将category方法列表里面的方法 加到 class的方法列表里面并且是前面。等到咱们再去调用class的方法时候,咱们经过去遍历class的方法列表去查到SEL,找到就会调用相应方法。因为category的方法在前面---致使因此会覆盖宿主类原本的方法(这就是为何category方法的优先级高于宿主类方法)
属性和协议同理!!!!!!!!!!!
*/
//至关于给array()->lists 从新放在起始地址 = array()->lists的起始地址 + addedCount
memmove(array()->lists + addedCount, array()->lists,
oldCount * sizeof(array()->lists[0]));
//至关于给addedLists 这个category新数组加到array()->的起始地址
memcpy(array()->lists, addedLists,
addedCount * sizeof(array()->lists[0]));
}
复制代码
以上应该能够回答,以前提的全部问题。若有疑问,能够联系我。
笔者是一个刚入门iOSer
此次关于category
的管中窥豹,必定有不少的不足,但愿你们不吝赐教!
有任何问题能够留言,或者直接联系QQ:346658618
但愿能够相互学习,一块儿进步!