Restkit映射嵌套数组

and*_*oid 6 objective-c restkit ios5

我有一个映射嵌套数组的问题.在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"
    }]
}
Run Code Online (Sandbox Code Playgroud)

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

@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
Run Code Online (Sandbox Code Playgroud)

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

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

sha*_*all 7

您需要确保相应地设置var类型:

@interface Article : NSObject
    @property (nonatomic, retain) NSString* title;
    @property (nonatomic, retain) NSString* body;
    @property (nonatomic, retain) NSSet* authors;
    @property (nonatomic, retain) NSDate*   publicationDate;
@end
Run Code Online (Sandbox Code Playgroud)

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

此外,您需要设置映射:

[articleMapping mapKeyPath:@"author" toRelationship:@"authors" withMapping:authorMapping];
Run Code Online (Sandbox Code Playgroud)


小智 0

整个响应字符串将其作为 nsmutable 字典,然后将作者值分配给 namutable 数组。