Created
November 18, 2011 05:53
-
-
Save a2/1375724 to your computer and use it in GitHub Desktop.
Swizzle
This file contains hidden or 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
// Don't forget to #import <objc/runtime.h> | |
void A2Swizzle(Class cls, SEL oldSel, SEL newSel, BOOL isClassMethod) | |
{ | |
if (isClassMethod) cls = object_getClass(cls); | |
Method origMethod = class_getInstanceMethod(cls, oldSel); | |
Method newMethod = class_getInstanceMethod(cls, newSel); | |
if (class_addMethod(cls, oldSel, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))) | |
class_replaceMethod(cls, newSel, method_getImplementation(origMethod), method_getTypeEncoding(origMethod)); | |
else | |
method_exchangeImplementations(origMethod, newMethod); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment