Skip to content

Instantly share code, notes, and snippets.

@OliverLetterer
Forked from a2/gist:1375724
Created July 15, 2012 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OliverLetterer/3115888 to your computer and use it in GitHub Desktop.
Save OliverLetterer/3115888 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