【发布时间】:2009-10-28 14:28:20
【问题描述】:
我尝试了 JRSwizzle 和 MethodSwizzle。它们在模拟器上编译得很好,但是当我尝试为 Device (3.x) 编译时抛出一堆错误
有没有人在 iphone 上玩过运气?有什么诀窍?
TIA
【问题讨论】:
标签: iphone objective-c swizzling
我尝试了 JRSwizzle 和 MethodSwizzle。它们在模拟器上编译得很好,但是当我尝试为 Device (3.x) 编译时抛出一堆错误
有没有人在 iphone 上玩过运气?有什么诀窍?
TIA
【问题讨论】:
标签: iphone objective-c swizzling
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);
}
我没有对此进行测试,只是因为我认为方法调配是一个极其危险的过程,并且还没有使用它的必要。
【讨论】: