2009-10-28 77 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