iOS用runtime给一个类动态添加方法 ---class_addMethod
先介绍下class_addMethod这个fangfa
- /**
- * Adds a new method to a class with a given name and implementation.
- *
- * @param cls The class to which to add a method.
- * @param name A selector that specifies the name of the method being added.
- * @param imp A function which is the implementation of the new method. The function must take at least two arguments—self and _cmd.
- * @param types An array of characters that describe the types of the arguments to the method.
- *
- * @return YES if the method was added successfully, otherwise NO
- * (for example, the class already contains a method implementation with that name).
- *
- * @note class_addMethod will add an override of a superclass's implementation,
- * but will not replace an existing implementation in this class.
- * To change an existing implementation, use method_setImplementation.
- */
- OBJC_EXPORT BOOL class_addMethod(Class cls, SEL name, IMP imp,
- const char *types)
- OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);
这是文档
描述就是:给一个类添加一个新的方法和该方法的具体实现
参数:
第一个
Class cls
cls :你要添加新方法的那个类
第二个
SEL name
name :要添加的方法
第三个
IMP imp
imp:指向实现方法的指针 就是要添加的方法的实现部分
第四个
const char *types
*types:我们要添加的方法的返回值和参数 叫 type encodings 这个东西怎么写我就不说了 可以去查 在这里我有一个懒办法得到它不用暴力手打 往后看
然后这个方法的描述和参数说完了,下面说说他用在哪,
给一个类用运行时添加一个方法 他可以用来解决我们开发中的这个问题------“unrecognized selector sent to instance 0x7faa2a132c0”
在OC中我们调用一个方法时 有个关键字 叫 “消息机制” 就是执行 [receiver message]; 这句代码其实就是发送了一个消息 (发消息的详细过程我就不写了 感兴趣 去查),
消息里面带着你调用这个方法的名称和参数以及是哪个对象发送的消息,然后 发出消息后 如果有对应的方法响应,就执行方法,没有的话就报“unrecognized selector sent to instance 0x7faa2a132c0” ,然后就crash ,为了防止crash我们可以用class_addMethod给找不到对应方法即将crash的消息添加一个与之对应的方法来防止其因为找不到相应方法而crash
在OC中找不到对相应的实现方法时 有补救机制 即 会先调用动态决议方法 该方法解决不了问题 再调用重定向方法 都解决不了问题是 crash
动态决议方法:
- + (BOOL)resolveClassMethod:(SEL)sel OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);
- + (BOOL)resolveInstanceMethod:(SEL)sel OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);
重定向方法:
- (id)forwardingTargetForSelector:(SEL)aSelector OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);
下面上代码:给一个类的对象调用一个未实现的方法 然后用runtime 在动态决议方法中为其添加实现 防止crash
Person类
- #import "Person.h"
- #import <objc/runtime.h>
- @implementation Person
- + (BOOL)resolveInstanceMethod:(SEL)sel {
- Method exchangeM = class_getInstanceMethod([self class], @selector(eatWithPersonName:));
- class_addMethod([self class], sel, class_getMethodImplementation(self, @selector(eatWithPersonName:)),method_getTypeEncoding(exchangeM));
- return YES;
- }
- - (void)eatWithPersonName:(NSString *)name {
- NSLog(@"Person %@ start eat ",name);
- }
- @end
里面重写resolveInstanceMethod方法 给找不到实现的方法 添加实现 - (void)eatWithPersonName:(NSString *)name;
第一句
Method exchangeM = class_getInstanceMethod([self class], @selector(eatWithPersonName:));
是为下面method_getTypeEncoding(exchangeM) 做铺垫 这样就拿到了eatWithPersonName方法的type encodings 用作class_addMethod的第四个参数,就是上面我提到的不用暴力手打的获取type encodings的方法
然后调用 class_addMethod 为当前类[self class] 的sel 方法 添加实现 class_getMethodImplementation(self, @selector(eatWithPersonName:)) 这一串就是拿到方法eatWithPersonName的IMP指针 然后最后一个参数是 type encodings
然后测试
- Person *person = [[Person alloc]init];
- //调用Person未实现方法eat
- [person performSelector:@selector(eat) withObject:@"minzhe"];
运行结果:
2017-07-14 14:41:35.297 RunTimeAddMethod[1458:379458] Person minzhe start eat
好了 添加上了 这样就没有 crash
iOS用runtime给一个类动态添加方法 ---class_addMethod的更多相关文章
- ios开发runtime学习三:动态添加方法(实际应用少,面试)
#import "ViewController.h" #import "Person.h" /* 1: Runtime(动态添加方法):OC都是懒加载机制,只要 ...
- 使用runtime给类动态添加方法并调用 - class_addMethod
上手开发 iOS 一段时间后,我发现并不能只着眼于完成需求,利用闲暇之余多研究其他的开发技巧,才能在有限时间内提升自己水平.当然,“其他开发技巧”这个命题对于任何一个开发领域都感觉不找边际,而对于我来 ...
- ios开发runtime学习四:动态添加属性
#import "ViewController.h" #import "Person.h" #import "NSObject+Property.h& ...
- 给python类动态添加方法(method)
群里有人问如何做到 def foo(): pass class Bar(object): pass Bar.set_instance_method(foo) b = Bar() b.foo() 这个其 ...
- ios的runtime为什么可以动态添加方法
一句话概括 每个instance都有一个isa,这个isa,里面含有所有的方法列表,ios提供库函数增加,修改,即实现了动态添加方法
- 使用TypeDescriptor给类动态添加Attribute
给类动态添加Attribute一直是我想要解决的问题,从msdn里找了很久,到Stack Overflow看了不少文章,算是最终有了答案. 先是有这样的一段解释 Attributes are stat ...
- iOS运行时使用(动态添加方法)
1 举例 我们实现一个Person类 然后Person 其实是没得对象方法eat:的 下面调用person的eat方法 程序是会奔溃的 那么需要借助运行时动态的添加方法 Person *p = [[ ...
- python装饰器、继承、元类、mixin,四种給类动态添加类属性和方法的方式(一)
介绍装饰器.继承.元类.mixin,四种給类动态添加类属性和方法的方式 有时候需要給类添加额外的东西,有些东西很频繁,每个类都需要,如果不想反复的复制粘贴到每个类,可以动态添加. # coding=u ...
- 快速上手Runtime(四)之动态添加方法
如果一个类方法非常多,加载类到内存的时候也比较耗费资源,可以使用动态给某个类,添加方法解决.做到优化内存,节省资源的效果. // // Person.m // ResolveInstanceMetho ...
- php里面用魔术方法和匿名函数闭包函数动态的给类里面添加方法
1.认识 __set (在给不可访问属性赋值时,__set() 会被调用) 也就是说你再访问一个类里面没有的属性,会出发这个方法 class A{ private $aa = '11'; publ ...
随机推荐
- java方法的笔记
方法 方法的概念 方法(method)是将具有独立功能的代码块组织成为一个整体,使其具有特殊功能的代码集 注意: 方法必须先创建才可以使用,该过程成为方法定义 方法创建后并不是直接可以运行的,需要手动 ...
- 搭建Redis高可用集群的哨兵模式(Redis-Sentinel)【Windows环境】
参考 https://blog.csdn.net/itanping/article/details/100544152 哨兵模式搭建好,Java中配置和使用Redis高可用集群的哨兵模式,引入Jedi ...
- [笔记] 什么是Groovy?什么是Kotlin?
关于Groovy 参考文章 https://www.w3cschool.cn/groovy/ 摘抄如下: Groovy是JVM的一个替代语言(替代是指可以用 Groovy 在Java平台上进行 Jav ...
- 36.201——LTE物理层——总体描述物理层综述协议
主要包括物理层在协议结构中的位置和功能,包括物理层4个规范36.211.36.212.36.213.36.214的主要内容和相互关系等 The radio interface is composed ...
- python自动化模块及运维工具
1. psutil 可以获取系统运行的进程和系统利用率(CPU 内存-)信息 import psutil 2. IPy 是python 第三方处理IP地址模块 from IPy import IP 3 ...
- join => innerJoin
drupal7中 ->join == ->innerJoin 不是leftJoin哦 无语哦
- lua-table面向对象
--使用table封装面向对象beauty={name = " "}--封装对象方法function beauty.init(self, name)print("十八年前 ...
- 正则表达式re.compile()的使用
re 模块提供了不少有用的函数,用以匹配字符串,比如: compile 函数match 函数search 函数findall 函数finditer 函数split 函数sub 函数subn 函数re ...
- unity3d Time.deltaTime个人理解
官方的解释是:静态只读属性,时间增量,渲染上一帧所花费的时间看下面的代码 /// <summary> /// 每帧刷新 /// </summary> void Update() ...
- C# 笔记--Sendkeys winform窗体控件回车及全选
SendKeys.Send() 向活动应用程序发送击键 SendKeys.SendWait() 向活动应用程序发送给定的键,然后等待消息被处理 这两个方法可以发送组合键,需要注意的是字母按键应为小写. ...