在 Objective-C 中从 NSDictionary 创建 NSObject
我想使用“for...in”循环将字典中的每个项目分配到我的对象中
我有一个这样的 NSDictionary :
{"user_id" : "1", "user_name" : "John"}
并且我有一个带有变量的 NSObject :
NSString *user_id;
NSString *user_name
使用 for in 循环,我想分配每个项目从我的 NSObject 中的 NSDictionary
for (NSString *param in userDictionary) {
}
我该怎么做?
谢谢,
I would like to assign each item from a dictionary into my object using a "for...in" loop
I have a NSDictionary like that :
{"user_id" : "1", "user_name" : "John"}
And I have an NSObject with variable :
NSString *user_id;
NSString *user_name
With a for in loop, I want to assign each item from my NSDictionary in my NSObject
for (NSString *param in userDictionary) {
}
How I can do that ?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看一下 NSObject 类方法:
例如:
Have a look at the NSObject class method:
For example:
我编写了一个小型库来自动执行此操作,它还处理日期转换
https://github.com/aryaxt/OCMapper
只要所有键都在,它就会自动将 NSDictionary 转换为 NSObject与属性名称相同。
如果键和属性名称不同,您可以编写映射
I wrote a small library that automates this, it also handles conversion of dates
https://github.com/aryaxt/OCMapper
It automatically converts NSDictionary to NSObject as long as all keys are the same as property names.
if the key and property names are not the same you can write mapping