Skip to content

Instantly share code, notes, and snippets.

@cruinh
Created August 8, 2013 17:09
Show Gist options
  • Save cruinh/6186568 to your computer and use it in GitHub Desktop.
Save cruinh/6186568 to your computer and use it in GitHub Desktop.
example of using apple's method_exchangeImplementations from objective-c runtime
- (void)atest1
{
NSLog(@"this is test 1");
}
- (void)atest2
{
NSLog(@"this is test 2");
[self atest2];
}
- (void)testExchange
{
Method originalMethod = class_getInstanceMethod([self class], @selector(atest1));
Method categoryMethod = class_getInstanceMethod([self class], @selector(atest2));
method_exchangeImplementations(originalMethod, categoryMethod);
[self atest1];
NSLog(@"The log should show \"this is test 2\" and then \"this is test 1\"");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment