开发者

Method Swizzle on iPhone device

开发者 https://www.devze.com 2022-12-09 12:30 出处:网络
I tried both JRSwizzle, and MethodSwizzle.They compile f开发者_运维知识库ine on the simulator but throw a bunch of errors when I try to compile for Device (3.x)

I tried both JRSwizzle, and MethodSwizzle. They compile f开发者_运维知识库ine on the simulator but throw a bunch of errors when I try to compile for Device (3.x)

Has anyone had any luck swizzling on the iphone? Whats the trick?

TIA


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消