oc運行時交換方法

void fs_exchangeMethod(Class cls, SEL old, SEL new)
{
    Method ori_Method = class_getInstanceMethod(cls, old);
    
    Method my_Method = class_getInstanceMethod(cls, new);
    
    BOOL didAddMethod =
    class_addMethod(cls,
                    old,
                    method_getImplementation(my_Method),
                    method_getTypeEncoding(my_Method));

    if (didAddMethod) {
        class_replaceMethod(cls,
                            new,
                            method_getImplementation(ori_Method),
                            method_getTypeEncoding(ori_Method));
    } else {
        method_exchangeImplementations(ori_Method, my_Method);
    }
}
相關文章
相關標籤/搜索