iOS ------ Method Swizzling (动态方法交换)

一,Method Swizzling 简介

Method(方法)对应的是objc_method结构体;而objc_method结构体中包含了SEL method_name(方法名),IMP method_imp(方法实现)

// objc_method 结构体
typedef struct objc_method *Method;

struct objc_method {
    SEL _Nonnull method_name;                    // 方法名
    char * _Nullable method_types;               // 方法类型
    IMP _Nonnull method_imp;                     // 方法实现
};

Method(方法),SEL(方法名),IMP(方法实现)三者的关系:

在运行中,class(类)维护了一个method list(方法列表)来确定消息的正确发送。OC中调用方法叫做发送消息,发送消息前会查找消息,查找过程就是通过SEL查找IMP的过程。method list (方法列表)存放的元素就是Method(方法)。而Method(方法)中映射了一对键值对:SEL(方法名)IMP(方法实现)

原理:
Method swizzling修改了method list(方法列表),使不同Method(方法)中的键值对发生了交换。比如交换前两个键值对分别为SEL A:IMP A,SEL B:IMP B,交换之后就变为了SEL A : IMP B、SEL B : IMP A。
在这里插入图片描述

二,MethodSwizzling简单代码实现

在当前类的+(void)load方法中增加Method Swizzling操作,交换(void)originalFunction(void)swizzledFunction的方法实现。

#import "ViewController.h"
#import <objc/runtime.h>
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self SwizzlingMethod];
    [self originalFunction];
    [self swizzledFunction];
    
    
}
- (void)SwizzlingMethod {
    //当前类
    Class class = [self class];
    
    //方法名
    SEL originalSeletor = @selector(originalFunction);
    SEL swizzledSeletor = @selector(swizzledFunction);
    
    //方法结构体
    Method originalMethod = class_getInstanceMethod(class, originalSeletor);
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSeletor);
    
    //调用交换两个方法的实现
    method_exchangeImplementations(originalMethod, swizzledMethod);
}
//原始方法
- (void)originalFunction {
    NSLog(@"originalFunction");
}
//替换方法
- (void)swizzledFunction {
    NSLog(@"swizzledFunction");
}
@end

在这里插入图片描述

上面的代码简单的将两个方法进行了交换。但在实际应用中并不是那么简单,更多的是为当前类添加一个分类,然后在分类中进行MethodSwimming操作,并且要考虑的东西要更多,且更复杂。

三,MethodSwizzling的使用方案

一般是在该类的分类中添加MethodSwizzling交换方法

@implementation UIViewController (Swizzling)

// 交换 原方法 和 替换方法 的方法实现
+ (void)load {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 当前类
        Class class = [self class];
        
        // 原方法名 和 替换方法名
        SEL originalSelector = @selector(originalFunction);
        SEL swizzledSelector = @selector(swizzledFunction);
        
        // 原方法结构体 和 替换方法结构体
        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
        
        /* 如果当前类没有 原方法的 IMP,说明在从父类继承过来的方法实现,
         * 需要在当前类中添加一个 originalSelector 方法,
         * 但是用 替换方法 swizzledMethod 去实现它 
         */
        BOOL didAddMethod = class_addMethod(class,
                                            originalSelector,
                                            method_getImplementation(swizzledMethod),
                                            method_getTypeEncoding(swizzledMethod));
        
        if (didAddMethod) {
            // 原方法的 IMP 添加成功后,修改 替换方法的 IMP 为 原始方法的 IMP
            class_replaceMethod(class,
                                swizzledSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
        } else {
            // 添加失败(说明已包含原方法的 IMP),调用交换两个方法的实现
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
    });
}

// 原始方法
- (void)originalFunction {
    NSLog(@"originalFunction");
}

// 替换方法
- (void)swizzledFunction {
    NSLog(@"swizzledFunction");
}

@end

一些用到的方法

通过SEL获取方法Method

// 获取实例方法
OBJC_EXPORT Method _Nullable
class_getInstanceMethod(Class _Nullable cls, SEL _Nonnull name);

// 获取类方法
OBJC_EXPORT Method _Nullable
class_getClassMethod(Class _Nullable cls, SEL _Nonnull name);

IMP的getter/setter方法

// 获取一个方法的实现
OBJC_EXPORT IMP _Nonnull
method_getImplementation(Method _Nonnull m); 

// 设置一个方法的实现
OBJC_EXPORT IMP _Nonnull
method_setImplementation(Method _Nonnull m, IMP _Nonnull imp)

替换方法

// 获取方法实现的编码类型
OBJC_EXPORT const char * _Nullable
method_getTypeEncoding(Method _Nonnull m);

