model转字典
2015-06-30 19:28
309 查看
+ (NSDictionary *) entityToDictionary:(id)entity
{
Class clazz = [entity class];
u_int count;
objc_property_t* properties = class_copyPropertyList(clazz, &count);
NSMutableArray* propertyArray = [NSMutableArray arrayWithCapacity:count];
NSMutableArray* valueArray = [NSMutableArray arrayWithCapacity:count];
for (int i = 0; i < count ; i++)
{
objc_property_t prop=properties[i];
const char* propertyName = property_getName(prop);
[propertyArray addObject:[NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding]];
// const char* attributeName = property_getAttributes(prop);
// NSLog(@"%@",[NSString stringWithUTF8String:propertyName]);
// NSLog(@"%@",[NSString stringWithUTF8String:attributeName]);
id value = [entity performSelector:NSSelectorFromString([NSString stringWithUTF8String:propertyName])];
if(value ==nil)
[valueArray addObject:[NSNull null]];
else {
[valueArray addObject:value];
}
// NSLog(@"%@",value);
}
free(properties);
NSDictionary* returnDic = [NSDictionary dictionaryWithObjects:valueArray forKeys:propertyArray];
NSLog(@"%@", returnDic);
return returnDic;
}
{
Class clazz = [entity class];
u_int count;
objc_property_t* properties = class_copyPropertyList(clazz, &count);
NSMutableArray* propertyArray = [NSMutableArray arrayWithCapacity:count];
NSMutableArray* valueArray = [NSMutableArray arrayWithCapacity:count];
for (int i = 0; i < count ; i++)
{
objc_property_t prop=properties[i];
const char* propertyName = property_getName(prop);
[propertyArray addObject:[NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding]];
// const char* attributeName = property_getAttributes(prop);
// NSLog(@"%@",[NSString stringWithUTF8String:propertyName]);
// NSLog(@"%@",[NSString stringWithUTF8String:attributeName]);
id value = [entity performSelector:NSSelectorFromString([NSString stringWithUTF8String:propertyName])];
if(value ==nil)
[valueArray addObject:[NSNull null]];
else {
[valueArray addObject:value];
}
// NSLog(@"%@",value);
}
free(properties);
NSDictionary* returnDic = [NSDictionary dictionaryWithObjects:valueArray forKeys:propertyArray];
NSLog(@"%@", returnDic);
return returnDic;
}
相关文章推荐
- 自定义model 转出 字典
- No sources that matches the given name (at 'layout_toLeftOf' with value '@id/
- Java反射应用案例
- 提高mysql千万级大数据SQL查询优化30条经验(Mysql索引优化注意)
- 回溯法之求n个集合的幂集
- Intel MKL 在VS中的配置与安装笔记
- 写一个算法实现在一个整数数组中,找出第二大的那个数字。
- 如何查找最新文献
- Java文件选择对话框(文件选择器JFileChooser)的使用:以一个文件加密器为例
- IT架构之IT架构标准——思维导图
- javascript基础
- iOS7新特性 ViewController转场切换(三) 自定义视图控制器容器的切换---非交互式
- iOS 阶段学习第11天笔记(OC基础知识)
- 如何做好PPT?
- 写一个通用的事件侦听器函数(兼容多浏览器)
- git查看历史与乱码解决
- android 完美退出应用程序。
- 理解SVG的viewport,viewBox,preserveAspectRatio
- Freemarker-2.3.22 Demo - No04_条件判断
- TexturePacker使用