如何避免分类覆盖宿主类的同名方法

#import 

@interface NSObject (Utilities)

+ (void)overrideCategorySelector:(SEL)sel isMeta:(BOOL)isMeta;

@end
#import "NSObject+Utilities.h"
#import 

@implementation NSObject (Utilities)

+ (void)overrideCategorySelector:(SEL)sel isMeta:(BOOL)isMeta{
    unsigned int count;
    Class cClass = [self class];
    if (isMeta) {
        cClass = objc_getMetaClass(class_getName([self class]));
    }
    Method *list = class_copyMethodList(cClass, &count);
    while (count > 0) {
        unsigned int i = --count;
        Method m = list[i];
        if (method_getName(m) == sel) {
            IMP imp = method_getImplementation(m);
            method_setImplementation(class_getInstanceMethod(cClass, sel), imp);
            break;
        }
    }
    free(list);
}

@end

详细解释后续补充!

你可能感兴趣的:(如何避免分类覆盖宿主类的同名方法)