要说明runtime,首先要明白objc这门语言,是基于C的封装.真是因为runtime,objc才有了面对对象的特性.

也就说,所有objc的语言,在运行时都会转换成C.

也是基于这样的特性,runtime可以做很多不可思议的事情.比如动态添加成员变量,函数等等.

User *usr = [[User alloc] init];

    NSLog(@"%@",usr.description);

    unsigned int count = ;

    Ivar *members = class_copyIvarList([User class] , &count);

    for (int i = ; i<count ; i++) {

        Ivar var = members[i];

        const char *memName = ivar_getName(var);

        const char *memType = ivar_getTypeEncoding(var);

        NSLog(@"%s type:%s", memName, memType);

    }

    //值修改
Ivar name = members[]; object_setIvar(usr, name, @"王三蛋"); NSLog(@"object_setIvar修改成员变量Name后的值:%@",usr.description); class_addMethod([User class], @selector(method:), (IMP)addingFunc, "i@:i@"); //通过class_addMethod 动态添加函数 unsigned int FuncCount = ; Method *membersFuncs = class_copyMethodList([User class], &FuncCount); //遍历已有函数名
for (int i = ; i< FuncCount; i++) { SEL Met = method_getName(membersFuncs[i]); NSString *MetName = [NSString stringWithCString:sel_getName(Met) encoding:NSUTF8StringEncoding]; NSLog(@"User的函数:%@",MetName); } objc_msgSend(usr,@selector(method:),@"动态添加的函数");
int addingFunc(id self,SEL _cmd,NSString *str)
{
NSLog(@"Added Func,%@",str); return ;
}

给出一个model转换的

//
// NSObject+ModelHandle.h
// MVVM
//
// Created by M on 16/3/3.
// Copyright © 2016年 Meng. All rights reserved.
// #import <Foundation/Foundation.h> @interface NSObject (ModelHandle) +(instancetype)ModelWithDict:(NSDictionary*)dict; @end
//
// NSObject+ModelHandle.m
// MVVM
//
// Created by M on 16/3/3.
// Copyright © 2016年 Meng. All rights reserved.
// #import "NSObject+ModelHandle.h"
#import <objc/runtime.h> @implementation NSObject (ModelHandle) +(instancetype)ModelWithDict:(NSDictionary*)dict
{
id obj = [self new]; for (NSString *propertyName in [self GetProperList]) { if (dict[propertyName]) { [obj setValue:dict[propertyName] forKey:propertyName]; } } return obj;
} +(NSArray*)GetProperList
{
unsigned int count = ; objc_property_t *propertyList = class_copyPropertyList([self class], &count);//获取class里的属性. NSMutableArray *arr = [NSMutableArray array]; for (int i = ; i<count; i++) { objc_property_t property = propertyList[i]; const char *CharName = property_getName(property); NSString *StrName = [[NSString alloc] initWithUTF8String:CharName]; [arr addObject:StrName]; } free(propertyList);//释放 return arr.copy;
} @end

直接在相应的model的头文件里引入 #import "NSObject+ModelHandle.h"

Model *m = [Model  ModelWithDict:Dict[@"result"]];

相比 setValuesForKeysWithDictionary, 此函数对数据的检查不是那么严格.

