首先创建两个类 每一个类里面都创建一个方法
例如:类1
@interface classOne : NSObject
- (void)methodOne;
@end
@implementation classOne - (void)methodOne {
NSLog(@"methodOne");
}
@end
类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
//输出结果是methodTwo 已经实现了方法的交换
网友评论