// 添加方法实现
OBJC_EXPORT BOOL
class_addMethod(Class _Nullable cls, SEL _Nonnull name, IMP _Nonnull imp, 
                const char * _Nullable types);
                
// 替换方法的 IMP,如:A替换B(B指向A,A还是指向A)
OBJC_EXPORT IMP _Nullable
class_replaceMethod(Class _Nullable cls, SEL _Nonnull name, IMP _Nonnull imp, 
                    const char * _Nullable types);
                    
// 交换两个方法的 IMP,如:A交换B(B指向A,A指向B)
OBJC_EXPORT void
method_exchangeImplementations(Method _Nonnull m1, Method _Nonnull m2);

四,注意事项

1.保证方法交换只执行一次

为了保证方法交换的代码可以优先交换,一般会将其写在+load方法中,但是+load的方法也能被主动调用,如果多次调用就会被还原,如果调用[super load] 方法也会造成这样的结果;所以我们要保证方法只交换一次,可选择在单例模式下。

+ (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self lz_methodSwizzlingWithClass:self oriSEL:@selector(study) swizzledSEL:@selector(play)];
    });
}

2.用子类方法替换父类方法

在子类中用子类的方法subFuntionA替换父类的方法function A。子类实例和父类实例分别调用function A,最终都实现的是subFuntionA。

如果我们在子类的方法subFuntionA1替换了父类中的方法functionA后想要继续调用functionA,同理应该这样写

- (void)subFunctionA {
    [self subFunctionA];
    NSLog(@"%s", __func__);
}

再用子类实例和父类实例分别调用function A。

父类调用时就会报错,子类调用就不会。

在上面的函数中调用subFuntionA,但父类本身方法列表中没subFuntionA,所以父类也就报了unrecognized selector 的错误。

出现上面找不到方法的原因是:子类用自己的实现直接替换了父类的方法。

如果我们能不能为子类添加一个和父类一样的方法,子类中进行替换就不会影响父类了。

+ (void)swizzingClassB:(Class)cls oldSEL:(SEL)oldSel toNewSel:(SEL)newSel {
    if (!cls) { return; }
    Method oldM = class_getInstanceMethod(cls, oldSel);
    Method newM = class_getInstanceMethod(cls, newSel);
    
    // 先尝试给 cls 添加方法(SEL: oldSel  IMP: newM),防止子类直接替换父类中的方法
    BOOL addSuccess = class_addMethod(cls, oldSel, method_getImplementation(newM), method_getTypeEncoding(oldM));
    
    if (addSuccess) { // 添加成功即:原本没有 oldSel,成功为子类添加了一个 oldSel - newM 的方法
        // 这里将原 newSel的imp替换为 oldM 的 IMP
        class_replaceMethod(cls, newSel, method_getImplementation(oldM), method_getTypeEncoding(oldM));
    }
    else {
        method_exchangeImplementations(oldM, newM);
    }
}
  • 使用class_addMethod为当前类添加functionA方法,关联subFuntionA方法的imp
  • 返回值为NO,说明子类已经实现了subFuntionA,则直接进行方法交换,不会影响父类
  • 返回值为YES,说明子类未实现了subFuntionA,添加成功后,使用class_replaceMethod将sub functionA替换为functionA的imp

再用子类实例和父类实例分别调用function A。
这时父类实例调用functionA没有受到子类方法交换的影响,实现的就是functionA。
而子类实例就会在实现subfunctionA中实现function A。

五,MethodSwizzling应用场景

1,为UITableView的异常加载占位图

对于UITableView的异常加载情况分为无数据或网络异常。
对于检测tableView是否为空,借助tableView的代理dataSource即可。核心代码是,依次获取table View所具有的组数和行数,通过isEmpty这个flag标示最后确定是否添加占位图。

- (void)checkEmpty {
    BOOL isEmpty = YES;//flag标示

    id  dataSource = self.dataSource;
    NSInteger sections = 1;//默认一组
    if ([dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)]) {
        sections = [dataSource numberOfSectionsInTableView:self];//获取当前TableView组数
    }

    for (NSInteger i = 0; i < sections; i++) {
        NSInteger rows = [dataSource tableView:self numberOfRowsInSection:sections];//获取当前TableView各组行数
        if (rows) {
            isEmpty = NO;//若行数存在,不为空
        }
    }
    if (isEmpty) {//若为空,加载占位图
        if (!self.placeholderView) {//若未自定义,展示默认占位图
            [self makeDefaultPlaceholderView];
        }
        self.placeholderView.hidden = NO;
        [self addSubview:self.placeholderView];
    } else {//不为空,隐藏占位图
        self.placeholderView.hidden = YES;
    }
}

