RunTime 之Method Swizzling

2018-03-07  本文已影响0人  進无尽

前言

有关Runtime的知识总结,我本来想集中写成一篇文章的,但是最后发现实在是太长,而且不利于阅读,最后分成了如下几篇:


isa swizzling 的应用

KVO

Method Swizzling API 说明

Method Swizzling技术它能够让我们在运行时替换已有方法来实现我们的一些需求。我们都知道方法由两个部分组成。Selector(SEL)相当于一个方法的id;IMP是方法的实现。这样分开的一个便利之处是selector和IMP之间的对应关系可以被改变。这也是方法拦截替换的实现基础。而 Method Swizzling 可以交换两个方法的实现。

Objective-C 提供了以下 API 来动态替换类方法或实例方法的实现:

这 3 个方法有一些细微的差别,给大家介绍如下:

从以上的区别我们可以总结出这 3 个 API 的使用场景:

以上 3 个方法的源码在 这里,感兴趣的同学可以读一读。

应用一:拦截系统自带的方法调用Method Swizzling

一般是在load方法中进行,确保最先被调用。+load方法会在Appdelegate的方法之前执行,是最先执行的方法。

使用场景

Method Swizzling 可以重写某个方法而不用继承,同时还可以调用原先的实现。通常的做法是在category中添加一个方法(当然也可以是一个全新的class)。可以通过method_exchangeImplementations这个运行时方法来交换实现。

以下的代码在UIViewController的类别中

+ (void)load {
    
    //在debug模式下进行方法的交换
    
#ifdef DEBUG
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
       //原本的viewWillAppear方法
       Method  viewWillAppear11= class_getInstanceMethod([self class], @selector(viewWillAppear:));
       //需要替换成 能够输出日志的viewWillAppear
       Method logViewWillAppear11 = class_getInstanceMethod([self class], @selector(logViewWillAppear:));
        BOOL addSucc = class_addMethod([self class], @selector(viewWillAppear:), method_getImplementation(logViewWillAppear11), method_getTypeEncoding(logViewWillAppear11));
        if (addSucc) {
            class_replaceMethod([self class], @selector(logViewWillAppear:), method_getImplementation(viewWillAppear11), method_getTypeEncoding(viewWillAppear11));
        }
        else{
            method_exchangeImplementations(viewWillAppear11, logViewWillAppear11);
        }
     });
#endif
}

- (void)logViewWillAppear:(BOOL)animated {
    
    NSString *className = NSStringFromClass([self class]);
    //在这里,你可以进行过滤操作,指定哪些viewController需要打印,哪些不需要打印
    if ([className hasPrefix:@"UI"] == NO) {
        NSLog(@"%@ will appear OOOOOO",className);
    }
//    //下面方法的调用,其实是调用viewWillAppear
//    [self logViewWillAppear:animated];
}

应用二:运用Runtime知识替换系统方法避免崩溃

为一些容易造成崩溃的类,在其分类中的 +(void) load方法中完成方法的替换
在自定义的方法中处理掉那些造成崩溃的逻辑即可。

举个栗子:

@implementation NSArray (Safe)  
+ (void)load
  {
     
    [NSClassFromString(@"__NSArray0") swapMethod:@selector(objectAtIndex:)
                                   currentMethod:@selector(ls_zeroObjectAtIndex:)];
    
  }

- (id)ls_zeroObjectAtIndex:(NSUInteger)index
{
    if (index >= self.count)
    {
        return nil;
    }
    return [self ls_zeroObjectAtIndex:index];
}
上一篇 下一篇

猜你喜欢

热点阅读