使用runtime替換類的方法
一些系統(tǒng)類或者第三方庫的類方法無法修改原文件中的方法,可以通過添加分類颅湘,method_exchangeImplementations替換方法實現(xiàn)對原方法的修改
#import "NSObject+Repail.h"
#import <objc/runtime.h>
#import "LivefaceViewController.h"
@implementation LivefaceViewController (Repail)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method m1 = class_getInstanceMethod([self class], NSSelectorFromString(@"setCallBackQueue:"));
Method m2 = class_getInstanceMethod([self class], NSSelectorFromString(@"__t_setCallBackQueue:"));
method_exchangeImplementations(m1, m2);
});
}
- (void)__t_setCallBackQueue:(dispatch_queue_t)callBackQueue {
[self performSelector:@selector(__t_setCallBackQueue:) withObject:dispatch_get_main_queue() afterDelay:0];
}
@end