Runtime系列2--Method Swizzling

Method Swizzling 的原理

咱们先来了解下 Objective-C 中方法 Method 的数据结构:html

typedef struct method_t *Method;
struct method_t {
    SEL name;
    const char *types;
    IMP imp;

    struct SortBySELAddress :
        public std::binary_function
  
  
  

 
  
  { bool operator() (const method_t& lhs, const method_t& rhs) { return lhs.name < rhs.name; } }; }; 

 复制代码

本质上,它就是 struct method_t 类型的指针,因此咱们重点看下结构体 method_t 的定义。在结构体 method_t 中定义了三个成员变量和一个成员函数:ios

  • name 表示的是方法的名称,用于惟一标识某个方法,好比 @selector(viewWillAppear:) ;
  • types 表示的是方法的返回值和参数类型(详细信息能够查阅苹果官方文档中的 Type Encodings);
  • imp 是一个函数指针,指向方法的实现;
  • SortBySELAddress 顾名思义,是一个根据 name 的地址对方法进行排序的函数。

由此,咱们也能够发现 Objective-C 中的方法名是不包括参数类型的,也就是说下面两个方法在 runtime 看来就是同一个方法:编程

- (void)viewWillAppear:(BOOL)animated;
- (void)viewWillAppear:(NSString *)string;复制代码

而下面两个方法倒是能够共存的:数组

- (void)viewWillAppear:(BOOL)animated;
+ (void)viewWillAppear:(BOOL)animated;复制代码

由于实例方法和类方法是分别保存在类对象和元类对象中的.安全

原则上,方法的名称 name 和方法的实现 imp 是一一对应的,而 Method Swizzling 的原理就是动态地改变它们的对应关系,以达到替换方法实现的目的。数据结构


Method Swizzling 的使用

那么何时咱们才须要使用 Method Swizzling呢?多线程

咱们主要是用它来把系统的方法交换为咱们本身的方法,从而给系统方法添加一些咱们想要的功能。函数

下面会列举诸多使用场景,让你们见识一下 Method Swizzling 的强大之处。字体

因为method swizzling的实现模式是固定的,因此咱们能够抽成一个方法,做为NSobject的分类,之后能够直接调用便可。ui

1. NSObject分类代码

@interface NSObject (Swizzling) 

+ (void)methodSwizzlingWithOriginalSelector:(SEL)originalSelector bySwizzledSelector:(SEL)swizzledSelector; 

@end


====================================================================
#import "NSObject+MethodSwizzling.h"
#import 
  
  
  

 
  
  @implementation NSObject (MethodSwizzling) + (void)methodSwizzlingWithOriginalSelector:(SEL)originalSelector bySwizzledSelector:(SEL)swizzledSelector { Class class = [self class]; Method originalMethod = class_getInstanceMethod(class, originalSelector); Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); //先尝试給源SEL添加IMP,这里是为了不源SEL没有实现IMP的状况 BOOL didAddMethod = class_addMethod(class,originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)); if (didAddMethod) {//添加成功:说明源SEL没有实现IMP,将源SEL的IMP替换到交换SEL的IMP class_replaceMethod(class,swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); } else {//添加失败:说明源SEL已经有IMP,直接将两个SEL的IMP交换便可 method_exchangeImplementations(originalMethod, swizzledMethod); } } @end 

 复制代码

2.简单调用

#import "UIViewController+MethodSwizzling.h"
#import "NSObject+MethodSwizzling.h"

@implementation UIViewController (MethodSwizzling)
+(void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    [self methodSwizzlingWithOriginalSelector:@selector(viewWillAppear:) bySwizzledSelector:@selector(my_viewWillAppear:) ];
    });
}

-(void)my_viewWillAppear:(BOOL)animated{
    NSLog(@"调用了本身定义的viewWillAppear方法");
    [self my_viewWillAppear:YES];
}
@end复制代码

3.注意点

  1. 为何要在+(void)load方法里面调用method swizzling?

    答案:

    +load 和 +initialize 是 Objective-C runtime 会自动调用的两个类方法。可是它们被调用的时机倒是有差异的,+load 方法是在类被加载的时候调用的,也就是必定会被调用。而 +initialize 方法是在类或它的子类收到第一条消息以前被调用的,这里所指的消息包括实例方法和类方法的调用。也就是说 +initialize 方法是以懒加载的方式被调用的,若是程序一直没有给某个类或它的子类发送消息,那么这个类的 +initialize 方法是永远不会被调用的。此外 +load 方法还有一个很是重要的特性,那就是子类、父类和分类中的 +load 方法的实现是被区别对待的。换句话说在 Objective-C runtime 自动调用 +load 方法时,分类中的 +load 方法并不会对主类中的 +load 方法形成覆盖。综上所述,+load 方法是实现 Method Swizzling 逻辑的最佳“场所”。

  2. 为何使用dispatch_once执行方法交换?

    答案:

    方法交换应该要线程安全,并且保证在任何状况下(多线程环境,或者被其余人手动再次调用+load方法)只交换一次,除非只是临时交换使用,在使用完成后又交换回来。 最经常使用的用法是在+load方法中使用dispatch_once来保证交换是安全的。


