KVC:如何测试现有密钥
我需要一些关于 KVC 的帮助。
关于操作上下文的几句话:
1)iPhone 连接(客户端)到 webService 以获取对象,
2)我使用 JSON 传输数据,
3)如果客户端具有完全相同的对象映射,我可以迭代NSDictionary 来自 JSON 将数据存储在永久存储(coreData)中。 为此,我使用此代码片段(假设所有数据都是 NSString):
NSDictionary *dict = ... dictionary from JSON
NSArray *keyArray = [dict allKeys]; //gets all the properties keys form server
for (NSString *s in keyArray){
[myCoreDataObject setValue:[dict objectForKey:s] forKey:s]; //store the property in the coreData object
}
现在我的问题....
4)如果服务器实现具有 1 个新属性的新版本对象,会发生什么 如果我将数据传输到客户端并且客户端未处于保存版本级别(这意味着仍在使用“旧”对象映射),我将尝试为不存在的键分配一个值...并且我将有消息:
实体“myOldObject”与键“myNewKey”的键值编码不兼容
您能否建议我如何测试对象中键的存在,如果键存在,我可以继续进行值更新以避免错误消息?
抱歉,如果我的上下文解释有点令人困惑。
谢谢
达里奥
I need a little help about KVC.
Few words about the operating context:
1) The iPhone connects (client) to a webService to get an object,
2) I'm using JSON to transfer data,
3) If the client has exactly the same object mapping I can iterate through the NSDictionary from JSON to store the data in the permanent store (coreData).
To do that I'm using this code fragment (assume that are all data are NSString):
NSDictionary *dict = ... dictionary from JSON
NSArray *keyArray = [dict allKeys]; //gets all the properties keys form server
for (NSString *s in keyArray){
[myCoreDataObject setValue:[dict objectForKey:s] forKey:s]; //store the property in the coreData object
}
Now my problem ....
4) What happen if the server implementing a new version of the object with 1 new property
If I transfer the data to the client and the client is not at the save version level (it means is still using the "old" object mapping) I'll try to assign a value for a non existing key... and I will have the message:
the entity "myOldObject" is not key value coding-compliant for the key "myNewKey"
Could you suggest me how to test for the existence of the key in the object so, if the key exists, I'll can proceed to the value update avoiding the error message ?
Sorry if I have been a little confusing in my context explanation.
Thanks
Dario
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然我想不出一种方法来找出对象支持哪些键,但您可以利用以下事实:当您为不存在的键设置值时,对象的默认行为是 抛出异常。您可以将
setValue:forKey:
方法调用包含在@try
/@catch
块中来处理这些错误。考虑以下对象代码:
对于关键内容,这应该符合 KVC 标准,但没有其他内容。
如果您从以下程序访问此类:
您将获得类似于以下内容的控制台输出:
这表明程序很好地捕获了第一次访问密钥
nonexistentKey
的尝试,第二次尝试生成与您类似的例外。While I can't think of a way to find out what keys will an object support you could use the fact that when you set the value for a nonexistent key the default behaviour of your object is to throw an exception. You could enclose the
setValue:forKey:
method invocation in a@try
/@catch
block to handle these errors.Consider the following code for an object:
This should be KVC-compliant for the key
stuff
but nothing else.If you access this class from the following program:
You will get a console output similar to the following:
This shows that the first attempt to access the key
nonexistentKey
was nicely caught by the program, the second one generated an exception similar to yours.