微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

objective-c – Restkit映射嵌套数组

我有一个映射嵌套数组的问题.在 https://github.com/RestKit/RestKit/blob/master/Docs/Object%20Mapping.md上有一个映射嵌套对象的示例.

但是我必须做什么,如果嵌套对象是一个对象数组,例如JSON看起来像:

{ "articles": [
    { "title": "RestKit Object Mapping Intro","body": "This article details how to use RestKit object mapping...","author": [{
          "name": "Blake Watters","email": "blake@restkit.org"
      },{
          "name": "abc","email": "emailaddress"
      }]
      "publication_date": "7/4/2011"
    }]
}

为了获得一系列作者,我的课程应该如何?
这是示例中的代码

@interface Author : NSObject
    @property (nonatomic,retain) Nsstring* name;
    @property (nonatomic,retain) Nsstring* email;
@end



@interface Article : NSObject
    @property (nonatomic,retain) Nsstring* title;
    @property (nonatomic,retain) Nsstring* body;
    @property (nonatomic,retain) Author* author; // should be an array of Author-Objects
    @property (nonatomic,retain) NSDate*   publicationDate;
@end

我如何告诉Restkit,这是一个作者数组,如果我将作者属性的类更改为NSArray,那么Restkit如何知道,在这个NSArray中应该是Author-Objects … ??

我使用RKObjectMapping将对象从JSON映射到Objective-c,反之亦然.

解决方法

您需要确保相应地设置var类型:
@interface Article : NSObject
    @property (nonatomic,retain) NSSet* authors;
    @property (nonatomic,retain) NSDate*   publicationDate;
@end

它可能会将其声明为NSArray,但我个人将RestKit与CoreData模型一起使用,这些场景中的关系是NSSet.

此外,您需要设置映射:

[articleMapping mapKeyPath:@"author" toRelationship:@"authors" withMapping:authorMapping];

原文地址:https://www.jb51.cc/c/111371.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