iOS 交换系统类方法

    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 交换系统类方法)