Runtime与方法交换

代码:

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self func1];
    [self func2];

    // 方法交换
    Method m1 = class_getInstanceMethod([self class], @selector(func1));
    Method m2 = class_getInstanceMethod([self class], @selector(func2));
    method_exchangeImplementations(m1, m2);
    
    [self func1];
    [self func2];
}

- (void)func1 {
    NSLog(@"func1");
}

- (void)func2 {
    NSLog(@"func2");
}

@end

 输出:

func1
func2
func2
func1

 

posted @ 2016-12-14 10:07  Xwoder  阅读(145)  评论(0编辑  收藏  举报