Skip to content

Instantly share code, notes, and snippets.

@a2
Created November 18, 2011 05:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save a2/1375724 to your computer and use it in GitHub Desktop.
Save a2/1375724 to your computer and use it in GitHub Desktop.
Swizzle
// 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