//
//  NSObject+DDCategory.m
//  DDCategoryKit_Private
//  Created by DDIsFriend on 2022/7/29.

#import "NSObject+DDCategory.h"
#import <objc/runtime.h>

@implementation NSObject (DDCategory)
+ (void)dd_class:(Class)class originSelector:(SEL)originSelector swizzleSelector:(SEL)swizzleSelector{
    
    Method originMethod = class_getInstanceMethod(class, originSelector);
    Method swizzleMethod = class_getInstanceMethod(class, swizzleSelector);
    BOOL success = class_addMethod(class, originSelector, method_getImplementation(swizzleMethod), method_getTypeEncoding(swizzleMethod));
    if (success){
        class_replaceMethod(class, swizzleSelector, method_getImplementation(originMethod), method_getTypeEncoding(originMethod));
    }else{
        method_exchangeImplementations(originMethod, swizzleMethod);
    }
}

+ (void)dd_originSelector:(SEL)originSelector swizzleSelector:(SEL)swizzleSelector{
    Class class = [self class];
    Method originMethod = class_getInstanceMethod(class, originSelector);
    Method swizzleMethod = class_getInstanceMethod(class, swizzleSelector);
    BOOL success = class_addMethod(class, originSelector, method_getImplementation(swizzleMethod), method_getTypeEncoding(swizzleMethod));
    if (success){
        class_replaceMethod(class, swizzleSelector, method_getImplementation(originMethod), method_getTypeEncoding(originMethod));
    }else{
        method_exchangeImplementations(originMethod, swizzleMethod);
    }
}
@end
