【问题标题】:Mapping top level items and embedded arrays with RestKit使用 RestKit 映射顶级项目和嵌入式数组
【发布时间】:2015-03-27 17:44:43
【问题描述】:

鉴于以下 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

【问题讨论】:

  • 显示您的代码和模型。跟踪日志告诉你什么?
  • 位置的关键路径错误。你在调用什么 GET(什么路径)?
  • 位置的关键路径是错误的。我也很怀疑。我不确定你在 re: GET 中要问什么?
  • 您正在使用对象管理器来发出请求?显示该代码。

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


【解决方案1】:

在您的 XCDataModel 中获取一个名为 Foo 的实体。 设置 anno 和 vomini 的属性。

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

在 Foo 处为 Location 实体添加关系,放置多个关系。

Foo Like 的调用映射:

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

【讨论】:

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