我有一个类设置,理想情况下将读取传入的任何类的方法,然后在运行时将它们全部映射到单个选择器上,然后再将它们转发到原始选择器。
这确实有效,但我一次只能使用一种方法。问题似乎是,一旦我调配了第一种方法,我用来捕获和转发该方法的 IMP 现在已经与其他方法 IMP 交换了。任何进一步的尝试都会失败,因为他们使用新交换的 IMP 来替换其他的。
1) 所以我有 MethodA、MethodB 和 CustomCatchAllMethod。
2)我用 CustomCatchAllMEthod 交换 MethodA。MethodA->CustomCatchAllMethod、CustomCatchAllMethod->MethodA
3)现在我也尝试使用CustomCatchAllMethod 交换到MethodB,但是由于CustomCatchAllMethod 现在= MethodA,MethodB 变为MethodA 和MethodA->MethodB。
那么,如何为每个要拦截的新选择器获取/复制 IMP 的新实例?
这是上述流程的粗略模型:
void swizzle(Class classImCopying, SEL orig){
SEL new = @selector(catchAll:);
Method origMethod = class_getInstanceMethod(classImCopying, orig);
Method newMethod = class_getInstanceMethod(catchAllClass,new);
method_exchangeImplementations(origMethod, newMethod);
}
//In some method elsewhere
//I want this to make both methodA and methodB point to catchAll:
swizzle(someClass, @selector(methodA:));
swizzle(someClass, @selector(methodB:));