2013-04-25 31 views
3

我的后端回报这种JSON文件:如何使用此JSON实现动态映射(RKDynamicMapping)?

{ “回应”:{ “消息”: “测试” }, “responseCode”:205 }

根据responseCode值,什么是内部响应是不同的。 我知道我必须使用类似RKDynamicMapping的东西,但不知道如何。 我看到的问题是,在示例代码和手册中,映射之间的区分属性在里面,但在这种情况下是在外面。 我试过,但不起作用:

[dynamicMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation) { 
     NSNumber* responseCode=(NSNumber*)representation[@"responseCode"]; 
     if (responseCode.integerValue==204) { 
      return searchByLocatorResponseContent204Mapping; 
     } else if (responseCode.integerValue==205) { 
      return searchByLocatorResponseContent205Mapping; 
     } 
     return nil; 
    }]; 
    RKResponseDescriptor *searchByLocatorResponseContentResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:dynamicMapping pathPattern:kCheckinSearchByLocatorPath keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 
    [objectManager addResponseDescriptor:searchByLocatorResponseContentResponseDescriptor]; 

我猜是因为我想改变映射不拥有这个属性来区分它们(就像在从静止套件网站的女孩/男孩示例代码类型)

有没有人有任何建议? 在此先感谢。

+0

您是否可以不使用keypath来指定“响应”内项目的映射? – Wain 2013-04-26 16:40:48

回答

0

如果你这样设置,restkit预计dynamicMapping将为整个响应json提供映射,而不仅仅是'response'的内容。

基本上searchByLocatorResponseContent204Mapping需要映射response字段和responseCode字段。

据我所知,restkit不能向外看,因为representation参数只给你在当前上下文中的信息。但是,它在路线图上,并且已经有一张关于此的票:https://github.com/RestKit/RestKit/issues/1327