方法在iPhone设备上进行Swizzle

方法在iPhone设备上进行Swizzle,iphone,objective-c,swizzling,Iphone,Objective C,Swizzling,我尝试了Jrsizzle和Methodsizzle。它们在模拟器上编译得很好,但在我尝试为设备(3.x)编译时会抛出一堆错误 有没有人有幸在iphone上畅饮过?诀窍是什么 TIACocoaDev维基对方法swizzling进行了广泛的讨论。Mike Ash在该页面底部有一个相对简单的实现: #import <objc/runtime.h> #import <objc/message.h> //.... void Swizzle(Class c, SEL orig,

我尝试了Jrsizzle和Methodsizzle。它们在模拟器上编译得很好,但在我尝试为设备(3.x)编译时会抛出一堆错误

有没有人有幸在iphone上畅饮过?诀窍是什么


TIA

CocoaDev维基对方法swizzling进行了广泛的讨论。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);
}
#导入
#进口
//....
无效Swizzle(c类,选择原始,选择新)
{
方法origMethod=class_getInstanceMethod(c,orig);
方法newMethod=class_getInstanceMethod(c,new);
if(class_addMethod(c,orig,method_getImplementation(newMethod),method_getTypeEncoding(newMethod)))
类_replaceMethod(c,new,method_getImplementation(origMethod),method_getTypeEncoding(origMethod));
其他的
方法交换实施(原始方法,新方法);
}

我没有对此进行测试,只是因为我认为方法swizzling是一个极其危险的过程,而且还没有必要使用它。

谢谢你的帮助。他们使用这个的关键是#导入#导入@funkybro-感谢您指出这一点。在它被取下之前,我找到了一个存档版本的页面。当wiki恢复时,我将替换归档链接。+1表示*swizzling是一个极其危险的过程,而且还没有必要使用它。*swizzling在构建单元测试模拟框架时非常有用。+1表示@odyth表示它对单元测试非常有用。我将方法swizzling与OCMock结合起来,对NSURLConnection请求到服务器和来自服务器的响应进行单元验证。Swizzling使我们能够轻松地进行这种类型的验证。