runtime之method_exchangeImplementations交换两个方法

#import "UIViewController+Swizzling.h"
#import <objc/runtime.h>
@implementation UIViewController (Swizzling)

+(void)load{
    //虽然load只执行一次,但是为了保险起见,我们还是给加个dispatch_once吧,良好的编程习惯,从这里开始
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        
        SEL orginSel = @selector(viewWillAppear:);
        SEL overrideSel = @selector(overrideViewWillAppear:);
        
        Method originMethod = class_getInstanceMethod([self class], orginSel);
        Method overrideMethod = class_getInstanceMethod([self class], overrideSel);
        
        //原来的类没有实现指定的方法,那么我们就得先做判断,把方法添加进去,然后进行替换
        if (class_addMethod([self class], orginSel, method_getImplementation(overrideMethod) , method_getTypeEncoding(originMethod))) {
            class_replaceMethod([self class],
                                overrideSel,
                                method_getImplementation(originMethod),
                                method_getTypeEncoding(originMethod));
            
        }else{
            //交换实现
            method_exchangeImplementations(originMethod, overrideMethod);
        }
    });
}

 - (void)overrideViewWillAppear:(BOOL)animation{
    NSLog(@"%@-----overrideViewWillAppear",self);
    //这里并不会造成死循环,因为这个时候是去调用原来的ViewWillAppear:(BOOL)animation方法了。
    [self overrideViewWillAppear:animation];
}
@end

1.以上是角交换两个方法可以做app全埋点

#import "NSMutableDictionary+Swizzling.h"
#import <objc/runtime.h>

@implementation NSMutableDictionary (Swizzling)

+ (void)load
{
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        
        SEL orginSel = @selector(setObject:forKey:);
        SEL overrideSel = @selector(overrideSetObject:forKey:);
        
         Class o_class = objc_getClass("__NSDictionaryM");
        Method originMethod = class_getInstanceMethod(o_class, orginSel);
        Method overrideMethod = class_getInstanceMethod(o_class, overrideSel);
        
        //原来的类没有实现指定的方法,那么我们就得先做判断,把方法添加进去,然后进行替换
        if (class_addMethod(o_class, orginSel, method_getImplementation(overrideMethod) , method_getTypeEncoding(originMethod))) {
            class_replaceMethod(o_class,
                                overrideSel,
                                method_getImplementation(originMethod),
                                method_getTypeEncoding(originMethod));
            
        }else{
            //交换实现
            method_exchangeImplementations(originMethod, overrideMethod);
        }
    });
}



- (void)overrideSetObject:(id)anObject forKey:(id <NSCopying>)aKey;
{
    if (anObject) {
        NSLog(@"%@--overrideSetObject",self);
        /** 注意:必须调用自己的方法名 */
        [self overrideSetObject:anObject forKey:aKey];
    }
}
@end

/**
类                               “真身”

NSArray                       __NSArrayI

NSMutableArray                __NSArrayM

NSDictionary                  __NSDictionaryI

NSMutableDictionary           __NSDictionaryM
*/

 

2.以上另外一个用法,兼容字典传空;同理NSArray也可以这么写一个,需要注意的是类的真身是什么,要针对性的写

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值