是否有反向的“setValuesForKeysWithDictionary”? - makeDictionaryWithObjectProperties?
我从 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,根据协议,我的视图全部符合,但我也想用这些值填充 NSManagedObject 。因此,我认为使用 NSDictionary 作为中间步骤,我可以不必在 NSManagedObject 上执行一个类别,该类别根据子类 NSObject 的对象上的值手动设置每个属性。
有了字典,我就可以:
[myManagedObject setValuesForKeysWithDictionary:dict];
完成上述操作后,我就无法恢复字典表示形式了?
I parse some JSON from a web service, this gives me an NSDictionary, I use this dictionary to populated properties on a valueEntity of type NSObject by
[myObject setValuesForKeysWithDictionary:JSONDict];
(myObject has the same property names and types as the dictionary from the JSON parser)
name = name
count = count
startDate = startDate
etc..
Is there a way to go the other way, where I have an NSDictionary that I would like to have "filled" with the properties and their values from an my NSObject subclass.
Like I suggest in the title, something along the lines of this:
one way
MyObject *myObject = [[MyObject alloc] init];
[myObject setValuesForKeysWithDictionary:JSONDict];
the other way around
NSMutableDictionary *dict = [myObject makeDictionaryWithObjectProperties];
The reason for this is that I have a valueEntity which, by protocol, my views all conform to, but I would like to populate an NSManagedObject with the values as well. So I thought that using the NSDictionary as an intermediate step I can get around having to do a category on my NSManagedObject that sets each property manually from the value on my object subclassing NSObject.
With a dictionary I can go:
[myManagedObject setValuesForKeysWithDictionary:dict];
I just can't get the dictionary representation back out once I done the above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,方法是:
传入表示您感兴趣的对象属性的键数组,并返回包含属性值的字典。
NSKeyValueCoding 协议定义了许多强大且非常有用的方法;熟悉是一件很棒的事情。
http://developer. apple.com/library/ios/#documentation/cocoa/reference/foundation/Protocols/NSKeyValueCoding_Protocol/Reference/Reference.html
还有一个非常有价值的配套指南:
http://developer.apple.com/library/ios/documentation /cocoa/Conceptual/KeyValueCoding/KeyValueCoding.html#//apple_ref/doc/uid/10000107i
Yep, the method is:
You pass in an array of keys representing the object properties that you're interested in, and get back a dictionary containing the property values.
The
NSKeyValueCoding
protocol defines a number of powerful and highly useful methods; it's a great thing to get familiar with.http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Protocols/NSKeyValueCoding_Protocol/Reference/Reference.html
There's also a very valuable companion guide:
http://developer.apple.com/library/ios/documentation/cocoa/Conceptual/KeyValueCoding/KeyValueCoding.html#//apple_ref/doc/uid/10000107i
你真的想获得所有财产吗?通常,以字典形式获取对象的所有属性是没有意义的。例如,如果
NSManagedObject
包含关系,您是否也想在字典中获取它?如果是这样,你想如何表示它?正如 jlehr 所说,最好使用
dictionaryWithValuesForKeys:
显式提供一组键。也就是说,使用可怕的 Objective-C 运行时,您确实可以实现 makeDictionaryWithObjectProperties 。使用 class_getPropertyList 和 property_getName。我不会详细说明如何做到这一点,因为如果您遵循这种方法,您就知道自己在做什么。
顺便说一句,使用
setValuesForKeysWithDictionary:
将从 Internet 返回的JSON
对象分配给NSManagedObject
是非常危险的。因为,如果JSON
对象碰巧包含您不期望的附加键,您的代码可能会突然崩溃。所以,不要这样做。另一条评论:你的伪代码
没有多大意义:是的,你可以针对
NSObject
调用setValuesForKeysWithDictionary:
,但它会惨败,因为你不能setValue:forKey:
将JSONDict
中的任何条目设置为普通NSObject
。我想您知道这一点,但我想明确地说出这一点,以便看到此页面的新手不会被愚弄。Do you really want to get all the properties? Often it doesn't make sense to get all the properties of an object as a dictionary. For example, if an
NSManagedObject
contains a relationship, do you want to get it too in the dictionary? If so, how do you want to represent it?As jlehr said, it's better to feed a set of keys explicitly using
dictionaryWithValuesForKeys:
.That said, using the dreaded Objective-C runtime, you can indeed implement
makeDictionaryWithObjectProperties
. Use class_getPropertyList and property_getName. I won't detail how to do that, because if you do follow this approach, you know what you're doing.By the way, it's very dangerous for you to assign a
JSON
object returned from the Internet to anNSManagedObject
usingsetValuesForKeysWithDictionary:
. Because, if theJSON
object happens to contain an additional key which you don't expect, your code can suddenly crash. So, don't do that.Another comment: your pseudo-code
doesn't make much sense: yes you can call
setValuesForKeysWithDictionary:
againstNSObject
, but it will miserably fail, because you can'tsetValue:forKey:
for any of the entries inJSONDict
to a vanillaNSObject
. I guess you know that, but I'd like to say that explicitly so that a novice who comes across this page won't be fooled.这个人使用了一些运行时魔法来获取对象上声明的所有属性,并在没有情况下获取字典手动提供属性名称。
我还发现了这个 不错的 github 存储库,他们几乎做了同样的技巧,但是在一个漂亮的包装中。他们还能够从对象创建 json 或 plist。
This guy here used some runtime magic to get all the properties declared on an object and got the dictionary without manually providing the property names.
And there'e also this nice github repo i found, They pretty much do the same trick, but in a nice package. They also have the ability to create a json or plist from an object.