【问题标题】:Restkit Mapping Nested ArrayRestkit 映射嵌套数组
【发布时间】:2012-04-24 09:17:54
【问题描述】:

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

【问题讨论】:

    标签: objective-c ios5 restkit


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      拆分出来会有帮助。

      @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];
      

      【讨论】:

        【解决方案3】:

        整个响应字符串将其作为 nsmutable 字典,然后将作者值分配给可命名数组..

        【讨论】:

          【解决方案4】:

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

          【讨论】:

            猜你喜欢
            • 2012-10-11
            • 1970-01-01
            • 1970-01-01
            • 2012-05-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-05-29
            • 1970-01-01
            相关资源
            最近更新 更多