The Pragmatic Ball boy

iOSを中心にやってる万年球拾いの老害エンジニアメモ

Objective-Cのクラスメソッドの差し替え

テストのときにUIAlertViewの表示を消してくれるライブラリで
どうやってUIAlertViewのメソッドをフックしてるのかと調べてみたところ
method_exchangeImplementations()を使って実装されてた。



+ (void)swapInstanceMethodsForClass: (Class) cls selector: (SEL)sel1 andSelector: (SEL)sel2 {


    Method method1 = class_getInstanceMethod(cls, sel1);


    Method method2 = class_getInstanceMethod(cls, sel2);


    method_exchangeImplementations(method1, method2);


}









method_exchangeImplementations()を使えばこのようにクラスメソッドを差し替えられるのでテストでは便利。