一、分类的用途
项目中的应用
1.分解体积大的类文件,按功能区分
2.声明私有方法
3.framework库中的方法公开
二、分类可以实现的内容
1.扩展类方法
2.扩展对象方法
3.添加协议
4.添加属性(添加getter、setter,不能添加成员变量)
思考:分类与类的关系,分类中为什么不能添加成员变量?
三、分类的底层结构
添加的分类会经过两个阶段
1.编译阶段:分类是编译器特性,在编译的时候,会将分类变成category_t结构体,所有的方法,属性,协议等都存放在这里
2.运行时阶段:当程序运行的时候,通过runtime,将category_t中的内容合并到类中
//编译阶段,将分类编译 成category_t类型的数据
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);
};
四、合并过程
合并过程发成在运行时阶段,app运行的时候首先执行的方法并不是main函数,在此之前dyld(动态链接器,操作系统级别的组件)会调用_objc_init()方法,其中进行runtime的初始化
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();
// images 是镜像、模块
// 注册关于加载images的通知回调,参数都是方法
// map_images OC image被加载映射到内存 (包含category合并过程)
// load_images OC 加载镜像、模块的时候调用+load()方法在此时被调用
// unmap_image OC image被移除内存时
_dyld_objc_notify_register(&map_images, load_images, unmap_image);
}
map_images 内部会执行合并过程
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);
}
继续调用
void
map_images_nolock(unsigned mhCount, const char * const mhPaths[],
const struct mach_header * const mhdrs[])
{
......
// 内部会开始读取内容,类信息、分类信息等
// totalClasses 项目中所有的类
if (hCount > 0) {
_read_images(hList, hCount, totalClasses, unoptimizedTotalClasses);
}
......
}
_read_images内部搜索类、属性、协议、分类信息
void _read_images(header_info **hList, uint32_t hCount, int totalClasses, int unoptimizedTotalClasses)
{
......
// 获取所有的类
for (EACH_HEADER) {
classref_t *classlist = _getObjc2ClassList(hi, &count);
.....
}
//获取协议
......
// 获取分类
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
Class cls = remapClass(cat->cls);
......
// remethodizeClass方法中重新组织类
if (cat->classMethods || cat->protocols
|| (hasClassProperties && cat->_classProperties))
{
addUnattachedCategoryForClass(cat, cls->ISA(), hi);
if (cls->ISA()->isRealized()) {
remethodizeClass(cls->ISA());//重新组织类
}
......
}
remethodizeClass 这个方法中会将类和分类信息合并再一起
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*/))) {
if (PrintConnecting) {
_objc_inform("CLASS: attaching categories to class '%s' %s",
cls->nameForLogging(), isMeta ? "(meta)" : "");
}
//合并
// cls 类对象或者是元类对象
// cats 分类列表
attachCategories(cls, cats, true /*flush caches*/);
free(cats);
}
}
//attachCategories 方法
// cls 类对象或者是元类对象
// cats 分类列表 [category_t,category_t,category_t,...]
static void
attachCategories(Class cls, category_list *cats, bool flush_caches)
{
if (!cats) return;
if (PrintReplacedMethods) printReplacements(cls, cats);
bool isMeta = cls->isMetaClass();
// 分配内存空间
// 方法列表
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));
int mcount = 0;
int propcount = 0;
int protocount = 0;
int i = cats->count;
bool fromBundle = NO;
// 循环 i-- 最后参与编译的分类放到第一位依次类推
while (i--) {
// 获取某一个分类
auto& entry = cats->list[i];
// 取出方法列表---一维数组,如果是元类去对象方法,如果不是,取实例方法
method_list_t *mlist = entry.cat->methodsForMeta(isMeta);
if (mlist) {
// method_list_t **mlists 是总的方法列表二维数组[[method_t,method_t],[method_t,method_t]]
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 开始附加 方法、属性、协议
// class_rw_t 是对象中可读可写的
// 调用的data()方法中是将bits &一个掩码,获取rw结构体的地址
prepareMethodLists(cls, mlists, mcount, NO, fromBundle);
// mlist 是所有分类中的方法 一维数组
// rw->methods 取出rw中的方法列表,然后附加所有分类的方法列表mlists
rw->methods.attachLists(mlists, mcount);
free(mlists);
if (flush_caches && mcount > 0) flushCaches(cls);
// rw-> properties.取出所有的属性
// 然后附加分类中的属性
rw->properties.attachLists(proplists, propcount);
free(proplists);
// rw->protocols取出所有的协议,然后附加分类中的协议
rw->protocols.attachLists(protolists, protocount);
free(protolists);
}
attachLists 方法是将类中的属性列表、协议列表、方法列表和分类中的属性、协议、方法进行合并为 一个列表存放到rw中
以方法列表为例
// addedList 是外面传入的mlist addedCount 是传入的mlist的元素个数
void attachLists(List* const * addedLists, uint32_t addedCount) {
//mlist 是所有分类中的方法 一维数组
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]));
// 分类中的方法地址,移动到前面
// addedLists 的顺序是后编译的再最前面
// 编译顺序决定了,分类中的方法列表中的顺序,最后编译的会放到最前面
// 分类中的方法永远都覆盖 原有类的方法,优先调用分类中的方法
// 将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]));
}
}
分类的合并是发生在运行时初始化的时候,进行的。编译的顺序决定了,分类在合并中的位置,即优先调用后面编译的分类中的内容
五、类扩展Extension
1.类扩展,存放在.m文件中,他在程序编译的时候,就已经合并到类中了
2.类扩展声明的都是私有的属性和方法
3.类扩展增加成员变量,声明属性会自动实现getter、setter方法
六、总结
1.分类中有属性、方法、协议,没有成员变量
2.分类的数据合并发生在运行时初始化的时候
3.后编译的分类数据存放在最前面,优先调用
4.类扩展在编译阶段 就已经合并到类中了,并且属性和方法都是私有的
网友评论