IOS开发 模型赋值 runtime
#import "CZJsonObject.h"
#import <objC/runtime.h>
#import <objc/message.h> NSString *setter_from_key(NSString *key)
{
//capitalise the first letter
NSString *setter = [key stringByReplacingCharactersInRange:NSMakeRange(, ) withString:[[key substringToIndex:] uppercaseString]];
//add 'set' and the colon
setter = [NSString stringWithFormat:@"set%@:", setter];
return setter;
} @implementation CZJsonObject - (void)fitNilInproperty
{
Class _targetClass = [self class];
while ([_targetClass class]!=[NSObject class]) {
[self fitNibWithClass:_targetClass];
_targetClass = [_targetClass superclass];
}
}
/*
* @waring 对于自定义getter和/或setter方法名的属性无效
*/
- (void)fitNibWithClass:(Class)class
{
//传出参数,获取属性的数量
unsigned int outCount = ;
//获取属性列表的副本(所指向的指针)
objc_property_t* ptable = class_copyPropertyList(class, &outCount);//执行后outCount等于类的属性数量
//备份列表指针,便于最后释放
objc_property_t* ptable_first = ptable;
//遍历属性列表
for (unsigned int index = ; index<outCount; index++) {
//获取 当前属性的@encode
const char* attrName = property_getAttributes(*ptable);
NSString* attrNameStr = [NSString stringWithCString:attrName encoding:NSUTF8StringEncoding];
//判断当前属性类型是否为对象
//若@encode以@T开头,则为id型(近似但不等于对象)
//若@encode以@T开头,而且匹配[".+"],则意味着属性为自定义对象类型(NS类包含在内)
if (([attrNameStr hasPrefix:@"T@"]) && ([attrNameStr rangeOfString:@"\".+\"" options:NSRegularExpressionSearch].location!=NSNotFound)) {
//获取当前属性 的 属性名
const char* propertyCName = property_getName(*ptable);
NSString* propertyName = [NSString stringWithCString:propertyCName encoding:NSUTF8StringEncoding];
//获取getter的方法名
NSString* getterName = propertyName;
//检查该getter是否存在,若getter被自定义,则getter名不等于属性名
BOOL isDefaultGetter = [self respondsToSelector:NSSelectorFromString(getterName)];
if (isDefaultGetter) {
//调用该属性的getter,获取属性值
NSObject* propertyValue = [self performSelector:NSSelectorFromString(getterName)];
//当属性值为nil
if (propertyValue==nil||propertyValue==[NSNull null]||propertyValue==NULL) {
//从@encode分离类型信息
NSArray* attrArr = [attrNameStr componentsSeparatedByString:@"\""];
//取出属性类型名
NSString* propertyTypeClassName = [attrArr objectAtIndex:1];
//根据类型名获取默认值
NSObject* defaultValue = [self defaultValueWithClassType:propertyTypeClassName];
//同上,检查该getter是否为默认的setter
BOOL isDefaultSetter = [self respondsToSelector:NSSelectorFromString(setter_from_key(propertyName))];
if (isDefaultSetter) {
//调用setter,将默认值传人
[self performSelector:NSSelectorFromString(setter_from_key(propertyName)) withObject:defaultValue];
}
}
}
}
//列表偏移到下一位属性
ptable++;
}
//遍历完成后,释放属性列表副本
free(ptable_first);
}
- (id)defaultValueWithClassType:(NSString*)type
{
id result = [[NSObject alloc]init];
if ([type hasPrefix:@"NS"]) {
if ([type isEqualToString:@"NSString"]) {
result = @"";//[[NSString alloc]init];
}else if ([type isEqualToString:@"NSNumber"]) {
result = @;//[[NSNumber alloc]init];
}else if ([type isEqualToString:@"NSDictionary"]) {
result = [[NSDictionary alloc]init];
}else if ([type isEqualToString:@"NSArray"]) {
result = [[NSArray alloc]init];
}else if ([type isEqualToString:@"NSMutableDictionary"]) {
result = [[NSMutableDictionary alloc]initWithCapacity:];
}else if ([type isEqualToString:@"NSMutableArray"]) {
result = [[NSMutableArray alloc]initWithCapacity:];
}else if ([type isEqualToString:@"NSDate"]) {
result = [[NSDate alloc]initWithTimeIntervalSince1970:];
}else if ([type isEqualToString:@"NSData"]) {
result = [[NSData alloc]initWithBase64EncodedString:@" " options:NSDataBase64DecodingIgnoreUnknownCharacters];
}
}
return result;
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
if ([key isEqualToString:@"id"]) {
_theId = value;
}else if([key isEqualToString:@"description"]){
_theDescription = value;
}else{
//NSLog(@"%@ underfine the property: @property(nonatomic,readwrite) %@ %@; = %@",[self class],[value class],key,value);
}
}
IOS开发 模型赋值 runtime的更多相关文章
- iOS开发之使用Runtime给Model类赋值
本篇博客算是给网络缓存打个基础吧,本篇博客先给出简单也是最容易使用的把字典转成实体类的方法,然后在给出如何使用Runtime来给Model实体类赋值.本篇博客会介绍一部分,主要是字典的key与Mode ...
- iOS开发小技巧 - runtime适配字体
iOS开发小技巧 - runtime适配字体 版权声明:本文为博主原创文章,未经博主允许不得转载,有问题可联系博主Email: liuyongjiesail@icloud.com 一个iOS开发项目无 ...
- iOS开发——高级篇——Runtime实际应用
前言 本篇主要介绍Runtime在开发中的一些使用场景,顺便讲解了下MJExtension的底层实现 一.runtime简介 RunTime简称运行时.OC就是运行时机制,也就是在运行时候的一些机制, ...
- iOS开发——高级特性&Runtime运行时特性详解
Runtime运行时特性详解 本文详细整理了 Cocoa 的 Runtime 系统的知识,它使得 Objective-C 如虎添翼,具备了灵活的动态特性,使这门古老的语言焕发生机.主要内容如下: 引言 ...
- iOS开发笔记之Runtime实用总结
前言 runtime的资料网上有很多了,部分有些晦涩难懂,我通过自己的学习方法总结一遍,主要讲一些常用的方法功能,以实用为主,我觉得用到印象才是最深刻的.另外runtime的知识还有很多,想要了解更多 ...
- IOS开发中关于runtime的认识
首先要知道我们写的代码在程序运行过程中都会被转化成runtime的C代码执行. runtime突出的一点就是OC中消息传递机制的应用.objc_msgsend(target,SEL); 首先我们先看一 ...
- iOS开发——高级技术精选OC篇&Runtime之字典转模型实战
Runtime之字典转模型实战 如果您还不知道什么是runtime,那么请先看看这几篇文章: http://www.cnblogs.com/iCocos/p/4734687.html http://w ...
- iOS开发之遍历Model类的属性并完善使用Runtime给Model类赋值
在上篇博客<iOS开发之使用Runtime给Model类赋值>中介绍了如何使用运行时在实体类的基类中添加给实体类的属性赋值的方法,这个方法的前提是字典的Key必须和实体类的Property ...
- 我的iOS开发系列博文
之前目录性的总结了发表过的关于OC方面的文章,今天在目录性的总结一下有关iOS开发的文章.走过路过不要错过哦,今天的博文也全都是干货.写技术博客与大家交流一下思想也是不错的. 下面是我的技术博客中有关 ...
随机推荐
- 不是技术牛人,如何拿到国内IT巨头的Offer(转载)
转载的文章,中间有几段需要去学习. byvoid 面阿里星计划的面试结果截图泄漏,引起无数IT屌丝的羡慕敬仰.看看这些牛人,NOI金牌,开源社区名人,三年级开始写Basic-在跪拜之余我们不禁要想,和 ...
- mvc view-controller mvc annotation-driven
1.mvc view-controller 使页面直接通过某个连接跳转,不进过mvc handler 需要加一个配置 <mvc : view-controller path="/suc ...
- 标准盒模型与IE盒模型之间的转换
首先上图,这两张很明显可以看出IE盒模型和标准盒模型之间的差别. 当然今天不是去细细追究两种模型具体是怎么去计算布局的,那个很多文章已经已经有过了,不再重复.以前刚开始学习盒模型的时候,就学到的是IE ...
- PNG图片压缩工具
https://tinypng.com/ 效果非常不错. 340k的图能压缩到140k左右. 视觉效果差距不大
- APP测试点总结
1.功能性测试: ——根据产品需求文档编写测试用例. ——软件设计文档编写用例. 注意:就是根据产品需求文档编写测试用例而进行测试.2.兼容性测试: ——android版本的兼容性 ——手机分辨率兼容 ...
- HtmlAgilityPack下载开启压缩的页面乱码
当一个被采集的网页是开启压缩了的话,如果使用HtmlAgilityPack 的HtmlWeb默认配置去下载,下载回来的HTML代码是乱码,应该进行如下操作 HtmlWeb web = new Html ...
- Ms sql将首字母大写
--辅助表 create table a ( a int ) declare @b int begin insert into a values(@b) end; go --表数据 ),id int) ...
- PIC32MZ tutorial -- Blinky LED
Today I finish the "Blinky LED" application on PIC32MZ starter kit. This application let L ...
- 41. 这张表lbdeveloplog可以查看各个对象的提交情况,包括是哪个IP提交的
lbdeveloplog 这张表lbdeveloplog可以查看各个对象的提交情况,包括是哪个IP提交的
- MAXIMO-IBM文件夹的笔记
MAXIMO--IBM整套文件的目录结构及常用的文件说明: applications文件装的maximo的最重要的文件,包括ear包等发布文件. 进入applications文件夹里,maximo文件 ...