用字典自动填充Objective C实例的所有属性 [英] Automatically populate all properties of an Objective C instance with a dictionary

查看:76
本文介绍了用字典自动填充Objective C实例的所有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一类具有要从字典中设置值的属性.

I have a class with properties that I would like to set values from a dictionary.

换句话说,我想使它自动化:

In other words, I would like to automate this:

objectInstace.val1 = [dict objectForKey:@"val1"];
objectInstace.val2 = [dict objectForKey:@"val2"];

具有类似这样的内容(伪代码):

with something like this (pseudo code):

for key, value in dict:
    setattr(objectInstance, key, value)

推荐答案

您可以使用键值编码方法

You can use the Key-Value Coding method setValuesForKeysWithDictionary:. It does precisely what you want. Just [someObject setValuesForKeysWithDictionary:propertiesDictionary].

这篇关于用字典自动填充Objective C实例的所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