ios runtime部分事例方法说明
一.场景--动态改变变量
unsigned int count = ;
Ivar *ivar = class_copyIvarList([self.person class], &count);
for (int i = ; i<count; i++) {
Ivar var = ivar[i];
const char *varName = ivar_getName(var);
NSString *proname = [NSString stringWithUTF8String:varName]; if ([proname isEqualToString:@"_name"]) { //这里别忘了给属性加下划线
object_setIvar(self.person, var, @"daming");
break;
}
}
NSLog(@"XiaoMing change name is %@",self.person.name);
self.textfield.text = self.self.person.name;
方法说明:
1.Ivar *class_copyIvarList(Class cls, unsigned int *outCount)
用法:返回类的所有属性和变量
参数:class 类型
outCount 类型的数目
返回:Ivar 指针 当做组来用
拓展:class_copyPropertyList 返回对象类的属性(@property申明的属性)
2.const char *ivar_getName(Ivar v)
用法: 返回实例变量的名称。
拓展:ivar_getOffset: 返回实例变量的偏移量。
ivar_getTypeEncoding:返回实例变量的类型字符串。
3. void object_setIvar(id obj, Ivar ivar, id value)
用法:修改类型的变量
参数:obj 所选类
ivar 实例变量
value 更改变量
拓展:id object_getIvar(id obj, Ivar ivar) 获取类实例的变量
二、场景--添加方法
class_addMethod([self.person class], @selector(guess), (IMP)guessAnswer, "v@:");
if ([self.person respondsToSelector:@selector(guess)]) {
//Method method = class_getInstanceMethod([self.xiaoMing class], @selector(guess));
[self.person performSelector:@selector(guess)]; } else{
NSLog(@"Sorry,I don't know");
}
self.textview.text = @"beijing";
1.BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types) //Adds a new method to a class with a given name and implementation.
用法:为类添加一个方法
参数:class 类
SEL 方法选择器 相当于名字
IMP 方法指针
types 描述方法参数类型的字符数组
说明:sel与方法指针名字可以不同 一个是名字 一个是指针地址
三、场景--动态方法交换
self.person = [Person new];
NSLog(@"%@",_person.sayName);
NSLog(@"%@",_person.saySex);
Method m1 = class_getInstanceMethod([self.person class], @selector(sayName));
Method m2 = class_getInstanceMethod([self.person class], @selector(saySex));
method_exchangeImplementations(m1, m2);
1.Method class_getInstanceMethod(Class cls, SEL name)
用法:获取某类的方法(Method类)
参数:class 类
SEL 方法选择器
2.void method_exchangeImplementations(Method m1, Method m2)
用法:交换方法
参数:Method 方法
四、场景--替换方法
//这里也可以使用 [self.person class],不过要先初始化
Method m1 = class_getInstanceMethod([Person class], @selector(sayName));
Method m2 = class_getInstanceMethod([Tool class], @selector(changeMethod)); method_exchangeImplementations(m1, m2);
用法说过了,嗯!
五:场景--方法上增加额外功能
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class selfClass = [self class];
SEL oriSEL = @selector(sendAction:to:forEvent:);
Method oriMethod = class_getInstanceMethod(selfClass, oriSEL);
SEL cusSEL = @selector(mySendAction:to:forEvent:);
Method cusMethod = class_getInstanceMethod(selfClass, cusSEL);
BOOL addSucc = class_addMethod(selfClass, oriSEL, method_getImplementation(cusMethod), method_getTypeEncoding(cusMethod));
if (addSucc) {
class_replaceMethod(selfClass, cusSEL, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
}else {
method_exchangeImplementations(oriMethod, cusMethod);
}
});
}
- (void)mySendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
[[Tool sharedManager] addCount];
[self mySendAction:action to:target forEvent:event];
}
说明:在程序运行时,Runtime会将所有的Class和Category加载到内存中,这时,会调用类的load方法,通知我们Class或Category已经被加载到内存中。
代码逻辑:交换了方法 ,执行逻辑就改变了 :mySendAction触发->再次调用转到原有sendAction方法
1. IMP method_getImplementation(Method m)
用法:获取IMP
拓展:const char *method_getTypeEncoding(Method m) 获取说明
SEL method_getName(Method m) 获取name(SEL)
2.IMP class_replaceMethod(Class cls, SEL name, IMP imp, const char *types)
用法:Replaces the implementation of a method for a given class.
注意:注销 free(ivars);
参考资料来源:https://github.com/Tuccuay/RuntimeSummary
ios runtime部分事例方法说明的更多相关文章
- iOS项目之交换方法(runtime)
在项目中,经常会遇到系统自带的方法满足不了自己的需求,往往我们解决这种情况的时候,都是在分类中添加一个方法.然而很多时候,项目已经开发很长时间了,如果一个一个的去替换系统的方法,太浪费宝贵的时间,所以 ...
- ios runtime的相关知识
一.iOS runtime原理 对于runtime机制,在网上找到的资料大概就是怎么去用这些东西,以及查看runtime.h头文件中的实现,当然这确实是一种很好的学习方法,但是,其实我们还是不会知道r ...
- iOS Runtime的消息转发机制
前面我们已经讲解Runtime的基本概念和基本使用,如果大家对Runtime机制不是很了解,可以先看一下以前的博客,会对理解这篇博客有所帮助!!! Runtime基本概念:https://www.cn ...
- iOS Runtime 实操练习
iOS Runtime 知识详解: http://yulingtianxia.com/blog/2014/11/05/objective-c-runtime/ 一般可以运行Runtime进行以下操作 ...
- iOS runtime探究(二): 从runtime開始深入理解OC消息转发机制
你要知道的runtime都在这里 转载请注明出处 http://blog.csdn.net/u014205968/article/details/67639289 本文主要解说runtime相关知识, ...
- IOS runtime动态运行时二
在C#.Java中有编译时多态和运行时多态,在OC中,只有运行时的多态,这与它的运行机制有关.OC中,方法的调用是通过消息的传递来进行的.在IOS runtime动态运行时一http://www.cn ...
- iOS --runtime理解
iOS~runtime理解 Runtime是想要做好iOS开发,或者说是真正的深刻的掌握OC这门语言所必需理解的东西.最近在学习Runtime,有自己的一些心得,整理如下,一为 查阅方便二为 或许能给 ...
- iOS-提高iOS开发效率的方法和工具
提高iOS开发效率的方法和工具 介绍 这篇文章主要是介绍一下我在iOS开发中使用到的一些可以提升开发效率的方法和工具. IDE 首先要说的肯定是IDE了,说到IDE,Xcode不能跑,当然你也可能同时 ...
- iOS runtime的理解和应用
项目中经常会有一些的功能模块用到runtime,最近也在学习它.对于要不要阅读runtime的源码,我觉得仅仅是处理正常的开发,那真的没有必要,只要把常用的一些函数看下和原理理解下就可以了. 但是如果 ...
随机推荐
- 第八篇 一个用JS写的省市县三级联动
前些天,做网站用需要用到一个省市县的三级联动,数据要从数据库里面读取,我想了下思路,动手写了下来. 一.思路 js利用Ajax读取控制器里面的函数,利用函数读取存储过程,返回 ...
- GridView应用随笔
1. 数据绑定 GridView可以使用数据源控件和设置控件的DataSource属性来绑定数据,这里主要讲设置DataSource属性来绑定. 1.写一个返回值为DataSet或者DataTable ...
- CSS揭秘 技巧(五):条纹背景
条纹背景 https://github.com/FannieGirl/ifannie/问题:条纹背景 在设觉设计中无处不在,我们真的可以用css 创建图案吗? 这一章相对还是比较复杂的哦!一起get. ...
- HttpServletRequest 各种方法总结
HttpServletRequest HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,开发人员通过这个对象 ...
- java properties 对list的支持
经测试,原生的properties 对 list 不支持. 参考样例如下: id=1 id=2 具体代码如下: java.util.Properties prop = new Properties() ...
- @Autowired 注释对在哪里和如何完成自动连接提供了更多的细微的控制。
1.@Autowired 可以用在多个地方,在 setter 方法上,属性上 或者 带有多个参数的任意方法上. Setter 方法中的 @Autowired. 当 Spring遇到一个在 setter ...
- Robot Framework 使用笔记
条件表达式: Run Keyword If 表达式 执行动作 ... ELSE IF 表达式 执行动作 ... ELSE 执行动作 基础格式见上表,下面是我遇到的坑: 表达式:判断字符串变量是 ...
- 在windows上缓存git 密码
缓存git密码 一搜索 大部分都是在linux上的 . git config --global credential.helper cache 但在windows上pull或者push会报如下错误: ...
- php学习测试题目
<?php header("content-type:text/html;charset=utf-8"); /* 1.银行给客户每天万分之四的利率,本金10 ...
- ###服务(Service)
Start服务开启方式 1) 创建服务 public class MyService extends Service 2) 添加注册表 <service android:name=&qu ...