2009-10-28 49 views
24

我尝试了JRSwizzle和MethodSwizzle。他们在模拟器上编译得很好,但是当我尝试为Device(3.x)编译时抛出一堆错误iPhone设备上的Swizzle方法

有没有人在iPhone上运行过任何运气?诀窍是什么?

TIA

回答

54

的CocoaDev维基上的方法混写here了广泛的讨论。迈克水曲柳具有在该页面底部的相对简单的实现:

#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); 
} 

我没有测试过这一点,只是因为我认为方法混写一个极其危险的过程,并没有需要使用它。

+3

谢谢你的工作。他们使用这个密钥是 #import #import dizy 2009-10-28 19:04:56

+0

Wiki链接没有工作 – funkybro 2013-09-18 16:30:58

+0

@funkybro - 感谢您指出。在被取下之前,我挖掘了该页面的存档版本。当wiki恢复时,我会替换存档链接。 – 2013-09-18 16:49:52