首页 > 其他 > 详细

model转字典

时间:2015-07-01 06:24:22      阅读:291      评论:0      收藏:0      [点我收藏+]

+ (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;  

}


model转字典

原文:http://5828666.blog.51cto.com/5818666/1669522

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!