第一次看到runtime时,觉得太高大上,动态获取方法、属性等简直厉害的不要不要的。在经过查找资料+实践后,发现runtime并没有想象中那么复杂,接下来对runtime进行基本的介绍。

  要使用运行时方法需要引入runtime.h文件

  一、基础知识  

  Method :成员方法

  Ivar : 成员变量

  二、常用方法

  class_copyPropertyList : 获取属性列表

  class_copyMethodList : 获取成员方法列表

  class_copyIvarList:获取成员变量列表

  ivar_getName:获取变量名

  property_getName:获取属性名

  使用示例:

  1.获取成员变量列表

//1.获取变量list
unsigned int ivarCount = ; //成员变量数
Ivar *ivarList = class_copyIvarList([self class], &ivarCount);//ivar数组 for (int i = ; i < ivarCount; i++) {//遍历
Ivar ivar = ivarList[i]; //获取ivar
const char *name = ivar_getName(ivar);//获取变量名
NSString *key = [NSString stringWithUTF8String:name];
NSLog(@"%@", key); }       free(ivarList);
 

  2.获取属性列表

unsigned int count = ;
objc_property_t *propertList = class_copyPropertyList([self class], &count);
for (int i = ; i < count; i++) {
objc_property_t property = propertList[i];
const char *name = property_getName(property);
const char *attrs = property_getAttributes(property);
// property_copyAttributeValue(,) 第一个参数为objc_property_t,第二个参数"V"获取变量名,"T"获取类型
const char *value = property_copyAttributeValue(property, "V");
NSLog(@"name = %s, attrs = %s, value = %s", name, attrs, value);
}

    free(propertList);

  3.获取方法列表 

unsigned int count = ;
Method *methodList = class_copyMethodList([self class], &count);
for (int i = ; i < count; i++) {
Method method = methodList[i];
SEL selector = method_getName(method);//方法入口
const char *sel_name = sel_getName(selector);
NSLog(@"方法名 %s", sel_name);
}
free(methodList);

  三、使用方向:归档、字典<---->模型、框架封装等

  实现归档

  

#define WKCodingImplementing \
- (void)encodeWithCoder:(NSCoder *)aCoder \
{ \
unsigned int ivarCount = ; \
Ivar *ivarList = class_copyIvarList([self class], &ivarCount); \
for (int i = ; i < ivarCount; i++) { \
Ivar ivar = ivarList[i]; \
const char *name = ivar_getName(ivar); \
const char *type = ivar_getTypeEncoding(ivar); \
NSLog(@"%s-----%s", name, type); \
NSString *key = [NSString stringWithUTF8String:name]; \
id value = [self valueForKey:key]; \
[aCoder encodeObject:value forKey:key]; \
} \
free(ivarList); \
} \
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder \
{ \
if (self = [super init]) { \
unsigned int ivarCount = ; \
Ivar *ivarList = class_copyIvarList([self class], &ivarCount); \
for (int i = ; i < ivarCount; i++) { \
Ivar ivar = ivarList[i]; \
const char *name = ivar_getName(ivar); \
NSString *key = [NSString stringWithUTF8String:name]; \
NSLog(@"%@ %@", key, value); \
id value = [aDecoder decodeObjectForKey:key]; \
[self setValue:value forKey:key]; \
} \
} \
return self; \
}

 

  

