我尝试了 JRSwizzle 和 MethodSwizzle。它们在模拟器上编译得很好,但当我尝试为设备(3.x)编译时抛出一堆错误
有人在 iphone 上有过运气吗?有什么窍门?
TIA
CocoaDev wiki 对方法调配进行了广泛的讨论here。 Mike Ash 在该页面底部有一个相对简单的实现:
#import <objc/runtime.h>
#import <objc/message.h>
//....
void Swizzle(Class c, SEL orig, SEL new)
{
Method origMethod = class_getInstanceMethod(c, orig);
Method newMethod = class_getInstanceMethod(c, new);
if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
else
method_exchangeImplementations(origMethod, newMethod);
}
我还没有对此进行测试,只是因为我认为方法混合是一个极其危险的过程,并且还没有必要使用它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)