将对象转换成字典
来源:互联网 发布:windows.open 返回值 编辑:程序博客网 时间:2024/05/17 20:00
#import <objc/runtime.h>
// 通过对象返回一个NSDictionary ,键是属性名称 ,值是属性值
+(NSDictionary *)getObjectData:(id)obj
{
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
unsigned int propsCount;
objc_property_t *props =class_copyPropertyList([obj class], &propsCount);
for (int i = 0; i < propsCount; i++) {
objc_property_t prop = props[i];
NSString *propName = [NSString stringWithUTF8String:property_getName(prop)];
id value = [obj valueForKey:propName];
if (value == nil) {
value = [NSNull null];
}else
{
value = [self getObjectInternal:value];
}
[dic setObject:value forKey:propName];
}
return dic;
}
+(id)getObjectInternal:(id)obj
{
if ([obj isKindOfClass:[NSString class]]||[obj isKindOfClass:[NSNumber class]]||[obj isKindOfClass:[NSNull class]]) {
return obj;
}
if ([obj isKindOfClass:[NSArray class]]) {
NSArray *objarr = obj;
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:objarr.count];
for (int i= 0; i< objarr.count; i++) {
[arr setObject:[self getObjectInternal:[objarr objectAtIndex:i]] atIndexedSubscript:i];
}
return arr;
}
if ([obj isKindOfClass:[NSDictionary class]]) {
NSDictionary *objdic = obj;
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:[objdic count]];
for(NSString *key in objdic.allKeys)
{
[dic setObject:[self getObjectInternal:[objdic objectForKey:key]] forKey:key];
}
return dic;
}
return [self getObjectData:obj];
}
0 0
- 将对象转换成字典
- 扩展将对象转换成字典
- IOS将字典转换成模型对象
- iOS 对象转换成字典
- django QuerySet对象转换成字典对象
- django QuerySet对象转换成字典对象
- 将数据实体转换成字典集合
- swift 将json字符串 转换成 字典
- 将Java对象转换成JSON对象
- 将Clob对象转换成String对象
- 将CoreFoudation对象转换成OC对象
- 将单个对象转换成JSON对象
- python如何将字符串转换成字典dict类型
- iOS 将字典 数组 转换成 NSdata 或者 NSString
- iOS将字典转换成http的参数字符串
- 使用JSONKit将字符串,字典,数组转换成json格式
- 将NSData(JSON数据)类型转换成字典
- python 使用 simplejson 将字符串转换成字典dict
- winApplication与winConsoleApplication
- Difference between frame and bounds? frame和bounds有什么不同?
- adb命令查看sharedpreferences
- 从JDK源码角度看并发锁的优化
- 自定义的HorizontalScrollView与Viewpager组成tab之间的滑动
- 将对象转换成字典
- Android学习笔记二十九:Android Socket编程介绍(示例,出错代码)
- iOS学习之——状态栏
- 打印ARP表
- 人数不定的工资类
- 判断客户机中Word是否存在
- mtk8127 bt sco 路径
- Jquery可移动的DIV
- Ibatis 中<![[ CDATA[ ]]>意思