// // Copyright © 2018年 Zhejiang Dahua Technology Co.,Ltd. All rights reserved. // #import "NSObject+MethodSwizzle.h" #import @implementation NSObject (MethodSwizzle) + (void)swizzleInstanceMethod:(SEL)origSelector withMethod:(SEL)newSelector { Class cls = [self class]; Method originalMethod = class_getInstanceMethod(cls, origSelector); Method swizzledMethod = class_getInstanceMethod(cls, newSelector); BOOL didAddMethod = class_addMethod(cls, newSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)); if (didAddMethod) { method_exchangeImplementations(originalMethod, swizzledMethod); } } + (void)swizzleInstanceMethod:(SEL)origSelector withMethod:(SEL)newSelector withClass:(Class)cls { Method originalMethod = class_getInstanceMethod(cls, origSelector); Method swizzledMethod = class_getInstanceMethod(cls, newSelector); BOOL didAddMethod = class_addMethod(cls, origSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)); if (didAddMethod) { class_replaceMethod(cls, newSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); } else { method_exchangeImplementations(originalMethod, swizzledMethod); } } + (void)swizzleClassMethod:(SEL)origSelector withMethod:(SEL)newSelector { Class cls = [self class]; Method originalMethod = class_getClassMethod(cls, origSelector); Method swizzledMethod = class_getClassMethod(cls, newSelector); BOOL didAddMethod = class_addMethod(cls, newSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)); if (didAddMethod) { method_exchangeImplementations(originalMethod, swizzledMethod); } } + (void)swizzleClassMethod:(SEL)origSelector withMethod:(SEL)newSelector withClass:(Class)cls { Method originalMethod = class_getClassMethod(cls, origSelector); Method swizzledMethod = class_getClassMethod(cls, newSelector); BOOL didAddMethod = class_addMethod(cls, origSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)); if (didAddMethod) { class_replaceMethod(cls, newSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); } else { method_exchangeImplementations(originalMethod, swizzledMethod); } } @end