method swizzling
每个类都有自己的方法列表,一个 Method = SEL + IMP + 方法签名,SEL = 方法名称字符串,IMP = 一个函数的起始地址。
method swizzling 就是把 SEL 对应的 IMP 和另一个 SEL 的 IMP 交换。操作要保证唯一性、原子性。在 +load 方法中实现;使用 dispatch_once。因为没有编译器的安全保证,操作一定要仔细。
+load 与 +initialize:
+load 在类初次加载时调用(比main函数还早),initialize 在类内方法被第一次调用前调用。
使用场景比如 统计 view controller 显示次数,直接写在 viewWillAppear 会入侵业务代码,一般会用 category,但是如果有很多类需要统计,就需要很多 category。可以只封装一个工具类。
先介绍一下用 category (下面的代码有一大部分可以简化,只是为了后面做铺垫;只以 viewWillAppear 为例,其他集合类、类方法有逻辑上相同):
// 这个函数在 +load 中调用,且 dispatch_once
+ (void)swizzleUIViewControllerViewWillAppear {
Class systemClass = NSClassFromString(@"UIViewController");
SEL sel_System = NSSelectorFromString(viewWillAppear:);
SEL sel_Custom = @selector(swizzle_viewWillAppear:); // 后面有定义
Method method_System = class_getInstanceMethod(systemClass, sel_System);
Method method_Custom = class_getInstanceMethod([self class], sel_Custom);
IMP imp_System = method_getImplementation(method_System);
IMP imp_Custom = method_getImplementation(method_Custom);
method_exchangeImplementations(method_System, method_Custom);
}
- (void)swizzle_viewWillAppear:(BOOL)animated {
// 这里写统计有关的代码
// 标记1
// 实际调用系统的 viewWillAppear: (已交换地址)
[self swizzle_viewWillAppear:animated];
}
现在考虑用一个工具类。把上述代码复制到 Tool 类中,实际使用中会报错:unrecognized selector。考虑:vc 中调用 viewWillAppear(SEL)时,它的 IMP 实际指向 swizzle_viewWillAppear,因此会进入 Tool 类的这个函数。问题出现了:此时压入栈中的 self 指针是 vc 类的实例指针,vc 类中根本就找不到 swizzle_viewWillAppear 这个 SEL!
解决办法:给 vc 的类加上这个 Method 入口不就行了么。最后一行改为:
// 添加 custom_sel -> system_imp 的 Method
// 如果添加成功,只剩下把 system_sel 的 imp 替换为 custom_imp,实现交换。
if (class_addMethod(systemClass, sel_Custom, imp_System, method_getTypeEncoding(method_System))) {
class_replaceMethod(systemClass, sel_System, imp_Custom, method_getTypeEncoding(method_System));
}
问题又来了,如果添加这个方法失败了呢?也就是上面的代码的 else 部分怎么写?
很多博客里写的是
else {
// 原 vc 类中已存在 custom_sel,直接交换实现:
method_exchangeImplementations(method_System, method_Custom);
}
好,现在假设 vc 中已存在 swizzle_viewWillAppear 这个函数:
vc 执行 viewWillAppear,由于已交换 IMP,代码会运行至 标记1(前面有标注);
现在执行:[self swizzle_viewWillAppear:animated];
此时的 self 指针本质上是 vc,给 vc 发这条消息会走到 vc 类的 swizzle_viewWillAppear,而不是目的地 viewWillAppear。
那么 else 该怎么写?首先不能与其他业务有冲突。直接抛异常,提醒改名为 swizzle_viewWillAppear111。