RestKit:如何定义可选(嵌套)键路径
我有一个像这样的 JSON 结构
{
"objects":{
"person":[
{
"id":"1",
"name":"my name"
"contacts":{
"address":{
"id":"1",
"street":"mystreet",
"country":"mycountry"
},
"email":{
"id":"1",
"emailAddress":"[email protected]"
}
},
},
...
,我正在尝试按以下方式映射它:
[personMapping mapKeyPath:@"contacts.address" toRelationship:@"address" withMapping:addressMapping];
只要联系人存在,这种方法就可以正常工作。但如果没有地址/电子邮件,则不会发送此元素,这会导致 RKMappingOperation#applyRelationshipMappings
中出现异常: value = [self.sourceObject valueForKeyPath:relationshipMapping.sourceKeyPath];
现在,我的解决方法是使用 @try...@catch
,但我希望有更好的解决方案...
非常感谢!
编辑:如果根本没有发送任何联系人,则会发生异常;那么,JKDictionary 看起来像这样(同样在 RKMappingOperation#applyRelationshipMappings
中):
{
contacts = "";
name = myName;
id = 1;
...
}
I've a JSON-structure like
{
"objects":{
"person":[
{
"id":"1",
"name":"my name"
"contacts":{
"address":{
"id":"1",
"street":"mystreet",
"country":"mycountry"
},
"email":{
"id":"1",
"emailAddress":"[email protected]"
}
},
},
...
and I'm trying to map it the following way:
[personMapping mapKeyPath:@"contacts.address" toRelationship:@"address" withMapping:addressMapping];
This works well as long as contacts is existing. But if there's no address/email, then this element isn't sent, which leads to an exception in RKMappingOperation#applyRelationshipMappings
: value = [self.sourceObject valueForKeyPath:relationshipMapping.sourceKeyPath];
Right now, my workaround is to use @try...@catch
, but I hope there's a better solution for this...
Thanks a lot!
EDIT: The exception happens then if no contacts are sent at all; then, the JKDictionary looks like this (again in RKMappingOperation#applyRelationshipMappings
):
{
contacts = "";
name = myName;
id = 1;
...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您应该在实例化对象时正确初始化它们?也许一开始你的所有财产都是零;然后随着您的操作填写属性;在提交(或映射或其他任何内容)之前,检查属性值的数据完整性。
Maybe you should properly initialise your objects when you instantiate them? Maybe start out with all your properties zero; then as you go along fill out properties; and before you commit (or map or whatever), check the data integrity of your property values..