Skip to content

Instantly share code, notes, and snippets.

@ruandao
Created April 22, 2015 08:14
Show Gist options
  • Save ruandao/acb3482fc97123b37c6e to your computer and use it in GitHub Desktop.
Save ruandao/acb3482fc97123b37c6e to your computer and use it in GitHub Desktop.
swift method swizzle
public override class func initialize() {
struct Static {
static var token: dispatch_once_t = 0
}
if self !== RCChatListViewController.self {
return
}
dispatch_once(&Static.token, { () -> Void in
self.swizze("onSelectedTableRow:", targetFunc: "xxx_onSelectedTableRow:")
})
}
class func swizze(srcFunc:String, targetFunc:String) {
let originalSelector = Selector(srcFunc)
let swizzledSelector = Selector(targetFunc)
let originalMethod = class_getInstanceMethod(self, originalSelector)
let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)
let didAddMethod = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
if didAddMethod {
class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
} else {
method_exchangeImplementations(originalMethod, swizzledMethod)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment