iOS开发Runtime 方法替换
通过#import <objc/runtime.h>我们可以找到:
/**
* Returns a specified instance method for a given class.
*
* @param cls The class you want to inspect.
* @param name The selector of the method you want to retrieve.
*
* @return The method that corresponds to the implementation of the selector specified by
* \e name for the class specified by \e cls, or \c NULL if the specified class or its
* superclasses do not contain an instance method with the specified selector.
*
* @note This function searches superclasses for implementations, whereas \c class_copyMethodList does not.
*/
OBJC_EXPORT Method _Nullable
class_getInstanceMethod(Class _Nullable cls, SEL _Nonnull name)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
class_getInstanceMethod这个可以获取类的实例方法
#import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN @interface RuntimeObjc : NSObject -(void)runEg;
-(void)otherRunEg; @end
#import "RuntimeObjc.h"
#import <objc/runtime.h> @implementation RuntimeObjc +(void)load{ Method runEg = class_getInstanceMethod(self, @selector(runEg));
Method otherRunEg = class_getInstanceMethod(self, @selector(otherRunEg));
//交换
method_exchangeImplementations(runEg, otherRunEg);
} -(void)runEg{
NSLog(@"runEg");
}
-(void)otherRunEg{
NSLog(@"otherRunEg");
[self otherRunEg];//已经做了置换 故调用的runEg
}
@end
调用
RuntimeObjc * objc = [[RuntimeObjc alloc]init];
[objc runEg];
打印:
2020-05-21 15:42:43.416766+0800 11111[35733:176260] otherRunEg
2020-05-21 15:42:43.416928+0800 11111[35733:176260] runEg

iOS开发Runtime 方法替换的更多相关文章
- iOS开发-Runtime详解
iOS开发-Runtime详解 简介 Runtime 又叫运行时,是一套底层的 C 语言 API,其为 iOS 内部的核心之一,我们平时编写的 OC 代码,底层都是基于它来实现的.比如: [recei ...
- iOS 开发-- Runtime 1小时入门教程
1小时让你知道什么是Objective-C Runtime,并对它有一定的基本了解,可以在开发过程中运用自如. 三.Objective-C Runtime到底是什么东西? 简而言之,Objective ...
- Runtime 方法替换 和 动态添加实例方法 结合使用
前言: 方法替换,可以替换任意外部类的方法,而动态添加方法只能实现在被添加类创建的对象里,但是将方法替换和动态添加方法结合使用,可以实现,对任意外部类动态添加需要的方法,这个方法可以是类方法也可以是实 ...
- iOS开发runtime学习:一:runtime简介与runtime的消息机制
一:runtime简介:也是面试必须会回答的部分 二:runtime的消息机制 #import "ViewController.h" #import <objc/messag ...
- iOS 11 使用方法替换(Method Swizzling),去掉导航栏返回按钮的文字
方法一:设置BarButtonItem的文本样式为透明颜色,代码如下: [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegro ...
- iOS开发·runtime原理与实践: 消息转发篇(Message Forwarding) (消息机制,方法未实现+API不兼容奔溃,模拟多继承)...
本文Demo传送门: MessageForwardingDemo 摘要:编程,只了解原理不行,必须实战才能知道应用场景.本系列尝试阐述runtime相关理论的同时介绍一些实战场景,而本文则是本系列的消 ...
- ios开发runtime学习三:动态添加方法(实际应用少,面试)
#import "ViewController.h" #import "Person.h" /* 1: Runtime(动态添加方法):OC都是懒加载机制,只要 ...
- ios开发runtime学习二:runtime交换方法
#import "ViewController.h" /* Runtime(交换方法):主要想修改系统的方法实现 需求: 比如说有一个项目,已经开发了2年,忽然项目负责人添加一个功 ...
- iOS开发-Runtime详解(简书)
简介 Runtime 又叫运行时,是一套底层的 C 语言 API,其为 iOS 内部的核心之一,我们平时编写的 OC 代码,底层都是基于它来实现的.比如: [receiver message]; // ...
随机推荐
- Error: Can't find Python executable "G:\Python27"
错误如题,node-gyp官网介绍不够详细,应设置python.exe的具体绝对路径,如下所示: npm config set python G:\Python27\python.exe 转载于:ht ...
- 【原创】Linux Mutex机制分析
背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...
- Cell Phone Network G
最小点队的题意:https://www.luogu.com.cn/problem/P2899 与战略游戏不同的是,这里要求占领所有的点而不是边. 1自己被自己染色(有信号塔) 这时我们可以想一下,u被 ...
- 王颖奇 20171010129《面向对象程序设计(java)》第十一周学习总结
实验十一 集合 实验时间 2018-11-8 1.实验目的与要求 (1) 掌握Vetor.Stack.Hashtable三个类的用途及常用API: (2) 了解java集合框架体系组成: (3) ...
- Jmeter系列(11)- 并发线程组Concurrency Thread Group详解
如果你想从头学习Jmeter,可以看看这个系列的文章哦 https://www.cnblogs.com/poloyy/category/1746599.html Concurrency Thread ...
- 遍历HashMap常用的的三种方式
遍历HashMap常用的的三种方式 HashMap是我们使用非常多的集合之一,下面就来介绍几种常用的HashMap的遍历方式. 1.首先定义一个新的HashMap,并往里面添加一些数据. HashMa ...
- 如何得知某期刊是否被EI收錄?
转载:http://tul.blog.ntu.edu.tw/archives/4627 若因投稿或評鑑需要,欲得知某期刊是否被 EI 收錄,其實就是確認該期刊是否包含在 EV 平台中的 COMPEND ...
- [codeforces-542-C]YY?
链接:http://codeforces.com/problemset/problem/542/C 题意:对一个函数f(x),定义域[1,n], 令f(k,x) = f(f(f(f...f(x)))) ...
- go多种uuid生成方式
package main import ( "fmt" "github.com/chilts/sid" "github.com/kjk/betterg ...
- Python 的缩进是不是反人类的设计?
前些天,我写了<Python为什么使用缩进来划分代码块?>,文中详细梳理了 Python 采用缩进语法的 8 大原因.我极其喜欢这种简洁优雅的风格,所以对它赞美有加. 然而文章发出去后,非 ...