接下来实现如何添加占位图
如果可以让tableView在执行reloadData时自动检查其行数就可以了。也就是我们在原有的reload Data方法的基础上添加checkEmpty此方法。这里我们可以通过MethodSwizzling替换reload Data方法,给予它新的实现。

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //方法交换,将reloadData实现交换为sure_reloadData
        [self methodSwizzlingWithOriginalSelector:@selector(reloadData) bySwizzledSelector:@selector(sure_reloadData)];
    });
}

- (void)sure_reloadData {
    [self checkEmpty];
    [self sure_reloadData];
}

这样就可以在实现reloadData的同时检查行数从而判断我是否加载占位图的功能。

具体实现demo
tableView的异常加载占位图

2,处理UIButton的重复点击

避免一个按钮被快速点击多次。同样利用Method Swizzling

  • 为 UIControl 或 UIButton 建立一个 Category。
  • 在分类中添加一个 NSTimeInterval xxx_acceptEventInterval; 的属性,设定重复点击间隔
  • 在分类中实现一个自定义的 xxx_sendAction:to:forEvent: 方法,在其中添加限定时间相应的方法。
  • 利用 Method Swizzling 将 sendAction:to:forEvent: 方法和 xxx_sendAction:to:forEvent: 进行方法交换。
#import "UIButton+TBCustom.h"
#import <objc/runtime.h>

@interface UIButton()

@property (nonatomic, assign) NSTimeInterval custom_acceptEventInterval; // 可以用这个给重复点击加间隔

@end

@implementation UIButton (TBCustom)

+ (void)load{
    Method systemMethod = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
    SEL sysSEL = @selector(sendAction:to:forEvent:);
    
    Method customMethod = class_getInstanceMethod(self, @selector(custom_sendAction:to:forEvent:));
    SEL customSEL = @selector(custom_sendAction:to:forEvent:);
    
    //添加方法 语法:BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types) 若添加成功则返回No
    // cls:被添加方法的类  name:被添加方法方法名  imp:被添加方法的实现函数  types:被添加方法的实现函数的返回值类型和参数类型的字符串
    BOOL didAddMethod = class_addMethod(self, sysSEL, method_getImplementation(customMethod), method_getTypeEncoding(customMethod));
    
    //如果系统中该方法已经存在了,则替换系统的方法  语法:IMP class_replaceMethod(Class cls, SEL name, IMP imp,const char *types)
    if (didAddMethod) {
        class_replaceMethod(self, customSEL, method_getImplementation(systemMethod), method_getTypeEncoding(systemMethod));
    }else{
        method_exchangeImplementations(systemMethod, customMethod);
        
    }
}

- (NSTimeInterval )custom_acceptEventInterval{
    return [objc_getAssociatedObject(self, "UIControl_acceptEventInterval") doubleValue];
}

- (void)setCustom_acceptEventInterval:(NSTimeInterval)custom_acceptEventInterval{
    objc_setAssociatedObject(self, "UIControl_acceptEventInterval", @(custom_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSTimeInterval )custom_acceptEventTime{
    return [objc_getAssociatedObject(self, "UIControl_acceptEventTime") doubleValue];
}

- (void)setCustom_acceptEventTime:(NSTimeInterval)custom_acceptEventTime{
    objc_setAssociatedObject(self, "UIControl_acceptEventTime", @(custom_acceptEventTime), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)custom_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{
    
    // 如果想要设置统一的间隔时间,可以在此处加上以下几句
    // 值得提醒一下:如果这里设置了统一的时间间隔,只会影响UIButton, 如果想统一设置,也想影响UISwitch,建议将UIButton分类,改成UIControl分类,实现方法是一样的
     if (self.custom_acceptEventInterval <= 0) {
         // 如果没有自定义时间间隔,则默认为.4秒
        self.custom_acceptEventInterval = .4;
     }
    
    // 是否小于设定的时间间隔
    BOOL needSendAction = (NSDate.date.timeIntervalSince1970 - self.custom_acceptEventTime >= self.custom_acceptEventInterval);
    
    // 更新上一次点击时间戳
    if (self.custom_acceptEventInterval > 0) {
        self.custom_acceptEventTime = NSDate.date.timeIntervalSince1970;
    }
    
    // 两次点击的时间间隔小于设定的时间间隔时,才执行响应事件
    if (needSendAction) {
        [self custom_sendAction:action to:target forEvent:event];
    }
}

addTarget:action:forControlEvents: 方法将 buttonTapped: 方法与按钮的点击事件关联起来。当用户点击按钮时,按钮会调用 sendAction:to:forEvent: 方法,并将 buttonTapped: 方法作为动作发送给指定的目标对象(在这里是 self,即当前对象)

3,处理数组越界的问题

Method Swizzling 可以用于解决数组越界导致的崩溃问题。通过交换 NSArray 或 NSMutableArray 的方法实现,我们可以在访问数组元素之前进行边界检查,以防止越界访问。

#import <objc/runtime.h>

@implementation NSArray (SafeAccess)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];
        SEL originalSelector = @selector(objectAtIndex:);
        SEL swizzledSelector = @selector(safe_objectAtIndex:);
        
        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);
        }
    });
}

