runtime是oc的真面目。oc底层的一套c语言API.

 unsigned int count;
//获取属性列表
objc_property_t *propertyList = class_copyPropertyList([self class], &count);
for (unsigned int i=; i<count; i++) {
const char *propertyName = property_getName(propertyList[i]);
NSLog(@"property---->%@", [NSString stringWithUTF8String:propertyName]);
} //获取方法列表
Method *methodList = class_copyMethodList([self class], &count);
for (unsigned int i; i<count; i++) {
Method method = methodList[i];
NSLog(@"method---->%@", NSStringFromSelector(method_getName(method)));
} //获取成员变量列表
Ivar *ivarList = class_copyIvarList([self class], &count);
for (unsigned int i; i<count; i++) {
Ivar myIvar = ivarList[i];
const char *ivarName = ivar_getName(myIvar);
NSLog(@"Ivar---->%@", [NSString stringWithUTF8String:ivarName]);
} //获取协议列表
__unsafe_unretained Protocol **protocolList = class_copyProtocolList([self class], &count);
for (unsigned int i; i<count; i++) {
Protocol *myProtocal = protocolList[i];
const char *protocolName = protocol_getName(myProtocal);
NSLog(@"protocol---->%@", [NSString stringWithUTF8String:protocolName]);
}

应用1:KVC字典转模型

Class clazz = Person.class;
unsigned int count = ; Person *person = [[Person alloc]init];
NSDictionary *dict = @{@"name":@"zhangsan",@"age":@, @"height": @1.75}; Ivar *ivars = class_copyIvarList(clazz, &count);
for (int i = ; i < count; i++) {
const char *cname = ivar_getName(ivars[i]);
NSString *name = [NSString stringWithUTF8String:cname];
NSString *key = [name substringFromIndex:]; const char *coding = ivar_getTypeEncoding(ivars[i]); // 获取类型
NSString *strCode = [NSString stringWithUTF8String:coding];
id value = dict[key];
if ([strCode isEqualToString:@"f"]) {// 判断类型是否是float
value = @(0.0);
} [person setValue:value forKey:key];
}
NSLog(@"%@", person);

应用2:NSCoding归档和解档

- (void)encodeWithCoder:(NSCoder *)aCoder {
unsigned int count = ;
Ivar *ivars = class_copyIvarList(self.class, &count);
for (int i = ; i < count; i++) {
const char *cname = ivar_getName(ivars[i]);
NSString *name = [NSString stringWithUTF8String:cname];
NSString *key = [name substringFromIndex:]; id value = [self valueForKey:key]; // 取出key对应的value
[aCoder encodeObject:value forKey:key]; // 编码
}
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super init]) {
unsigned int count = ;
Ivar *ivars = class_copyIvarList(self.class, &count);
for (int i = ; i < count; i++) {
const char *cname = ivar_getName(ivars[i]);
NSString *name = [NSString stringWithUTF8String:cname];
NSString *key = [name substringFromIndex:]; id value = [aDecoder decodeObjectForKey:key]; // 解码
[self setValue:value forKey:key]; // 设置key对应的value
}
}
return self;
}

其他应用场景:

交换方法实现,

类\对象的关联对象,

动态添加方法,拦截未实现的方法

动态创建一个类

