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 ...
随机推荐
- CCF 201909-2 小明种苹果(续)
#include <iostream> #include <bits/stdc++.h> #include <string> using namespace std ...
- Python的入门学习之Day 9——from“夜曲编程”
Day 9 time: 2021.8.6. 昨天复习了第一章"数据与运算".第二章"条件判断"的相关重点,今天就感觉得心应手了.今天上新的内容是关于新一章节&q ...
- mybatisplus根据json字段查询
参考:https://blog.csdn.net/m0_73311735/article/details/126869623
- JBOSS环境与应用部署
- 403 forbidden 与 413Too Large
http://www.ccschy.com/shuma/12846.html https://blog.51cto.com/u_15127556/4543159 查的有关资料如下,最后的原因是服务器网 ...
- EhLib 9安装
EhLib 9安装 安装EhLib: 在Installer文件夹里找到EhLibInstaller.exe,右键以管理员权限运行EhLibInstaller.exe. 按照提示一路安装下来,顺利完成. ...
- vagrant搭建centos7
准备工作 下载安装vagrant https://releases.hashicorp.com/vagrant/2.3.4/vagrant_2.3.4_windows_amd64.msi 选择自己需要 ...
- Wix打包升级包
前面我们已经知道怎么制作一个完整安装包了,但我们的软件往往不能一次性就满足客户的需要,当客户需要我们给软件进行升级的时候,我们应该怎么做呢? 在这之前,我们有必要了解下Windows Installe ...
- 深入理解 epoll 原理
从网卡如何接收数据说起 CPU 如何知道接受了数据? 进程阻塞为什么不占用 CPU 资源? 工作队列 等待队列 唤醒进程 内核接收网络数据全过程 同时监视多个 socket 的方法 select 的监 ...
- Java script Date和长整型互换
document.write(new Date().getTime()); document.write('<br/>') var date1=new Date(1590024428000 ...