工作中没怎么用到runtime的东西,所以一直没怎么看,现在开始拿起来。

runtime之方法的交换:

都知道OC中有category可以对已知类进行扩展,但是假如工程中需要修改某类的原方法,若用category的话,调用的时候会调用到category中实现的方法,而原方法中的功能就已经被覆盖,这样就调用不到系统的方法了,为了避免这种情况,我们可以用方法交换的形式:
如写一个NSURL的分类,现在将它的URLWithString:的方法替换成我们自己写的方法:

+ (void)load{
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    Method urlWithStringMethod = class_getClassMethod(self, @selector(URLWithString:));

    Method wd_urlWithStringMethod = class_getClassMethod(self, @selector(WD_urlWithString:));

    /*交换了URLWithString和WD_urlWithString的函数指针(该指针指向方法的实现),所以交换了之后,若调用URLWithString:方法,实际上是调用了WD_urlWithString方法的实现,而调用了WD_urlWithString:方法,实际上的调用了URLWithString:方法的实现*/

    method_exchangeImplementations(urlWithStringMethod, wd_urlWithStringMethod);
  });
}

/*
在load中将NSURL中的URLWithString方法跟自定义的WD_urlWithString进行了交换
在外部调用系统的URLWithString:方法,实际上会到WD_urlWithString:中
*/
+ (instancetype)WD_urlWithString:(NSString *)urlString{
  NSURL *url = [NSURL WD_urlWithString:urlString]; //实际调用的URLWithString:方法的实现,所以不会造成无限循环
  if (url == nil) {
    NSLog(@"url is null");
  }
  return url;
}

method:

实际上method是一个结构体,

struct objc_method {
  SEL method_name OBJC2_UNAVAILABLE;
  char *method_types OBJC2_UNAVAILABLE;
  IMP method_imp OBJC2_UNAVAILABLE;
}

method_name 是方法的 selector,可以理解为运行时的方法名;
*method_types 是一个参数和返回值类型编码的字符串;
method_imp 是指向方法实现的指针。
Method Swizzling 的实质是在运行时,访问对象的方法结构体,并改变它的底层实现。

拿上面的例子来说,变化如下:

交换前->

method URLWithString {
  SEL method_name = @selector(URLWithString:)
  char *method_types = “v@:“ //返回值void, 参数id(self),selector(_cmd)
  IMP method_imp = 0x000FFFF //指向([NSURL URLWithString:])
}
method WD_urlWithString {
  SEL method_name = @selector(WD_urlWithString:)
  char *method_types = “v@:“ //返回值void, 参数id(self),selector(_cmd)
  IMP method_imp = 0x000EEEE //指向([NSURL WD_urlWithString:])
}
交换后->
method URLWithString {
  SEL method_name = @selector(URLWithString:)
  char *method_types = “v@:“ //返回值void, 参数id(self),selector(_cmd)
  IMP method_imp = 0x000FFFF //指向([NSURL WD_urlWithString:])
}
method WD_urlWithString {
  SEL method_name = @selector(WD_urlWithString:)
  char *method_types = “v@:“ //返回值void, 参数id(self),selector(_cmd)
  IMP method_imp = 0x000EEEE //指向([NSURL URLWithString:])
}
可以看到使用method_exchangeImplementations实质是交换它们的IMP。
Method Swizzling,我们可以对系统中的方法进行修改,动态添加。

在load中处理,因为 +load 方法会在类被添加到 OC 运行时执行,保证了 Swizzling 方法的及时处理。

