IOS Dictionary和Model相互转换
- //
- // HYBJSONModel.h
- // Json2ModelDemo
- //
- // Created by huangyibiao on 14-9-15.
- // Copyright (c) 2014年 Home. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- /*!
- * @brief JSON转换成Model,或者把Model转换成JSON
- * @author huangyibiao
- */
- @interface HYBJSONModel : NSObject
- /*!
- * @brief 把对象(Model)转换成字典
- * @param model 模型对象
- * @return 返回字典
- */
- + (NSDictionary *)dictionaryWithModel:(id)model;
- /*!
- * @brief 获取Model的所有属性名称
- * @param model 模型对象
- * @return 返回模型中的所有属性值
- */
- + (NSArray *)propertiesInModel:(id)model;
- /*!
- * @brief 把字典转换成模型,模型类名为className
- * @param dict 字典对象
- * @param className 类名
- * @return 返回数据模型对象
- */
- + (id)modelWithDict:(NSDictionary *)dict className:(NSString *)className;
- @end
- //
- // HYBJSONModel.m
- // Json2ModelDemo
- //
- // Created by huangyibiao on 14-9-15.
- // Copyright (c) 2014年 Home. All rights reserved.
- //
- #import "HYBJSONModel.h"
- #import <objc/runtime.h>
- typedef NS_ENUM(NSInteger, HYBJSONModelDataType) {
- kHYBJSONModelDataTypeObject = ,
- kHYBJSONModelDataTypeBOOL = ,
- kHYBJSONModelDataTypeInteger = ,
- kHYBJSONModelDataTypeFloat = ,
- kHYBJSONModelDataTypeDouble = ,
- kHYBJSONModelDataTypeLong = ,
- };
- @implementation HYBJSONModel
- /*!
- * @brief 把对象(Model)转换成字典
- * @param model 模型对象
- * @return 返回字典
- */
- + (NSDictionary *)dictionaryWithModel:(id)model {
- if (model == nil) {
- return nil;
- }
- NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
- // 获取类名/根据类名获取类对象
- NSString *className = NSStringFromClass([model class]);
- id classObject = objc_getClass([className UTF8String]);
- // 获取所有属性
- unsigned int count = ;
- objc_property_t *properties = class_copyPropertyList(classObject, &count);
- // 遍历所有属性
- for (int i = ; i < count; i++) {
- // 取得属性
- objc_property_t property = properties[i];
- // 取得属性名
- NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property)
- encoding:NSUTF8StringEncoding];
- // 取得属性值
- id propertyValue = nil;
- id valueObject = [model valueForKey:propertyName];
- if ([valueObject isKindOfClass:[NSDictionary class]]) {
- propertyValue = [NSDictionary dictionaryWithDictionary:valueObject];
- } else if ([valueObject isKindOfClass:[NSArray class]]) {
- propertyValue = [NSArray arrayWithArray:valueObject];
- } else {
- propertyValue = [NSString stringWithFormat:@"%@", [model valueForKey:propertyName]];
- }
- [dict setObject:propertyValue forKey:propertyName];
- }
- return [dict copy];
- }
- /*!
- * @brief 获取Model的所有属性名称
- * @param model 模型对象
- * @return 返回模型中的所有属性值
- */
- + (NSArray *)propertiesInModel:(id)model {
- if (model == nil) {
- return nil;
- }
- NSMutableArray *propertiesArray = [[NSMutableArray alloc] init];
- NSString *className = NSStringFromClass([model class]);
- id classObject = objc_getClass([className UTF8String]);
- unsigned int count = ;
- objc_property_t *properties = class_copyPropertyList(classObject, &count);
- for (int i = ; i < count; i++) {
- // 取得属性名
- objc_property_t property = properties[i];
- NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property)
- encoding:NSUTF8StringEncoding];
- [propertiesArray addObject:propertyName];
- }
- return [propertiesArray copy];
- }
- /*!
- * @brief 把字典转换成模型,模型类名为className
- * @param dict 字典对象
- * @param className 类名
- * @return 返回数据模型对象
- */
- + (id)modelWithDict:(NSDictionary *)dict className:(NSString *)className {
- if (dict == nil || className == nil || className.length == ) {
- return nil;
- }
- id model = [[NSClassFromString(className) alloc]init];
- // 取得类对象
- id classObject = objc_getClass([className UTF8String]);
- unsigned int count = ;
- objc_property_t *properties = class_copyPropertyList(classObject, &count);
- Ivar *ivars = class_copyIvarList(classObject, nil);
- for (int i = ; i < count; i ++) {
- // 取得成员名
- NSString *memberName = [NSString stringWithUTF8String:ivar_getName(ivars[i])];
- const char *type = ivar_getTypeEncoding(ivars[i]);
- NSString *dataType = [NSString stringWithCString:type encoding:NSUTF8StringEncoding];
- NSLog(@"Data %@ type: %@",memberName,dataType);
- HYBJSONModelDataType rtype = kHYBJSONModelDataTypeObject;
- if ([dataType hasPrefix:@"c"]) {
- rtype = kHYBJSONModelDataTypeBOOL;// BOOL
- } else if ([dataType hasPrefix:@"i"]) {
- rtype = kHYBJSONModelDataTypeInteger;// int
- } else if ([dataType hasPrefix:@"f"]) {
- rtype = kHYBJSONModelDataTypeFloat;// float
- } else if ([dataType hasPrefix:@"d"]) {
- rtype = kHYBJSONModelDataTypeDouble; // double
- } else if ([dataType hasPrefix:@"l"]) {
- rtype = kHYBJSONModelDataTypeLong;// long
- }
- for (int j = ; j < count; j++) {
- objc_property_t property = properties[j];
- NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property)
- encoding:NSUTF8StringEncoding];
- NSRange range = [memberName rangeOfString:propertyName];
- if (range.location == NSNotFound) {
- continue;
- } else {
- id propertyValue = [dict objectForKey:propertyName];
- switch (rtype) {
- case kHYBJSONModelDataTypeBOOL: {
- BOOL temp = [[NSString stringWithFormat:@"%@", propertyValue] boolValue];
- propertyValue = [NSNumber numberWithBool:temp];
- }
- break;
- case kHYBJSONModelDataTypeInteger: {
- int temp = [[NSString stringWithFormat:@"%@", propertyValue] intValue];
- propertyValue = [NSNumber numberWithInt:temp];
- }
- break;
- case kHYBJSONModelDataTypeFloat: {
- float temp = [[NSString stringWithFormat:@"%@", propertyValue] floatValue];
- propertyValue = [NSNumber numberWithFloat:temp];
- }
- break;
- case kHYBJSONModelDataTypeDouble: {
- double temp = [[NSString stringWithFormat:@"%@", propertyValue] doubleValue];
- propertyValue = [NSNumber numberWithDouble:temp];
- }
- break;
- case kHYBJSONModelDataTypeLong: {
- long long temp = [[NSString stringWithFormat:@"%@",propertyValue] longLongValue];
- propertyValue = [NSNumber numberWithLongLong:temp];
- }
- break;
- default:
- break;
- }
- [model setValue:propertyValue forKey:memberName];
- break;
- }
- }
- }
- return model;
- }
- @end
IOS Dictionary和Model相互转换的更多相关文章
- 【AutoMapper官方文档】DTO与Domin Model相互转换(下)
写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...
- ios中常用数据类型相互转换
ios中常用数据类型相互转换 //1. NSMutableArray和NSArray互转 // NSArray转为NSMutableArray NSMutableArray *arrM = [arr ...
- 【AutoMapper官方文档】DTO与Domin Model相互转换(上)
写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...
- 【AutoMapper官方文档】DTO与Domin Model相互转换(中)
写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...
- iOS 字典与JSON相互转换
iOS 字典与JSON相互转换 首先简单说一下为什么会写这种幼稚的文章. 现在的网络请求几乎都是AFN完成的,AFN也为我们写了了JSON转换字典的方法,但是不要忘记后台是一个很爱用JSON的人群,H ...
- iOS标准时间与时间戳相互转换
iOS标准时间与时间戳相互转换 (2012-07-18 17:03:34) 转载▼ 标签: ios 时间戳 标准时间 格式 设置 转化 杂谈 分类: iPhone开发 设置时间显示格式: NS ...
- 判断iOS系统的Model
获取iOS系统的Model (参考网址:https://www.theiphonewiki.com/wiki/Models) + (NSString *)getModel{ struct utsn ...
- C# 类型转换 Dictionary转Model类
/// <summary> /// 把Model转换为DataRow /// </summary> /// <typeparam name="T"&g ...
- Dictionary转为Model实例
Dictionary<string, object> dic = new Dictionary<string, object>(); dic.Add(); dic.Add(&q ...
随机推荐
- 用VS2005写一个 C 的类库和用 C#来调用的示例
一.用VS2005写一个 C 的类库的步骤: (1).建立一个空的Visual C++项目 (2).这时候在项目中可以看见 三个空目录 选中 "源文件" 目录,然后点鼠标右键,在弹 ...
- aix7安装was7、打补丁、更改访问端口、手动启动was、配置was7、部署项目
1:准备工作 首先了解下我们下面即将用到的aix命令,以及安装包.补丁安装工具.补丁 was7的安装包以及补丁工具都是压缩包形式并且以.tar.gz结尾的 安装包在800MB左右,通常为****_w ...
- js获取屏幕(设备)宽高
平常获取设备的宽高无非就那几 <script language="javascript"> var h = ""; h += " 网页可见 ...
- AspNet WebApi : MessageHandler(消息处理器 )
1. Http Message Handler WebApi中的MessageHandler类似MVC中的filter,可用于请求/响应到达真正目标前对请求或者响应进行修改,比如:用户身份验证,请求头 ...
- 那些年被我坑过的Python——不得不知(第二章)
问题一: Python3.5.X中的数据类型有哪些? 答:包括整型.布尔型.字符串型.浮点型.复数.列表.字典.集合.元组. 细化来说: 1.整型包括短整型和长整型,不过我们不必过度操心细节,因为短整 ...
- Vim C/C++的一键编译
开始用Vim差不多有两个月的时间, 一开始用Makefile 编译一整个项目无压力, 但是当写到单个文件的时候, 编译就比较麻烦了, 每次都得 :w :!gcc -o 1.exe 1.c :!1 非常 ...
- 转载:.NET Web开发技术简单整理
在最初学习一些编程语言.一些编程技术的时候,做的更多的是如何使用该技术,如何更好的使用该技术解决问题,而没有去关注它的相关性.关注它的理论支持,这种学习技术的方式是短平快.其实工作中有时候也是这样,公 ...
- ECMAScript 6 模块简介
任何平台的其中一个重要特性,除了需要支持代码库外就是模块.直到现在,Javascript还不支持原生的模块化.结果是,各种解决方案都将模块添加到类库中,比如CommonJS modules(部分由no ...
- BZOJ 1501 智慧珠游戏
Description Input 文件中包含初始的盘件描述,一共有10行,第i行有i个字符.如果第i行的第j个字符是字母”A”至”L”中的一个,则表示第i行第j列的格子上已经放了零件,零件的编号为对 ...
- [BZOJ 1055] [HAOI2008] 玩具取名 【记忆化搜索】
题目链接:BZOJ - 1055 题目分析 这种类似区间 DP 的记忆化搜索都是很相近的,比如字符串压缩和字符串扩展都差不多. 都是将现在 Solve 的区间分成子区间,再求解子区间. 这道题 Sol ...