A-Hing
11/14/2019 - 3:20 AM

runtime:方法交换:不用的两个类

void ISSwizzleInstanceMethod(Class class,Class alternativeClass, SEL originalSelector, SEL alternativeSelector)
{
    SEL sel_System = originalSelector;
    SEL sel_Custom = alternativeSelector;

    Method method_System = class_getInstanceMethod(class, sel_System);
    Method method_Custom = class_getInstanceMethod(alternativeClass, sel_Custom);

    IMP imp_System = method_getImplementation(method_System);
    IMP imp_Custom = method_getImplementation(method_Custom);

    if (class_addMethod(class, sel_Custom, imp_System, method_getTypeEncoding(method_System))) {
        class_replaceMethod(class, sel_System, imp_Custom, method_getTypeEncoding(method_System));
    } else {
        method_exchangeImplementations(method_System, method_Custom);
    }
}