Method Swizzling

如果我们想要在viewWillApper:的方法里做APM可以使用方法调和

给UIViewController添加一个Categroy

#import "UIViewController+Tracking.h"
#import 
@implementation UIViewController (Tracking)
+(void)load{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class =[self class];
        
        SEL originalSelector = @selector(viewWillAppear:);
        SEL swizzledSelector =@selector(hsk_viewWillAppear:);
        
        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
        
//        //选择器使用原先的,但是函数实现的指针使用现在的。
//        BOOL didAddMethod = class_addMethod(class
//                                            , originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
//        if (didAddMethod) {
//            class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
//        }else{
        
        
            method_exchangeImplementations(originalMethod, swizzledMethod);
        
//        }
        
        
    });
}


#pragma mark---Method Swizzling
//改变一个实例的方法

- (void)hsk_viewWillAppear:(BOOL)animated{
    
    [self hsk_viewWillAppear:animated];
    
    NSLog(@" %@  %@",NSStringFromClass([self class]) ,NSStringFromSelector(_cmd));
}
load 方法只要文件存在于Xcode 的文件管理器中就一定会调用,
而initialize   程序有调用到这个类的方法才会用到
如果在这个类里面重写load,这initialize 就会调用到。

在ViewController里面调用

- (void)viewWillAppear:(BOOL)animated{
    
    //如果有categroy 他回去找Categroy的实现
    //真神奇
    [super viewWillAppear:animated];
    
}

另外想交换NSDictionary 的objectForKey:方法
但是为什么不成功呢? 是因为这个方法带有返回值吗?

#import "NSDictionary+safeObjectForKey.h"
#import 
@implementation NSDictionary (safeObjectForKey)

+ (void)load{
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        SEL originalSelector = @selector(objectForKey:);
        
        SEL swizzledSelector = @selector(safeObjectForKey:);
        
        Class class = [self class];
        
        //拿到实例方法实现的函数指针
        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
        
        BOOL didAddMethod  =class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
        
        if (didAddMethod) {
            
            class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
        }
        else{
            
            method_exchangeImplementations(originalMethod, swizzledMethod);

        }
        
        
    });
    
}

//FIXME:为什么带有返回值得方法使用swizzling 不行呢?

- (id)safeObjectForKey:(NSString *)key{
    
    id  obj = [self safeObjectForKey:key];
    
    if (!obj) {
        
        NSLog(@"key为%@字典对应的值是nil",key);
        
        return nil;
    }
    return obj;
}
@end

源代码下载地址:https://github.com/housenkui/MethodSwizzling

你可能感兴趣的:(Method Swizzling)