Rodhos Soft

備忘録を兼ねた技術的なメモです。Rofhos SoftではiOSアプリ開発を中心としてAndroid, Webサービス等の開発を承っております。まずはご相談下さい。

メソッド交換

デバッグではまったのでメソッドの交換を試してみた。以下はそのコード。

extension CLLocationManager {
    func my_requestWhenInUseAuthorization() {
        print("requestWhenInUseAuthorization")
    }
    
    func my_requestAlwaysAuthorization() {
        print("requestAlwaysAuthorization")
    }
    
    func switchReq() {
        sw(from:#selector(requestAlwaysAuthorization), to:#selector(my_requestWhenInUseAuthorization))
        sw(from:#selector(requestWhenInUseAuthorization), to:#selector(my_requestWhenInUseAuthorization))
    }
    
    func sw(from:Selector, to:Selector) {
        let method = class_getInstanceMethod(CLLocationManager.self, from)
        let method2 = class_getInstanceMethod(CLLocationManager.self, to)
        method_exchangeImplementations(method, method2)
    }
}