Skip to content

Instantly share code, notes, and snippets.

@jamztang
Created May 2, 2012 02:06
Show Gist options
  • Save jamztang/2573050 to your computer and use it in GitHub Desktop.
Save jamztang/2573050 to your computer and use it in GitHub Desktop.
Handly method swizzling method
// Original Reference: http://stackoverflow.com/questions/1637604/method-swizzle-on-iphone-device
#import <objc/runtime.h>
#import <objc/message.h>
//....
void Swizzle(Class c, SEL orig, SEL new)
{
Method origMethod = class_getInstanceMethod(c, orig);
Method newMethod = class_getInstanceMethod(c, new);
if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
class_replaceMethod(c, new, 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