Created
April 8, 2015 15:01
-
-
Save Koze/cff0c78819b30a6393fa to your computer and use it in GitHub Desktop.
Example for method_exchangeImplementations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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