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
豪情壮志铁傲骨,原来英雄是孤独。