场景1. NSMutableArray崩溃问题

当咱们对一个NSMutableArray插入或者添加一个nil会致使崩溃,或者须要获取、移除的对象超过了数组的最大边界,就会出现数组越界,也会致使崩溃。

这个时候咱们可使用method swizzling来解决该问题,让数组在上述状况不会崩溃。

实例代码:

#import "NSObject+MethodSwizzling.h"
#import "NSMutableArray+SWmethod.h"
#import 
  
  
  

 
  
  @implementation NSMutableArray (SWmethod) + (void)load { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ [objc_getClass("__NSArrayM") methodSwizzlingWithOriginalSelector:@selector(removeObject:) bySwizzledSelector:@selector(safeRemoveObject:) ]; [objc_getClass("__NSArrayM") methodSwizzlingWithOriginalSelector:@selector(addObject:) bySwizzledSelector:@selector(safeAddObject:)]; [objc_getClass("__NSArrayM") methodSwizzlingWithOriginalSelector:@selector(removeObjectAtIndex:) bySwizzledSelector:@selector(safeRemoveObjectAtIndex:)]; [objc_getClass("__NSArrayM") methodSwizzlingWithOriginalSelector:@selector(insertObject:atIndex:) bySwizzledSelector:@selector(safeInsertObject:atIndex:)]; [objc_getClass("__NSArrayM") methodSwizzlingWithOriginalSelector:@selector(objectAtIndex:) bySwizzledSelector:@selector(safeObjectAtIndex:)]; }); } - (void)safeAddObject:(id)obj { if (obj == nil) { NSLog(@"%s can add nil object into NSMutableArray", __FUNCTION__); } else { [self safeAddObject:obj]; } } - (void)safeRemoveObject:(id)obj { if (obj == nil) { NSLog(@"%s call -removeObject:, but argument obj is nil", __FUNCTION__); return; } [self safeRemoveObject:obj]; } - (void)safeInsertObject:(id)anObject atIndex:(NSUInteger)index { if (anObject == nil) { NSLog(@"%s can't insert nil into NSMutableArray", __FUNCTION__); } else if (index > self.count) { NSLog(@"%s index is invalid", __FUNCTION__); } else { [self safeInsertObject:anObject atIndex:index]; } } - (id)safeObjectAtIndex:(NSUInteger)index { if (self.count == 0) { NSLog(@"%s can't get any object from an empty array", __FUNCTION__); return nil; } if (index > self.count) { NSLog(@"%s index out of bounds in array", __FUNCTION__); return nil; } return [self safeObjectAtIndex:index]; } - (void)safeRemoveObjectAtIndex:(NSUInteger)index { if (self.count <= 0)="" {="" nslog(@"%s="" can't="" get="" any="" object="" from="" an="" empty="" array",="" __function__);="" return;="" }="" if="" (index="">= self.count) { NSLog(@"%s index out of bound", __FUNCTION__); return; } [self safeRemoveObjectAtIndex:index]; } @end 
 
   

 复制代码

注意点:类簇

看以下代码

[objc_getClass("__NSArrayM") methodSwizzlingWithOriginalSelector:@selector(removeObject:) bySwizzledSelector:@selector(safeRemoveObject:) ];复制代码

这里没有使用self来调用,而是使用objc_getClass("__NSArrayM")来调用的。由于NSMutableArray的真实类只能经过后者来获取,而不能经过[self class]来获取,而method swizzling只对真实的类起做用。

这里就涉及到一个小知识点:类簇。

在iOS中存在大量的类簇,好比咱们经常使用的NSArray,NSDictonary等等

你们发现了吗,__NSArrayI才是NSArray真正的类,而NSMutableArray又不同。咱们能够经过runtime函数获取真正的类:

objc_getClass("__NSArrayI")复制代码

image

有关类簇的更多知识,你们能够看一下两篇博客:

总结:

上面只是展现了如何避免NSMutableArray的崩溃,主要是在原有的系统方法里面加上了判断。据此你们能够本身实现NSArray、NSDictonary的崩溃处理


场景二、全局更换UILabel的默认字体

需求

在项目比较成熟的基础上,遇到了这样一个需求,应用中须要引入新的字体,须要更换全部Label的默认字体,可是同时,对于一些特殊设置了字体的label又不须要更换。乍看起来,这个问题确实十分棘手,首先项目比较大,一个一个设置全部使用到的label的font工做量是巨大的,而且在许多动态展现的界面中,可能会漏掉一些label,产生bug。其次,项目中的label来源并不惟一,有用代码建立的,有xib和storyBoard中的,这也将浪费很大的精力。

解决办法