runtime的黑魔法的更多相关文章

  1. runtime 第四部分method swizzling

    接上一篇 http://www.cnblogs.com/ddavidXu/p/5924597.html 转载来源http://www.jianshu.com/p/6b905584f536 http:/ ...

  2. runtime之消息转发

    前言 在上一篇文章中我们初尝了runtime的黑魔法,可以在程序编译阶段就获取到成员变量的名字,特性以及动态的给对象增加属性等等,在接下来中我们进一步了解OC的消息发送机制.如果之前没接触过runti ...

  3. iOS书写高质量代码之耦合的处理

    原创 2016-12-26 MrPeak MrPeak杂货铺 耦合是每个程序员都必须面对的话题,也是容易被忽视的存在,怎么处理耦合关系到我们最后的代码质量.今天Peak君和大家聊聊耦合这个基本功话题, ...

  4. iOS如何限制使用SDK的版本? 解决iOS项目的版本兼容问题

      更新 2015-11-16 感谢微博好友@zyyy_000的评论,补充了为什么要在+ (void)load方法里面做Method Swizzling. 前言 最近,在做项目时,因为某种原因,突然要 ...

  5. [转]runtime 消息机制

    原文地址:http://www.jianshu.com/p/f6300eb3ec3d 一.关于runtime 之前在项目中有遇到过用runtime解决改变全局字体的问题,所以再一次感受到了runtim ...

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

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

  7. Objective-C Runtime 运行时之四:Method Swizzling

    理解Method Swizzling是学习runtime机制的一个很好的机会.在此不多做整理,仅翻译由Mattt Thompson发表于nshipster的Method Swizzling一文. Me ...

  8. iOS开发笔记之Runtime实用总结

    前言 runtime的资料网上有很多了,部分有些晦涩难懂,我通过自己的学习方法总结一遍,主要讲一些常用的方法功能,以实用为主,我觉得用到印象才是最深刻的.另外runtime的知识还有很多,想要了解更多 ...

  9. runtime之玩转成员变量

    前言: 不铺垫那么多,单刀直入吧:runtime是一个C和汇编写的动态库,就像是一个小小的系统,将OC和C紧密关联在一次,这个系统主要做两件事情. 1,封装C语言的结构体和函数,让开发者在运行时创建, ...

随机推荐

  1. 为普通Object添加类似AttachedProperty的属性

    为普通Object添加类似AttachedProperty的属性   周银辉 我们知道,在WPF中对应一个DependencyObject,我们很容易通过AttachedProperty来为类型附加一 ...

  2. node基础13:异步流程控制

    1.流程控制 因为在node中大部分的api都是异步的,比如说读取文件,如果采用回调函数的形式,很容易造成地狱回调,代码非常不容易进行维护. 因此,为了解决这个问题,有大神写了async这个中间件.极 ...

  3. VirtualBox装ghost XP

    在win7 professional 64上安装了virtualBox4.3.14 r95030 版本,之所以要安装这个vb,是因为刚升级的vm 打开之后很占用cpu, 网上又说vb不是很占用cpu而 ...

  4. vs2010集成git指南

    1.安装 Git Extensions  下载地址:http://gotgit.github.com/gotgithub/10-appendix/030-install-on-windows-cygw ...

  5. AppBox升级进行时 - 如何向OrderBy传递字符串参数(Entity Framework)

    AppBox 是基于 FineUI 的通用权限管理框架,包括用户管理.职称管理.部门管理.角色管理.角色权限管理等模块. Entity Framework提供的排序功能 再来回顾一下上篇文章,加载用户 ...

  6. sql server cross/outer apply 用法

    这是 sql server 帮助文档关于apply的描述: 使用 APPLY 运算符(2005或以上版本)可以为实现查询操作的外部表表达式返回的每个行调用表值函数.表值函数作为右输入,外部表表达式作为 ...

  7. Dribbble for windows phone 8

    正如你看到文章的标题所示.这是一个Dribbble 基于windows phone 8的客户端.[开源项目] 对于大部分的开发人员来说很少关注Dribbble[不妨打开看看或是注册一个player账号 ...

  8. 使用jquery脚本获取随笔、文章和评论的统计数,自定义显示位置

    为了这个问题,花了好些时间去摸索,无奈没有搞定.于是,我就到博问去提问,终于搞定! 在此,非常感谢SeayXu的热心帮助. 1.在需要的位置添加一个标签 <div id="stats_ ...

  9. git Bash常用命令

    1.Construct ssh key (If you want to commit to git server via THIS COMPUTER) git config --global user ...

  10. Spring IoC容器的初始化过程

    Spring IoC容器的初始化包括 BeanDefinition的Resource定位.载入和注册 这三个基本的过程.IoC容器的初始化过程不包含Bean依赖注入的实现.Bean依赖的注入一般会发生 ...