iOS进阶:Objective-C runtime(一)的更多相关文章

  1. iOS开发之使用Runtime给Model类赋值

    本篇博客算是给网络缓存打个基础吧,本篇博客先给出简单也是最容易使用的把字典转成实体类的方法,然后在给出如何使用Runtime来给Model实体类赋值.本篇博客会介绍一部分,主要是字典的key与Mode ...

  2. iOS进阶_地图上定位的标志——大头针

    一.添加大头针 地图使用的框架是MapKit 大头针走的是MKAnnotation协议 /* 注意:因为是满足协议MKAnnotation,所以没有MKAnnotation的系统大头针类,必须自定义大 ...

  3. iOS进阶指南试读之UI篇

    iOS进阶指南试读之UI篇 UI篇 UI是一个iOS开发工程师的基本功.怎么说?UI本质上就是你调用苹果提供给你的API来完成设计师的设计.所以,想提升UI的功力也很简单,没事就看看UIKit里的各个 ...

  4. iOS开发小技巧 - runtime适配字体

    iOS开发小技巧 - runtime适配字体 版权声明:本文为博主原创文章,未经博主允许不得转载,有问题可联系博主Email: liuyongjiesail@icloud.com 一个iOS开发项目无 ...

  5. (转发)IOS高级开发~Runtime(四)

    用C代替OC: #import <objc/runtime.h> #import <objc/message.h> #import <stdio.h> extern ...

  6. (转发)IOS高级开发~Runtime(三)

    11.系统类的方法实现部分替换 - (void) methodExchange { Method m1 = class_getInstanceMethod([NSStringclass],@selec ...

  7. (转发)IOS高级开发~Runtime(二)

    一些公用类: @interface ClassCustomClass :NSObject{ NSString *varTest1; NSString *varTest2; NSString *varT ...

  8. (转发)IOS高级开发~Runtime(一)

    IOS高级开发-Runtime(一) IOS高级开发-Runtime(二) IOS高级开发-Runtime(三) IOS高级开发-Runtime(四) 一些公用类: @interface Custom ...

  9. iOS开发——高级特性&Runtime运行时特性详解

    Runtime运行时特性详解 本文详细整理了 Cocoa 的 Runtime 系统的知识,它使得 Objective-C 如虎添翼,具备了灵活的动态特性,使这门古老的语言焕发生机.主要内容如下: 引言 ...

随机推荐

  1. 【转】zookeeper 的监控工具

            公司很多产品会使用zookeeper,比如Meta消息中间件,在测试的过程中,我们经常需要查询zookeeper里面的信息来精确定位问题.目前项目中有开发团队自己写的浏览器node-z ...

  2. IE下支持文本框和密码框placeholder效果的JQuery插件

    基于jQuery实现的,主要用于IE下实现placeholder效果,可同时支持文本和密码输入框.placeholder是HTML5新增的一个属性,当input设置了该属性后,该值的内容将作为灰色提示 ...

  3. Inno setup complier将文件添加注册表

    [Registry] Root: HKCR; Subkey:.; ValueType: string; ValueName: ; ValueData:"264file" Root: ...

  4. HTML注释简介

    HTML注释简介   在编写HTML代码时,我们经常要在一些关键代码旁做一下注释,这样做的好处很多,比如:方便理解.方便查找或方便项目组里的其它程序员了解你的代码,而且可以方便以后你对自己代码进行修改 ...

  5. js触摸屏案例

    js 手机端触发事事件.javascript手机端/移动端触发事件   处理Touch事件能让你跟踪用户的每一根手指的位置.你可以绑定以下四种Touch事件: 1 2 3 4 touchstart:  ...

  6. 推荐书目 - C++学习资料

    前言 在本文的前半部分我我会谈谈 我看过的书,和我个人的一些理解 ,并且会提供 C++标准委员会相关链接 和 C++第三方轮子/库总结 .本文的后半部分翻译了来自 The Definitive C++ ...

  7. SET STATISTICS IO和SET STATISTICS TIME 在SQL Server查询性能优化中的作用

    近段时间以来,一直在探究SQL Server查询性能的问题,当然也漫无目的的查找了很多资料,也从网上的大神们的文章中学到了很多,在这里,向各位大神致敬.正是受大神们无私奉献精神的影响,所以小弟也作为回 ...

  8. zoj1027 Human Gene Functions

    一道动态规划,两个串进行匹配,不同字母匹配的值不一样,也可以和空格匹配(空格不能与空格匹配),求最大的匹配值. 数据很弱,每个串都在100以内. 定义dp[i][j]为第一个串前i个数和第二个串前j个 ...

  9. Ubuntu14.04如何备份和恢复系统

    清理Ubuntu14.04的系统的垃圾:先清空回收站,软件升级到最新.Ubuntu系统与Windows系统所采用的文件系统不同, Ubuntu系统在使用或更新过程中不会产生文件碎片和垃圾文件,所以在使 ...

  10. perl install module && normal module

    Windows: perl -MCPAN -e shell install  XML::GDOME   Archive::Zip 后面继续更新... Archive usage: $obj = Arc ...