通过#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. Spring LDAP的使用

    LDAP入门http://www.jianshu.com/p/7e4d99f6baaf Spring LDAP,是Spring的一个组件,实现对LDAP的操作. 在编程操作MySQL时,我们除了用JD ...

  2. php beast windows编译教程

    git clone https://github.com/Microsoft/php-sdk-binary-tools.git c:\php-sdk cd c:\php-sdk git checkou ...

  3. H5 -- 取消a标签在点击时的背景颜色

    原文链接:点我 1.取消a标签在移动端点击时的蓝色 a { -webkit-tap-highlight-color: rgba(255, 255, 255, 0); -webkit-user-sele ...

  4. 数学--数论--Hdu 5793 A Boring Question (打表+逆元)

    There are an equation. ∑0≤k1,k2,⋯km≤n∏1⩽j<m(kj+1kj)%1000000007=? We define that (kj+1kj)=kj+1!kj! ...

  5. USB设备驱动模型

    嵌入式设备驱动的编写,基本上都要按照一定的驱动模型编写.不这么做的话,一旦设备发生了更新和改变,大部分的驱动代码都要推倒重来,代码的重用率低,不具备移植性.所以在新版linux2.6.22以后的内核版 ...

  6. 题目分享F 二代目

    题意:T个点R种双向边,P种单向边,求点S到每个点的最短距离 分析:(这再看不出来是spfa就该**了) 首先,这题能否用spfa就看他是否有负环呗,显然,双向边的权值非负,单向边还有个啥政策,总之显 ...

  7. 24-Java-Spring框架(二)

    Spring框架的了解.SpringIOC的部分内容请阅读23-Java-Spring框架(二) 三.Spring Web MVC(Model View Controller) 1.SpringMVC ...

  8. NIO(一) Java NIO 概述

    转:http://ifeve.com/overview/ Java NIO 由以下几个核心部分组成: Channels Buffers Selectors 虽然Java NIO 中除此之外还有很多类和 ...

  9. 如何将项目上传至GitHub?

    心血来潮的一天,突然想写点什么哈哈哈哈. 那就写写如何将项目上传到GitHub(矫情,上传个项目还要写个文章) 第一步:下载Git https://git-scm.com/download/win 下 ...

  10. 按照这些优化技巧来写 SQL,连公司 DBA 也鼓掌称赞!

    原文链接:按照这些优化技巧来写 SQL,连公司 DBA 也鼓掌称赞! 刚毕业的我们,都以为使用 MySQL 是非常的简单的,无非都是照着 [select from where group by ord ...