使用案例: 当需要在控制器销毁时,打印控制器的相关信息,需要在deallc 中写NSLog(@"...."); 如果控制器非常多的时候,在每个控制器中都重写dealloc方法就显得很蛋疼
所以这时就需要runtime的一个方法: method_exchangeImplementations(Method m1, Method m2)
建一个UIViewController的类扩展:UIViewController+Extension
在这里,可以在下列两个方法
+ (void)load;
+ (void)initialize;
任意一个方法中添加下面的代码
Method method1 = class_getInstanceMethod([XMGPerson class], @selector(dealloc));
Method method2 = class_getInstanceMethod([XMGPerson class], @selector(myDealloc));
method_exchangeImplementations(method1, method2);
但出现了这种错误,这是因为dealloc方法比较特殊: