iOS 对象转化为字典

来源:互联网 发布:wps官方下载 mac版 编辑:程序博客网 时间:2024/04/28 22:39

在实际开发的过程中,很多时候我们需要上传数据,代码中我们用对象处理数据,上传我们就需要把对象转化为字典

//对象转化为字典

-(NSMutableDictionary *)onereturnToDictionaryWithModel:(XXXXX *)model

{

   NSMutableDictionary *userDic = [NSMutableDictionarydictionary];

    unsignedint count =0;

    objc_property_t *properties =class_copyPropertyList([XXXXX class], &count);

    for (int i =0; i < count; i++) {

        constchar *name =property_getName(properties[i]);

        NSString *propertyName = [NSStringstringWithUTF8String:name];

        id propertyValue = [modelvalueForKey:propertyName];

        if (propertyValue) {

            [userDic setObject:propertyValueforKey:propertyName];

        }

    }

    free(properties);

    return userDic;

}