runtime的黑魔法
要说明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的黑魔法的更多相关文章
- runtime 第四部分method swizzling
接上一篇 http://www.cnblogs.com/ddavidXu/p/5924597.html 转载来源http://www.jianshu.com/p/6b905584f536 http:/ ...
- runtime之消息转发
前言 在上一篇文章中我们初尝了runtime的黑魔法,可以在程序编译阶段就获取到成员变量的名字,特性以及动态的给对象增加属性等等,在接下来中我们进一步了解OC的消息发送机制.如果之前没接触过runti ...
- iOS书写高质量代码之耦合的处理
原创 2016-12-26 MrPeak MrPeak杂货铺 耦合是每个程序员都必须面对的话题,也是容易被忽视的存在,怎么处理耦合关系到我们最后的代码质量.今天Peak君和大家聊聊耦合这个基本功话题, ...
- iOS如何限制使用SDK的版本? 解决iOS项目的版本兼容问题
更新 2015-11-16 感谢微博好友@zyyy_000的评论,补充了为什么要在+ (void)load方法里面做Method Swizzling. 前言 最近,在做项目时,因为某种原因,突然要 ...
- [转]runtime 消息机制
原文地址:http://www.jianshu.com/p/f6300eb3ec3d 一.关于runtime 之前在项目中有遇到过用runtime解决改变全局字体的问题,所以再一次感受到了runtim ...
- iOS开发——高级特性&Runtime运行时特性详解
Runtime运行时特性详解 本文详细整理了 Cocoa 的 Runtime 系统的知识,它使得 Objective-C 如虎添翼,具备了灵活的动态特性,使这门古老的语言焕发生机.主要内容如下: 引言 ...
- Objective-C Runtime 运行时之四:Method Swizzling
理解Method Swizzling是学习runtime机制的一个很好的机会.在此不多做整理,仅翻译由Mattt Thompson发表于nshipster的Method Swizzling一文. Me ...
- iOS开发笔记之Runtime实用总结
前言 runtime的资料网上有很多了,部分有些晦涩难懂,我通过自己的学习方法总结一遍,主要讲一些常用的方法功能,以实用为主,我觉得用到印象才是最深刻的.另外runtime的知识还有很多,想要了解更多 ...
- runtime之玩转成员变量
前言: 不铺垫那么多,单刀直入吧:runtime是一个C和汇编写的动态库,就像是一个小小的系统,将OC和C紧密关联在一次,这个系统主要做两件事情. 1,封装C语言的结构体和函数,让开发者在运行时创建, ...
随机推荐
- ubuntu系统升级记录
之前在openstack中安装了ubuntu 12.04虚拟机,版本较低,需要升级为高版本.下面分享下升级过程: ubuntu系统升级操作:$ cat /etc/issueUbuntu 12.04.5 ...
- 详解CSS中:nth-child的用法
前端的哥们想必都接触过css中一个神奇的玩意,可以轻松选取你想要的标签并给与修改添加样式,是不是很给力,它就是“:nth-child”. 下面我将用几个典型的实例来给大家讲解:nth-child的实际 ...
- Clone Graph leetcode java(DFS and BFS 基础)
题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...
- 在oracle中,如何当前系统时间往前推7天
' day from dual 另附: 当前时间减去7分钟的时间 ' MINUTE from dual 当前时间减去7小时的时间 ' hour from dual 当前时间减去7天的时间 ' day ...
- java IO流复制图片
一.使用字节流复制图片 //字节流方法 public static void copyFile()throws IOException { //1.获取目标路径 //(1)可以通过字符串 // Str ...
- url参数中带有+号,服务器端解码之后没了
解决办法: 客户端:encodeURIComponent 服务器端:Uri.UnescapeDataString 参考网址:http://www.cnblogs.com/artwl/archive/2 ...
- easyui 汇总
1. easyui datagrid 表格组件列属性 formatter columns:{ { field:' product', title:'商品', align:'center', width ...
- [转]Java compiler level does not match解决方法
查看链接:http://jingyan.baidu.com/article/95c9d20da3ec5fec4e756186.html
- java之线程
java之线程 一:线程: 线程是什么呢?线程,有时被称为轻量级进程是程序执行流的最小单元.一个标准的线程由线程ID,当前指令指针(PC),寄存器集合和堆栈组成.另外,线程是进程中的一个实体,是被系统 ...
- apache 多网站日志配置禁止ip访问
#禁止IP访问服务器AcceptFilter http noneAcceptFilter https none<VirtualHost 192.168.1.220>ServerName 1 ...