6

我有一个关于映射嵌套数组的问题。在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,反之亦然。

4

4 回答 4

8

您需要确保相应地设置 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

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

此外,您需要设置映射:

[articleMapping mapKeyPath:@"author" toRelationship:@"authors" withMapping:authorMapping];
于 2012-04-24T16:49:21.350 回答
3

把它分开会有帮助。

@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) NSArray* author; 
    @property (nonatomic, retain) NSDate*   publicationDate;
@end


//create the article mapping
RKObjectMapping *articleMapping = [RKObjectMapping mappingForClass:[Article class]];
//add rest of mappings here
[articleMapping addAttributeMappingsFromDictionary:@{
                                                  @"title":@"title"
} 

//create the author mapping
RKObjectMapping *authorMapping = [RKObjectMapping mappingForClass:[Author class]];
//add rest of mappings here
[authorMapping addAttributeMappingsFromDictionary:@{
                                                  @"name":@"name"
} 

//link mapping with a relationship
RKRelationshipMapping *rel = [RKRelationshipMapping relationshipMappingFromKeyPath:@"authors" toKeyPath:@"author" withMapping:authorMapping];

//add relationship mapping to article
[articleMapping addPropertyMapping:rel];
于 2014-06-30T19:00:37.527 回答
0

The Whole respnse string take it as nsmutable dictionary then you assign author values to namutable array..

于 2012-04-24T10:47:06.017 回答
0

您需要做的是使用 NSSet 属性来保存嵌套数组。这是一个完整的指南,可以帮助您做到这一点https://medium.com/ios-os-x-development/restkit-tutorial-how-to-fetch-data-from-an-api-into-core-data -9326af750e10

于 2015-02-10T15:59:47.750 回答