美文网首页
使用runtime替换类的方法

使用runtime替换类的方法

作者: William__Lu | 来源:发表于2018-08-31 15:07 被阅读0次
    使用runtime替换类的方法
    一些系统类或者第三方库的类方法无法修改原文件中的方法,可以通过添加分类,method_exchangeImplementations替换方法实现对原方法的修改
    #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
    

    相关文章

      网友评论

          本文标题:使用runtime替换类的方法

          本文链接:https://www.haomeiwen.com/subject/cggswftx.html