method swizzle

2020-11-27  本文已影响0人  今年27
+(void)load{
    NSError* error = nil;
    [self jr_swizzleMethod:@selector(kde_willDealloc) withMethod:@selector(willDealloc) error:&error];
}

-(BOOL)kde_willDealloc{
   return NO;
}

+ (BOOL)jr_swizzleMethod:(SEL)origSel_ withMethod:(SEL)altSel_ error:(NSError**)error_ {

    Method origMethod = class_getInstanceMethod(self, origSel_);
    if (!origMethod) {
        return NO;
    }

    Method altMethod = class_getInstanceMethod(self, altSel_);
    if (!altMethod) {
        return NO;
    }

    class_addMethod(self,
                    origSel_,
                    class_getMethodImplementation(self, origSel_),
                    method_getTypeEncoding(origMethod));

    class_addMethod(self,
                    altSel_,
                    class_getMethodImplementation(self, altSel_),
                    method_getTypeEncoding(altMethod));

    method_exchangeImplementations(class_getInstanceMethod(self, origSel_), class_getInstanceMethod(self, altSel_));
    return YES;
}
上一篇下一篇

猜你喜欢

热点阅读