/*
memberMethod:是否成员方法
*/
void swizzleMethod(Class class,SEL originalSelector,SEL swizzledSelector,BOOL memberMethod) {
Method originalMethod ;
Method swizzledMethod;
if( memberMethod ) {
originalMethod =class_getInstanceMethod(class, originalSelector);
swizzledMethod =class_getInstanceMethod(class, swizzledSelector);
}else{
class =object_getClass(class);// <------修改
originalMethod =class_getClassMethod(class, originalSelector);
swizzledMethod =class_getClassMethod(class, swizzledSelector);
}
IMP swizzledImp =method_getImplementation(swizzledMethod);
char* swizzledTypes = (char*)method_getTypeEncoding(swizzledMethod);
IMP originalImp =method_getImplementation(originalMethod);
char* originalTypes = (char*)method_getTypeEncoding(originalMethod);
BOOL success =class_addMethod(class, originalSelector, swizzledImp, swizzledTypes);
if(success) {
class_replaceMethod(class, swizzledSelector, originalImp, originalTypes);
}else{
// 添加失败,表明已经有这个方法,直接交换
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}