Method Swizzing中一般替换方法都写在Category类别里吗?有没有别的实现方式

新手上路,请多包涵

Method Swizzing中一般替换方法都写在Category类别里吗?有没有别的实现方式

阅读 2.9k
1 个回答

不一定非写分类,你调下面的方法进行方法交换,位置不随便写吗?

void methodSwizzle(Class c, SEL orig, SEL newS )
{
    Method origMethod = class_getInstanceMethod(c, orig);
    Method newMethod = class_getInstanceMethod(c, newS);
    
    BOOL addSuccess = class_addMethod(c, orig, method_getImplementation(newMethod),method_getTypeEncoding(newMethod) );
    
    if (addSuccess) {
        class_replaceMethod(c, newS, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
        
    }else{
        method_exchangeImplementations(origMethod, newMethod);
        
    }
    
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题