通过#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 方法替换的更多相关文章

  1. iOS开发-Runtime详解

    iOS开发-Runtime详解 简介 Runtime 又叫运行时,是一套底层的 C 语言 API,其为 iOS 内部的核心之一,我们平时编写的 OC 代码,底层都是基于它来实现的.比如: [recei ...

  2. iOS 开发-- Runtime 1小时入门教程

    1小时让你知道什么是Objective-C Runtime,并对它有一定的基本了解,可以在开发过程中运用自如. 三.Objective-C Runtime到底是什么东西? 简而言之,Objective ...

  3. Runtime 方法替换 和 动态添加实例方法 结合使用

    前言: 方法替换,可以替换任意外部类的方法,而动态添加方法只能实现在被添加类创建的对象里,但是将方法替换和动态添加方法结合使用,可以实现,对任意外部类动态添加需要的方法,这个方法可以是类方法也可以是实 ...

  4. iOS开发runtime学习:一:runtime简介与runtime的消息机制

    一:runtime简介:也是面试必须会回答的部分 二:runtime的消息机制 #import "ViewController.h" #import <objc/messag ...

  5. iOS 11 使用方法替换(Method Swizzling),去掉导航栏返回按钮的文字

    方法一:设置BarButtonItem的文本样式为透明颜色,代码如下: [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegro ...

  6. iOS开发·runtime原理与实践: 消息转发篇(Message Forwarding) (消息机制,方法未实现+API不兼容奔溃,模拟多继承)...

    本文Demo传送门: MessageForwardingDemo 摘要:编程,只了解原理不行,必须实战才能知道应用场景.本系列尝试阐述runtime相关理论的同时介绍一些实战场景,而本文则是本系列的消 ...

  7. ios开发runtime学习三:动态添加方法(实际应用少,面试)

    #import "ViewController.h" #import "Person.h" /* 1: Runtime(动态添加方法):OC都是懒加载机制,只要 ...

  8. ios开发runtime学习二:runtime交换方法

    #import "ViewController.h" /* Runtime(交换方法):主要想修改系统的方法实现 需求: 比如说有一个项目,已经开发了2年,忽然项目负责人添加一个功 ...

  9. iOS开发-Runtime详解(简书)

    简介 Runtime 又叫运行时,是一套底层的 C 语言 API,其为 iOS 内部的核心之一,我们平时编写的 OC 代码,底层都是基于它来实现的.比如: [receiver message]; // ...

随机推荐

  1. 数学--数论-- HDU -- 2854 Central Meridian Number (暴力打表)

    A Central Meridian (ACM) Number N is a positive integer satisfies that given two positive integers A ...

  2. Nginx编译与安装

    我的系统是CentOS-7,Nginx的源码可以在官网下载,网址为:http://nginx.org/en/download.html,我下载了目前的最新版本nginx-1.9.3.tar.gz 下载 ...

  3. python文件路径分隔符的详细分析

    写了挺久的python,文件分隔符的掌握肯定是必须的,但是我之前写的都是不规范的文件路径分隔符,例如‘’C:\User\temp\python.txt’,一直都没有报过错.也不知为啥,今天查阅资料才知 ...

  4. libevhtp初探

    libevent的evhttp不适合多线程,libevhtp重新设计了libevent的http API,采用了和memcached类似的多线程模型. worker线程的管道读事件的回调函数为htp_ ...

  5. Linux权限管理、系统进程管理

    权限管理 linux系统中分为四种角色 u=user 当前用户   g=group 同组用户   o=other 其他用户   a=all 代表所有用户 三种权限 r=read 可读 w=write ...

  6. 前端自适应样式reset.css

    @charset "utf-8"; /* CSS Document */ html, body, ul, li, ol, dl, dd, dt, p, h1, h2, h3, h4 ...

  7. 环境篇:Kylin3.0.1集成CDH6.2.0

    环境篇:Kylin3.0.1集成CDH6.2.0 Kylin是什么? Apache Kylin™是一个开源的.分布式的分析型数据仓库,提供Hadoop/Spark 之上的 SQL 查询接口及多维分析( ...

  8. xml(3)

    xml的解析方式:dom解析和sax解析 DOM解析 使用jaxp进行增删改查 1.创建DocumentBuilderFactory工厂 2.通过DocumentBuilderFactory工厂创建D ...

  9. SQL SERVER 的窗体函数OVER的使用:row_number/rank/dense_rank

    举个例子给大家加深印象,也方便理解: 1.目前有这几笔数据: Select as score into #studentSoure union all Select as score union al ...

  10. angular js 分页

    一.编写实体类PageResult public class PageResult implements Serializable { private Long total;//总记录数 privat ...