首先創(chuàng)建兩個(gè)類(lèi) 每一個(gè)類(lèi)里面都創(chuàng)建一個(gè)方法
例如:類(lèi)1
@interface classOne : NSObject
- (void)methodOne;
@end
@implementation classOne - (void)methodOne {
NSLog(@"methodOne");
}
@end
類(lèi)2
@interface classTwo : NSObject
(void)methodTwo;
@end
@implementation classTwo(void)methodTwo {
NSLog(@"methodTwo");
}
@end
交換方法
@implementation ViewController-
(void)viewDidLoad {
[super viewDidLoad];
// NSLog(@"host====%s",KsearchHost);
Method method1 = class_getInstanceMethod([classTwo class], @selector(methodTwo));
Method method2 = class_getInstanceMethod([classOne class], @selector(methodOne));
method_exchangeImplementations(method1, method2);
[[classOne new] methodOne];// Do any additional setup after loading the view, typically from a nib.
}
@end
//輸出結(jié)果是methodTwo 已經(jīng)實(shí)現(xiàn)了方法的交換