Automatically populate all properties of an Objective C instance with a dictionary

☆樱花仙子☆ 提交于 2019-12-06 06:02:58

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

ok unless I'm missing something why not create a method like:

setObjectProperties: (id) Object withValues:(NSDictionary *)dict

I've never done this, but this seems like it should work:

For each key in the dictonary:

NSString* setMethod = "set" + [key capitalizedString] + ":";  // This is pseudo-code.
SEL setSelector = NSSelectorFromString(setMethod);
[objectInstance performSelector:setSelector withObject:[dict objectForKey:key]];

(The code to form setMethod is pseudo-code & left as an exercise because Obj-C string manipulation is horrible.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!