- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我从 Web 服务解析一些 JSON,这给了我一个 NSDictionary,我使用这个字典来填充 NSObject 类型的 valueEntity 上的属性
[myObject setValuesForKeysWithDictionary:JSONDict];
(myObject 与来自 JSON 解析器的字典具有相同的属性名称和类型)
name = name
count = count
startDate = startDate
etc..
有没有另一种方法,我有一个 NSDictionary,我想用我的 NSObject 子类中的属性及其值“填充”它。就像我在标题中建议的那样:
一种方式
MyObject *myObject = [[MyObject alloc] init];
[myObject setValuesForKeysWithDictionary:JSONDict];
反过来
NSMutableDictionary *dict = [myObject makeDictionaryWithObjectProperties];
这样做的原因是我有一个 valueEntity,根据协议(protocol),我的 View 都符合它,但我也想用这些值填充 NSManagedObject。所以我认为使用 NSDictionary 作为中间步骤,我可以绕过必须在我的 NSManagedObject 上做一个类别,该类别根据我的对象子类 NSObject 上的值手动设置每个属性。
有了字典我就可以去:
[myManagedObject setValuesForKeysWithDictionary:dict];
完成上述操作后,我无法取回字典表示吗?
最佳答案
是的,方法是:
- (NSDictionary *)dictionaryWithValuesForKeys:(NSArray *)keys
您传入一个表示您感兴趣的对象属性的键数组,然后返回一个包含属性值的字典。
NSKeyValueCoding
协议(protocol)定义了许多强大且非常有用的方法;熟悉它是一件很棒的事情。
还有一个非常有值(value)的配套指南:
关于objective-c - 是否有反向 "setValuesForKeysWithDictionary"- makeDictionaryWithObjectProperties?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3711722/
我正在使用以下方法初始化 NSManagedObject 子类: - (void)setValuesForKeysWithDictionary:(NSDictionary *)keyedValues
我正在使用 setValuesForKeysWithDictionary 来填充我的模型对象。模型对象定义为 @interface CommonModel : NSObject @proper
所以我有一个 NSDictionary,其中一个键是字典数组。我要映射到的类具有匹配的键名和 setter 。 setValuesForKeysWithDictionary 可以为我填写子词典吗?当我
似乎 Swift 4 正在实现与 (void)setValuesForKeysWithDictionary() 不同的方法,在 Swift 4 中什么是这个函数的合适替代品? 我有的结构 struct
我从 Web 服务解析一些 JSON,这给了我一个 NSDictionary,我使用这个字典来填充 NSObject 类型的 valueEntity 上的属性 [myObject setValuesF
代码如下: func observeMessages() { let ref = FIRDatabase.database().reference().child("messages")
我正在快速尝试基于核心数据的应用程序,其中我正在执行以下步骤: 从 plist 中检索数据 遍历检索到的数据 在每次迭代期间,在 managedObjectContext 中插入一个 managedO
我有一个json字符串 {"name":"test","bar":{"name":"testBar"}} 在 objective-c 中我有一个对象 @interface Foo : NSObject
所以我很高兴地使用 JSON 和 setValuesForKeysWithDictionary 将数据猛击到我的系统中,直到我遇到一个具有 NSDate 属性的对象。 JSON 包含以下格式的日期:
我正在从我的 API 中提取一个 JSON 对象并使用以下代码创建它(事后看来不是正确的方法): + (YActivity *)instanceFromDictionary:(NSDictionary
我有将一些 json 反序列化为对象的代码。此代码在 iPhone 5s、iPhone 6 和 iPhone 6+ 上运行良好。 但是,在 iPhone 5 或 iPhone 4s 上运行时,出现错误
我是一名优秀的程序员,十分优秀!