RestKit:如何定义可选(嵌套)键路径

发布于 2024-12-23 15:41:37 字数 1247 浏览 0 评论 0原文

我有一个像这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌路终见情 2024-12-30 15:41:37

也许您应该在实例化对象时正确初始化它们?也许一开始你的所有财产都是零;然后随着您的操作填写属性;在提交(或映射或其他任何内容)之前,检查属性值的数据完整性。

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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文