runtime之方法的交换的更多相关文章

  1. ios - runtime运行时应用---交换方法

    runtime运行时用法之一 --- 交换类的方法,此处简单写了把系统的UIView的setBackgroundColor的方法换成了自定义的pb_setBackgroundColor 首先创建UIV ...

  2. Runtime之方法

    前两篇介绍了类与对象.成员变量&属性&关联对象的相关知识,本篇我们将开始讲解Runtime中最有意思的一部分内容:消息处理机制.我们从一个示例开始. 在OC中,我们使用下面这种方式来调 ...

  3. Java Runtime.availableProcessors()方法

    Java Runtime.availableProcessors()方法用法实例教程.   描述 java.lang.Runtime.availableProcessors() 方法返回到Java虚拟 ...

  4. Linux系统实现虚拟内存有两种方法:交换分区(swap分区)和交换文件

    Linux系统实现虚拟内存有两种方法:交换分区(swap分区)和交换文件 交换文件 查看内存:free -m , -m是显示单位为MB,-g单位GB 创建一个文件:touch /root/swapfi ...

  5. Mac下 eclipse target runtime com.genuitec.runtime 解决方法

    Mac下 eclipse target runtime com.genuitec.runtime 解决方法 解决步骤如下: 首先是找到工程项目一个名叫.settings的文件夹,里面有个叫 org.e ...

  6. runtime 实现方法交换 viewwillappear方法

    1.新建分类 #import "UIViewController+swizzling.h"#import <objc/runtime.h> @implementatio ...

  7. Runtime之方法交换

    在没有一个类的实现源码的情况下,想改变其中一个方法的实现,除了继承它重写.和借助类别重名方法暴力抢先之外,还有就是方法交换 方法交换的原理:在OC中调用一个方法其实是向一个对象发送消息,查找消息的唯一 ...

  8. [runtime] initialize方法讲解

    + (void)initializeDescription(描述)    Initializes the class before it receives its first message. 在这个 ...

  9. Objective-C 方法交换实践(二) - 方法指针交换

    一. 基本函数 根据 sel 得到 class 的实例方法 Method class_getInstanceMethod(Class cls, SEL name) 根据 sel 得到 class 的函 ...

随机推荐

  1. 设置USB数据监听

    设置USB数据监听   在Kali Linux中,USB也是作为一个通信端口进行存在.常见的鼠标.键盘.U盘都是通过USB接口传输数据.所以,对于USB接口也可以实施监听,类似网络接口一样.在进行US ...

  2. springboot 启动类启动跳转到前端网页404问题的两个解决方案

    前段时间研究springboot 发现使用Application类启动的话, 可以进入Controller方法并且返回数据,但是不能跳转到WEB-INF目录下网页, 前置配置 server: port ...

  3. Hystrix的介绍和简单使用

    这周在看项目的相关代码时,接触到了Hystrix,因此查询了相关资料学习了下. 一.什么是Hystrix Hystrix是Netflix针对微服务分布式系统的熔断保护中间件,当我们的客户端连接远程的微 ...

  4. BZOJ3295动态逆序对

    一道比较傻的CDQ分治 CDQ: 主要用于解决三位偏序的问题 #include<cstdio> #include<cctype> #include<algorithm&g ...

  5. Android 自定义ListView Item侧滑删除

    本程序是基于网上开源项目修改而来,具体来源忘了,懒得搜了,如果有不合适的地方,请原作者联系我,我会及时回复和处理的! 该例子程序中主要包含两个ListView,一个是实现侧滑删除,一个是侧滑出菜单,代 ...

  6. 【redis】5.spring boot项目中,直接在spring data jpa的Repository层使用redis +redis注解@Cacheable直接在Repository层使用,报错问题处理Null key returned for cache operation

    spring boot整合redis:http://www.cnblogs.com/sxdcgaq8080/p/8028970.html 首先,明确一下问题的场景 之前在spring boot整合re ...

  7. C# 使用xenocode混淆加密【转】

    http://www.cnblogs.com/chendaoyin/archive/2013/05/03/3056692.html   之前就了解过混淆加密工具,但这还是第一次使用,选择了xenoco ...

  8. php 的$_POST 和$_REQUEST的区别

    发现$_POST 没数据,而$_REQUEST 有数据 ----------------------------------------- https://stackoverflow.com/ques ...

  9. 南阳 oj 表达式求值 题目35 数据结构 NYO题目链接

     建议不会的看别人的代码自己在之上模拟一遍,仅仅要耐心模拟就会做出来 题目链接:http://acm.nyist.net/JudgeOnline/problem.php? pid=35 #incl ...

  10. PC常用电源IC、MOS、三极管、二极管厂家

    笔记本常用MOS.三极管.二极管厂家: 1.EMC 杰力电子(台湾)官方网站:http://www.excelliancemos.com/tw/solution.php 2.UBIQ(台湾电源厂家UP ...