美文网首页
iOS 交换系统类方法

iOS 交换系统类方法

作者: 单抽律化娜 | 来源:发表于2022-07-27 18:55 被阅读0次
    SEL original = @selector(imageNamed:);
    SEL swizzle = @selector(sy_imageNamed:);
    
    /// 需要用objc_getMetaClass,直接用[self class]无效
    Class class = objc_getMetaClass(object_getClassName(self));
    
    Method originalMethod = class_getClassMethod(class, @selector(imageNamed:));
    Method swizzlingMethod = class_getClassMethod(class, @selector(sy_imageNamed:));
    
    BOOL didAddMethod = class_addMethod(class, original, method_getImplementation(swizzlingMethod), method_getTypeEncoding(swizzlingMethod));
    
    if (didAddMethod) {
        class_replaceMethod(class, swizzle, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, swizzlingMethod);
    }

相关文章

网友评论

      本文标题:iOS 交换系统类方法

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