gpt4 book ai didi

objective-c - 是否有反向 "setValuesForKeysWithDictionary"- makeDictionaryWithObjectProperties?

转载 作者:太空狗 更新时间:2023-10-30 03:18:59 26 4
gpt4 key购买 nike

我从 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)定义了许多强大且非常有用的方法;熟悉它是一件很棒的事情。

http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Protocols/NSKeyValueCoding_Protocol/Reference/Reference.html

还有一个非常有值(value)的配套指南:

http://developer.apple.com/library/ios/documentation/cocoa/Conceptual/KeyValueCoding/KeyValueCoding.html#//apple_ref/doc/uid/10000107i

关于objective-c - 是否有反向 "setValuesForKeysWithDictionary"- makeDictionaryWithObjectProperties?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3711722/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com