objective-c底层: runtime机制的更多相关文章

  1. 刨根问底Objective-C Runtime

    http://chun.tips/blog/2014/11/05/bao-gen-wen-di-objective%5Bnil%5Dc-runtime-(2)%5Bnil%5D-object-and- ...

  2. Objective-C的对象模型和runtime机制

    内容列表 对象模型(结构定义,类对象.元类和实例对象的关系) 消息传递和转发机制 runtime系统功能理解 对象模型 结构定义 对象(Object): OC中基本构造单元 (building blo ...

  3. iOS开发之深入探讨runtime机制02-runtime的简单使用

    runtime机制为我们提供了一系列的方法让我们可以在程序运行时动态修改类.对象中的所有属性.方法. 下面就介绍运行时一种很常见的使用方式,字典转模型.当然,你可能会说,“我用KVO直接 setVal ...

  4. iOS开发之深入探讨runtime机制01-类与对象

    最近有个同事问我关于“runtime机制”的问题,我想可能很多人对这个都不是太清楚,在这里,和大家分享一下我对于runtime机制的理解.要深入理解runtime,首先要从最基本的类与对象开始,本文将 ...

  5. <转>ASP.NET学习笔记之理解MVC底层运行机制

    ASP.NET MVC架构与实战系列之一:理解MVC底层运行机制 今天,我将开启一个崭新的话题:ASP.NET MVC框架的探讨.首先,我们回顾一下ASP.NET Web Form技术与ASP.NET ...

  6. 【Spark 深入学习 04】再说Spark底层运行机制

    本节内容 · spark底层执行机制 · 细说RDD构建过程 · Job Stage的划分算法 · Task最佳计算位置算法 一.spark底层执行机制 对于Spark底层的运行原理,找到了一副很好的 ...

  7. .net core系列之《从源码对Configuration的底层运行机制进行分析》

    通过对Configuration源代码的分析从而来自定义一个配置数据源 1.用反编译工具来看看AddJsonFile()这个方法究竟干了什么,源代码如下: public static IConfigu ...

  8. ReentrantLock的底层实现机制 AQS

    ReentrantLock的底层实现机制是AQS(Abstract Queued Synchronizer 抽象队列同步器).AQS没有锁之类的概念,它有个state变量,是个int类型,为了好理解, ...

  9. Runtime机制的使用整理

    一.基本概念 1.1.RunTime简称运行时,就是系统在运行的时候的一些机制,其中最主要的是消息机制. 1.2.对于C语言,函数的调用在编译的时候会决定调用哪个函数,编译完成之后直接顺序执行,无任何 ...

随机推荐

  1. 超级有用的9个PHP代码片段

    在开发网站.app或博客时,代码片段可以真正地为你节省时间.今天,我们就来分享一下我收集的一些超级有用的PHP代码片段.一起来看一看吧! 1.创建数据URI 数据URI在嵌入图像到HTML / CSS ...

  2. wordpress 常用函数 checked(),selected(),disabled()

    checked().selected().disabled(),这三个函数是主题设计和插件设计中添加后台设置比较常用到的函数. 例如自定义一个widget,这个widget有一个字段为文章排列方式.文 ...

  3. Oracle 删除重复行

    DELETE FROM your_table WHERE rowid not in (SELECT MIN(rowid) FROM your_table GROUP BY column1, colum ...

  4. laravel ajax表格删除

    view和jq @extends('layouts.main') @section('content') <h3>User List</h3> <p class=&quo ...

  5. Environment Variables

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms682653(v=vs.85).aspx Every process has an ...

  6. ArcGIS Server发布服务,报错001270

    错误001270 这个问题一般是因为数据源文件太大导致. 解决办法:  对于001270的错误,官方帮助中给出了一些可能的原因并提供了相应的解决办法(http://resources.arcgis.c ...

  7. [daily][device] linux挂载iphone

    头几个月去旅游,亲戚的iphone照了好多照片,空间不足.就备份在了我的电脑上. 那么问题就是如何在linux系统里挂载iphone? 我找到了这篇文档,然而我没看. https://wiki.arc ...

  8. js判断url是否有效

    方法一:(仅适用于ie) function CheckStatus(url) { XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP") ...

  9. Oozie协作框架

    一:概述 1.大数据协作框架 2.Hadoop的任务调度 3.Oozie的三大功能 Oozie Workflow jobs Oozie Coordinator jobs Oozie Bundle 4. ...

  10. oracle 表查询一

    通过scott用户下的表来演示如何使用select语句,接下来对emp.dept.salgrade表结构进行解说. emp 雇员表字段名称   数据类型       是否为空   备注-------- ...