- (id)safe_objectAtIndex:(NSUInteger)index {
    if (index < self.count) {
        return [self safe_objectAtIndex:index];
    } else {
        NSLog(@"Array index out of bounds: %lu", (unsigned long)index);
        return nil;
    }
}

@end

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/584735.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Hadoop概述

大数据处理技术 对大数据技术的基本概念进行简单介绍&#xff0c;包括分布式计算、服务器集群和 Google 的 3 个大数据技术。 分布式计算 对于如何处理大数据&#xff0c;计算机科学界有两大方向。 第一个方向是集中式计算&#xff0c;就是通过不断增加处理器的数量来增强单…

开源项目介绍-01:AAMED-master 圆和椭圆检测

前言: AAMED: Arc Adjacency Matrix based Fast Ellipse Detection :基于弧邻接矩阵的快速椭圆检测 1 下载 GitHub - Li-Zhaoxi/AAMED: Arc Adjacency Matrix based Fast Ellipse Detection 开源项目,支持windows 和 Linux的,然后,有C++,Python,Matlab的几个版本。 Git…

SQL底层执行过程

MySQL 的查询流程 客户端请求连接器 负责与客户端的通信,是半双工模式&#xff08;半双工(Half Duplex)数据传输指数据可以在一个信号载体的两个方向上传输,但是不能同时传输。&#xff09;&#xff0c;验证请求用户的账户和密码是否正确&#xff0c;③如果用户的账户和密码验…

模型量化与量化在LLM中的应用 | 得物技术

一、模型推理优化 随着模型在各种场景中的落地实践&#xff0c;模型的推理加速早已成为AI工程化的重要内容。而近年基于Transformer架构的大模型继而成为主流&#xff0c;在各项任务中取得SoTA成绩&#xff0c;它们在训练和推理中的昂贵成本使得其在合理的成本下的部署实践显得…

Git 如何修改已经推送的错误提交信息(有图有真相)

解决方案一&#xff1a;修改最新的提交信息 首先&#xff0c;我们来考虑最简单的情况&#xff1a;如果你在最近一次提交时输入了错误的提交信息&#xff0c;并且还没有进行下一次提交&#xff0c;那么你可以使用如下命令来修改最新的提交信息&#xff1a; $ git commit --ame…

VSCode SSH连接远程主机失败,显示Server status check failed - waiting and retrying

vscode ssh连接远程主机突然连接不上了&#xff0c;终端中显示&#xff1a;Server status check failed - waiting and retrying 但是我用Xshell都可以连接成功&#xff0c;所以不是远程主机的问题&#xff0c;问题出在本地vscode&#xff1b; 现象一&#xff1a; 不停地输入…

CPU架构

一、CPU组成 CPU内部由ALU&#xff08;算术逻辑单元&#xff09;、CU&#xff08;控制器&#xff09;、寄存器&#xff08;PC、IR、PSW、DR、通用寄存器等&#xff09;、中断系统组成&#xff0c;外部通过总线与控制总线、数据总线、地址总线进行相连&#xff0c;对数据和程序…

【保姆级讲解如何安装与配置Xcode】

&#x1f308;个人主页: 程序员不想敲代码啊 &#x1f3c6;CSDN优质创作者&#xff0c;CSDN实力新星&#xff0c;CSDN博客专家 &#x1f44d;点赞⭐评论⭐收藏 &#x1f91d;希望本文对您有所裨益&#xff0c;如有不足之处&#xff0c;欢迎在评论区提出指正&#xff0c;让我们共…

第十二章 案例二:配置Trunk,实现相同VLAN的跨交换机通信

1、实验环境 公司的员工人数已达到 100 人&#xff0c;其网络设备如图12.13所示&#xff0c;现在的网络环境导致广播较多网速慢&#xff0c;并且也不安全&#xff0c;公司希望按照部门划分网络&#xff0c;并且能够保证一定的网络安全性 图12.13 实验案例二拓扑图 其网络规划…

环境安装:python环境迁移(无网和有网)

