Skip to content

Instantly share code, notes, and snippets.

@Koze
Created April 8, 2015 15:01
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 Koze/cff0c78819b30a6393fa to your computer and use it in GitHub Desktop.
Save Koze/cff0c78819b30a6393fa to your computer and use it in GitHub Desktop.
Example for method_exchangeImplementations
#import "ViewController.h"
#import <objc/runtime.h>
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Class cls = self.class;
Method m1 = class_getInstanceMethod(cls, @selector(methodA));
Method m2 = class_getInstanceMethod(cls, @selector(methodB));
method_exchangeImplementations(m1, m2);
[self methodA];
// methodB called
}
- (void)methodA
{
NSLog(@"methodA called");
}
- (void)methodB
{
NSLog(@"methodB called");
}
@end
#import "ViewController.h"
#import <objc/runtime.h>
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Class cls = self.class;
Method m1 = class_getInstanceMethod(cls, @selector(methodA));
Method m2 = class_getInstanceMethod(cls, @selector(methodB));
method_exchangeImplementations(m1, m2);
[self methodA];
// methodB called
// methodA called
}
- (void)methodA
{
NSLog(@"methodA called");
}
- (void)methodB
{
NSLog(@"methodB called");
[self methodB];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment