iPhoneデバイスでのSwizzleの方法 質問する

iPhoneデバイスでのSwizzleの方法 質問する

JRSwizzleとMethodSwizzleの両方を試しました。シミュレータでは問題なくコンパイルされますが、デバイス(3.x)用にコンパイルしようとすると、大量のエラーが発生します。

iPhone でスウィズリングに成功した人はいますか? コツは何ですか?

ティア

ベストアンサー1

CocoaDev wikiにはメソッドスウィズリングに関する詳細な議論がある。ここ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);
}

私はメソッド スウィズリングを非常に危険なプロセスと見なしており、まだ使用する必要がなかったため、これをテストしていません。

おすすめ記事