使用字典自动填充Objective C实例的所有属性

gak*_*gak 2 introspection objective-c

我有一个属性的类,我想从字典中设置值.

换句话说,我想自动化这个:

objectInstace.val1 = [dict objectForKey:@"val1"];
objectInstace.val2 = [dict objectForKey:@"val2"];
Run Code Online (Sandbox Code Playgroud)

用这样的东西(伪代码):

for key, value in dict:
    setattr(objectInstance, key, value)
Run Code Online (Sandbox Code Playgroud)

Chu*_*uck 9

您可以使用键值编码方法setValuesForKeysWithDictionary:.它正是你想要的.只是[someObject setValuesForKeysWithDictionary:propertiesDictionary].