这是最简单方便的方法,咱们可使用runtime机制替换掉UILabel的初始化方法,在其中对label的字体进行默认设置。由于Label能够从initWithFrame、init和nib文件三个来源初始化,因此咱们须要将这三个初始化的方法都替换掉。

#import "UILabel+YHBaseChangeDefaultFont.h"
#import 
  
  
  

 
  
  @implementation UILabel (YHBaseChangeDefaultFont) /** *每一个NSObject的子类都会调用下面这个方法 在这里将init方法进行替换,使用咱们的新字体 *若是在程序中又特殊设置了字体 则特殊设置的字体不会受影响 可是不要在Label的init方法中设置字体 *从init和initWithFrame和nib文件的加载方法 都支持更换默认字体 */ +(void)load{ //只执行一次这个方法 static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ Class class = [self class]; // When swizzling a class method, use the following: // Class class = object_getClass((id)self); //替换三个方法 SEL originalSelector = @selector(init); SEL originalSelector2 = @selector(initWithFrame:); SEL originalSelector3 = @selector(awakeFromNib); SEL swizzledSelector = @selector(YHBaseInit); SEL swizzledSelector2 = @selector(YHBaseInitWithFrame:); SEL swizzledSelector3 = @selector(YHBaseAwakeFromNib); Method originalMethod = class_getInstanceMethod(class, originalSelector); Method originalMethod2 = class_getInstanceMethod(class, originalSelector2); Method originalMethod3 = class_getInstanceMethod(class, originalSelector3); Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); Method swizzledMethod2 = class_getInstanceMethod(class, swizzledSelector2); Method swizzledMethod3 = class_getInstanceMethod(class, swizzledSelector3); BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)); BOOL didAddMethod2 = class_addMethod(class, originalSelector2, method_getImplementation(swizzledMethod2), method_getTypeEncoding(swizzledMethod2)); BOOL didAddMethod3 = class_addMethod(class, originalSelector3, method_getImplementation(swizzledMethod3), method_getTypeEncoding(swizzledMethod3)); if (didAddMethod) { class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); } else { method_exchangeImplementations(originalMethod, swizzledMethod); } if (didAddMethod2) { class_replaceMethod(class, swizzledSelector2, method_getImplementation(originalMethod2), method_getTypeEncoding(originalMethod2)); }else { method_exchangeImplementations(originalMethod2, swizzledMethod2); } if (didAddMethod3) { class_replaceMethod(class, swizzledSelector3, method_getImplementation(originalMethod3), method_getTypeEncoding(originalMethod3)); }else { method_exchangeImplementations(originalMethod3, swizzledMethod3); } }); } /** *在这些方法中将你的字体名字换进去 */ - (instancetype)YHBaseInit { id __self = [self YHBaseInit]; UIFont * font = [UIFont fontWithName:@"这里输入你的字体名字" size:self.font.pointSize]; if (font) { self.font=font; } return __self; } -(instancetype)YHBaseInitWithFrame:(CGRect)rect{ id __self = [self YHBaseInitWithFrame:rect]; UIFont * font = [UIFont fontWithName:@"这里输入你的字体名字" size:self.font.pointSize]; if (font) { self.font=font; } return __self; } -(void)YHBaseAwakeFromNib{ [self YHBaseAwakeFromNib]; UIFont * font = [UIFont fontWithName:@"这里输入你的字体名字" size:self.font.pointSize]; if (font) { self.font=font; } } @end 

 复制代码

场景3. 面向切面编程: 数据统计

想象这样一个场景,出于某些需求,咱们须要跟踪记录APP中按钮的点击次数和频率等数据,怎么解决?固然经过继承按钮类或者经过类别实现是一个办法,可是带来其余问题好比别人不必定会去实例化你写的子类,或者其余类别也实现了点击方法致使不肯定会调用哪个,抖机灵的方法以下:

@implementation UIButton (Hook)

+ (void)load {

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        Class selfClass = [self class];

        SEL oriSEL = @selector(sendAction:to:forEvent:);
        Method oriMethod = class_getInstanceMethod(selfClass, oriSEL);

        SEL cusSEL = @selector(mySendAction:to:forEvent:);
        Method cusMethod = class_getInstanceMethod(selfClass, cusSEL);

        BOOL addSucc = class_addMethod(selfClass, oriSEL, method_getImplementation(cusMethod), method_getTypeEncoding(cusMethod));
        if (addSucc) {
            class_replaceMethod(selfClass, cusSEL, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
        }else {
            method_exchangeImplementations(oriMethod, cusMethod);
        }

    });
}

- (void)mySendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
    [CountTool addClickCount];
    [self mySendAction:action to:target forEvent:event];
}

@end复制代码

总结:

method swizzling方法的强大之处在于能够替换的系统的方法实现,添加本身想要的功能。上面的一些使用场景都是网上找的一些经典例子,你们能够触类旁通。

更多文章欢迎关注个人我的blog

blog.ximu.site

相关文章
相关标签/搜索