方法在iPhone设备上Swizzle [英] Method Swizzle on iPhone device
问题描述
我尝试了JRSwizzle和MethodSwizzle。他们在模拟器上编译得很好,但是当我尝试编译Device(3.x)时会抛出一堆错误
I tried both JRSwizzle, and MethodSwizzle. They compile fine on the simulator but throw a bunch of errors when I try to compile for Device (3.x)
有没有人在iPhone上乱跑?什么是诀窍?
Has anyone had any luck swizzling on the iphone? Whats the trick?
TIA
推荐答案
CocoaDev wiki有一个有关方法调配的广泛讨论此处。 Mike Ash在该页面的底部有一个相对简单的实现:
The CocoaDev wiki has an extensive discussion on method swizzling here. Mike Ash has a relatively simple implementation at the bottom of that page:
#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);
}
我没有对此进行测试,仅仅是因为我认为方法调整是极其危险的过程并且还没有必要使用它。
I have not tested this, simply because I regard method swizzling as an extremely dangerous process and haven't had the need to use it yet.
这篇关于方法在iPhone设备上Swizzle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!