Created
April 22, 2015 08:14
-
-
Save ruandao/acb3482fc97123b37c6e to your computer and use it in GitHub Desktop.
swift method swizzle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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