Skip to content

Instantly share code, notes, and snippets.

@davemess
Created March 26, 2014 17:58
Show Gist options
  • Save davemess/9789383 to your computer and use it in GitHub Desktop.
Save davemess/9789383 to your computer and use it in GitHub Desktop.
+ (void)replaceSelector:(SEL)originalSelector withSelector:(SEL)swizzledSelector forClass:(Class)class
{
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod =
class_addMethod(class,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
@davemess
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment