使用RestKit映射顶级项和嵌入式数组

时间:2015-03-27 17:44:43

标签: ios objective-c json restkit restkit-0.20

给出以下JSON结构:

{
  "foo": {
    "anno": "blah",
    "domini": null,
    "locations": [
      {
        "data": {
          "lat": null,
          "lon": null
         },
         "data": {
           "lat": null,
           "lon": null
         }
       }
     ]
  }
}

如何为此方案设置RestKit映射?我虽然拥有它,但我无法映射顶级foo项目anno和domini。我可以自己成功地映射位置,但不与foo协调。

我过去成功完成了这项工作,但现在有些事情正在逃避。

foo.h中

@interface Foo : NSObject
@property (nonatomic, strong) NSString *anno;
@property (nonatomic, strong) NSString *domini;
@end

Location.h

@interface LocationData : NSObject
@property NSString *lat;
@property NSString *lon;
@end

Controller.m或者

RKObjectMapping *fooMapping = [RKObjectMapping mappingForClass:[Foo class]];
[fooMapping addAttributeMappingsFromArray:@[@"anno", @"domini"]];

RKObjectMapping *locationMapping = [RKObjectMapping mappingForClass:[Location class]];
[locationMapping addAttributeMappingsFromArray:@[@"lat",@"lon"]];

[fooMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"location" toKeyPath:@"location" withMapping: locationMapping]];

RKResponseDescriptor *fooReponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:dataMapping method:RKRequestMethodGET pathPattern:@"foo" keyPath:@"foo" statusCodes:[NSIndexSet indexSetWithIndex:200]];

RKResponseDescriptor *locationResponseDescriptor =
        [RKResponseDescriptor responseDescriptorWithMapping:locationdMapping
                                                     method:RKRequestMethodGET
                                                pathPattern:nil
                                                    keyPath:@"foo.location"
                                                statusCodes:[NSIndexSet indexSetWithIndex:200]];

我认为这是所有重要的事情。我希望能够减少我发布的文字数量,并没有留下任何重要的内容。

编辑 2015-03-29

- (void)loadChildren {

NSDictionary *queryParams = @{@"sort" : @"new"};

[[RKObjectManager sharedManager] getObjectsAtPath:redditPath
                                       parameters:queryParams
                                          success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                                              _children = mappingResult.array;
                                              [self.tableView reloadData];
                                          }
                                          failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                              NSLog(@"You mean YOU'RE the lunatic who's responsible for almost destroying my ship? : %@", error);
                                          }];
}

redditpath早先使用...

设置
redditPath = [NSString stringWithFormat:@"/r/%@/new.json", subRedditToLoad];

在这种情况下,subRedditToLoad是 aww

1 个答案:

答案 0 :(得分:0)

在您的XCDataModel中获取名为Foo的实体。 设置anno和vomini的财产。

同时创建另一个实体位置。 使用Transformable类型设置名为 data 的属性。

在Foo为位置实体添加关系让它有很多关系。

Foo的调用映射:

[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"foo" toKeyPath:@"Foo" withMapping:[Location objectMappingForLocation:Enum]]];