Runtime-iOS的黑魔法,还是很好玩的,消息机制、方法替换简单记录了一点,持续更新....

1.方法替换

在类load方法中,替换系统方法

+ (void)load{

    Method oldColorMethod = class_getInstanceMethod([self class], @selector(setBackgroundColor:));

    Method newColorMethod = class_getInstanceMethod([self class], @selector(xg_setBackgroundColor:));
///交换方法
///调用系统的setBackgroundColor方法就会执行xg_setBackgroundColor方法
method_exchangeImplementations(oldColorMethod, newColorMethod);
}
- (void)xg_setBackgroundColor:(UIColor *)color{
///在此方法中不要调用系统的setBackgroundColor方法,防止循环引用
[self xg_setBackgroundColor:color];
}

2.动态生成属性

- (void)setNamexg:(NSString *)namexg{
objc_setAssociatedObject(self, "namexg", namexg, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSString *)namexg{
return objc_getAssociatedObject(self, "namexg");
}

3.字典转模型的实现

#import "NSObject+xgDictionary.h"
#import <objc/runtime.h> @implementation NSObject (xgDictionary)
+ (instancetype)xg_modelKeyValues:(NSDictionary *)dicData{
id model = [[self alloc]init];
unsigned int count = ;
///返回类的所有属性和变量
Ivar *ivarsA = class_copyIvarList(self, &count);
for (int i = ; i < count; i ++) {
Ivar iv = ivarsA[i];
NSString *ivarName = [NSString stringWithUTF8String:ivar_getName(iv)];
ivarName = [ivarName substringFromIndex:];
id value = dicData[ivarName];
[model setValue:value forKeyPath:ivarName];
}
return model;
}
@end

iOS-Runtime的那些事...编辑中....的更多相关文章

  1. iOS Runtime原理及使用

    runtime简介 因为Objc是一门动态语言,所以它总是想办法把一些决定工作从编译连接推迟到运行时.也就是说只有编译器是不够的,还需要一个运行时系统 (runtime system) 来执行编译后的 ...

  2. ios runtime的相关知识

    一.iOS runtime原理 对于runtime机制,在网上找到的资料大概就是怎么去用这些东西,以及查看runtime.h头文件中的实现,当然这确实是一种很好的学习方法,但是,其实我们还是不会知道r ...

  3. iOS Runtime 实践(1)

    很多时候我们都在看iOS开发中的黑魔法——Runtime.懂很多,但如何实践却少有人提及.本文便是iOS Runtime的实践第一篇. WebView 我们这次的实践主题,是使用针对接口编程的方式,借 ...

  4. 包建强的培训课程(11):iOS Runtime实战

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  5. iOS Runtime的消息转发机制

    前面我们已经讲解Runtime的基本概念和基本使用,如果大家对Runtime机制不是很了解,可以先看一下以前的博客,会对理解这篇博客有所帮助!!! Runtime基本概念:https://www.cn ...

  6. Xamarin iOS教程之显示和编辑文本

    Xamarin iOS教程之显示和编辑文本 Xamarin iOS显示和编辑文本 在一个应用程序中,文字是非常重要的.它就是这些不会说话的设备的嘴巴.通过这些文字,可以很清楚的指定这些应用程序要表达的 ...

  7. iOS runtime探究(二): 从runtime開始深入理解OC消息转发机制

    你要知道的runtime都在这里 转载请注明出处 http://blog.csdn.net/u014205968/article/details/67639289 本文主要解说runtime相关知识, ...

  8. IOS runtime动态运行时二

    在C#.Java中有编译时多态和运行时多态,在OC中,只有运行时的多态,这与它的运行机制有关.OC中,方法的调用是通过消息的传递来进行的.在IOS runtime动态运行时一http://www.cn ...

  9. iOS --runtime理解

    iOS~runtime理解 Runtime是想要做好iOS开发,或者说是真正的深刻的掌握OC这门语言所必需理解的东西.最近在学习Runtime,有自己的一些心得,整理如下,一为 查阅方便二为 或许能给 ...

随机推荐

  1. JavaSE(三)之static、final、abstract修饰符

    一.static修饰符 1.1.static变量 在类中,使用static修饰的成员变量,就是静态变量,反之为非静态变量. 静态变量和非静态变量的区别            静态变量属于类的,&quo ...

  2. ZOJ 1403&&HDU 1015 Safecracker【暴力】

    Safecracker Time Limit: 2 Seconds      Memory Limit: 65536 KB === Op tech briefing, 2002/11/02 06:42 ...

  3. BZOJ 1002: [FJOI2007]轮状病毒【生成树的计数与基尔霍夫矩阵简单讲解+高精度】

    1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 5577  Solved: 3031[Submit][Statu ...

  4. AtCoder Regular Contest 075

    任意门 C - Bugged 题意:类似装箱问题,但是最后体积总和不能为10的倍数. #include<cstdio> #include<cstring> #include&l ...

  5. HDU 2563 统计问题(递归,思维题)

    统计问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  6. UVa 11461 - Square Numbers【数学,暴力】

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  7. HDU--2021

    发工资咯:) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  8. .26-浅析webpack源码之事件流make(1)

    compilation事件流中,依然只是针对细节步骤做事件流注入,代码流程如图: // apply => this-compilation // apply => compilation ...

  9. 炫酷线条动画--svg

    我们经常可以在一些页面看到看起来很酷的线条动画,有些可以用css实现,有些css就无能为力了,今天来研究另一种实现方式,svg 如果对svg是什么还不了解的话,可以先去看看svg的基础教程: 一般对于 ...

  10. SQL的各种连接(cross join、inner join、full join)的用法理解

    SQL中的连接可以分为内连接,外连接,以及交叉连接 . 1. 交叉连接CROSS JOIN 如果不带WHERE条件子句,它将会返回被连接的两个表的笛卡尔积,返回结果的行数等于两个表行数的乘积: 举例, ...