前言 环境部署或迁移是一项简单而又考验应对能力的一项工作&#xff0c;需要考虑到网络环境的情况&#xff0c;无网环境下需要采取离线方式进行操作&#xff0c;有网环境则可以直接通过在线安装完成。 在进行Python环境迁移时&#xff0c;需要注意保持环境的一致性&#xff0c;…

Notion是什么,Notion软件下载,Notion官方网站在哪里?国内用户Notion怎么订阅升级会员?

Notion是什么 Notion&#xff0c;一款强大的多功能工具&#xff0c;可用于组织笔记、任务、项目、数据库和文档等。 Notion软件下载 这个到Notion官方网站下载就可以了。 怎么订阅Notion会员 注册好了Notion的账号&#xff0c;来到首页&#xff0c;点击设置&#xff0c;左边…

LED显示屏黑屏的常见原因及解决办法

LED显示屏在使用过程中偶尔会出现黑屏现象&#xff0c;可能由多种原因引起。以下是LED显示屏出现黑屏的主要情况及相应的解决办法&#xff1a; 1. 上电的瞬间就是黑的 原因&#xff1a;可能是因为上电过程中出现了一些异常情况&#xff0c;导致屏幕无法正常显示。 解决办法&…

如何保证Redis双写一致性?

目录 数据不一致问题 数据库和缓存不一致解决方案 1. 先更新缓存&#xff0c;再更新数据 该方案数据不一致的原因 2. 先更新数据库&#xff0c;再更新缓存 3. 先删除缓存&#xff0c;再更新数据库 延时双删 4. 先更新数据库&#xff0c;再删除缓存 该方案数据不一致的…

新手如何用Postman做接口自动化测试?

1、什么是自动化测试 把人对软件的测试行为转化为由机器执行测试行为的一种实践。 例如GUI自动化测试&#xff0c;模拟人去操作软件界面&#xff0c;把人从简单重复的劳动中解放出来&#xff0c;本质是用代码去测试另一段代码&#xff0c;属于一种软件开发工作&#xff0c;已…

洗地机哪个牌子质量最好?四款年度口碑品牌盘点

在当今家庭生活中&#xff0c;洗地机技术的飞速发展给我们的日常带来了极大的便利。洗地机作为智能家居领域的佼佼者&#xff0c;其实用性和智能化程度受到越来越多消费者的欢迎。许多人在选洗地机时会纠结一个问题&#xff1a;洗地机哪个牌子质量最好&#xff1f; 洗地机怎么…

# 谷歌 Chrome 浏览器无法安装插件的解决方法

谷歌 Chrome 浏览器无法安装插件的解决方法 运用开发模式安装 安装步骤&#xff1a; 1、 将 XX.crx 插件的扩展名改成 .zip 或者 .rar 并解压到文件夹 XX 目录。 1&#xff09;如&#xff1a;下载的 前端框架 vue.js 插件 nhdogjmejiglipccpnnnanhbledajbpd-6.6.1-Crx4Chro…

【AGX】Ubuntu20.04 + ROS_ noetic+ 大疆Mid360激光 雷达评测

大家好&#xff0c;我是虎哥&#xff0c;最近组装机器人&#xff0c;使用到了大疆孵化的圳市览沃科技有限公司&#xff08;简称Livox览沃科技&#xff09;推出的觅道系列全新混合固态激光雷达Mid-360&#xff0c;顺便试试效果&#xff0c;也记录一下使用入门过程。 "觅道M…

自然语言处理 (NLP) 和文本分析

自然语言处理 (NLP) 和文本分析&#xff1a;NLP 在很多领域都有着广泛的应用&#xff0c;如智能助手、语言翻译、舆情分析等。热门问题包括情感分析、命名实体识别、文本生成等。 让我们一起来详细举例子的分析讲解一下自然语言处理&#xff08;NLP&#xff09;和文本分析的应用…

基于java+springboot+vue实现的新闻资讯系统(文末源码+Lw)216

摘 要 传统信息的管理大部分依赖于管理人员的手工登记与管理&#xff0c;然而&#xff0c;随着近些年信息技术的迅猛发展&#xff0c;让许多比较老套的信息管理模式进行了更新迭代&#xff0c;文章信息因为其管理内容繁杂&#xff0c;管理数量繁多导致手工进行处理不能满足广…

String2⃣️-string类对象的修改操作

目录 string类对象的修改操作 1.push back &#xff1b;append&#xff1b;operator 2.assign&#xff1b;insert 3.erase&#xff1b;replace 4.rfind 5.substr 6.c_str 补充 1.reverse&#xff08;逆置 反转&#xff09; 2.Sort string类对象的修改操作 1.push back …
最新文章