RestKit在同一个Object上映射字符串数组和keyPath值

时间:2013-11-25 06:23:43

标签: ios objective-c restkit restkit-0.20

我已经找了几个小时的答案,我没有想法做什么。

我有一个我需要映射的JSON,它有一个没有keyPath的字符串数组,但是还有一个需要映射的属性,它有一个keyPath。我使用的是RestKit 0.21.0,当我更新到RestKit 0.22.0时出现了同样的错误。

JSON:

{
  "content": {
    "site": "www.inf.ufsc.br/~fcdocil",
    "modulos": [
      "noticias",
      "fotos",
      "conteudo"
    ]
  }
}

RKResponseDescriptor:

RKEntityMapping *moduleMapping = [RKEntityMapping mappingForEntityForName:@"Module" inManagedObjectStore:store];
    [moduleMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"type"]];
    [moduleMapping addAttributeMappingsFromDictionary:@{@"@parent.site" : @"site"}];

    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:moduleMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"content.modulos" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

当我运行它时,它成功地将数组映射到3个不同的元素中,如下所示:

SUCCESS (
    "<Module: 0x8e54280> (entity: Module; id: 0x8cd31f0 <x-coredata://B31664A2-091E-43B8-9B36-EB7AF6C27292/Module/p4> ; data: {\n    site = nil;\n    type = noticias;\n})",
    "<Module: 0x8e666b0> (entity: Module; id: 0x8cd3180 <x-coredata://B31664A2-091E-43B8-9B36-EB7AF6C27292/Module/p2> ; data: {\n    site = nil;\n    type = fotos;\n})",
    "<Module: 0x8e66a60> (entity: Module; id: 0x8cd3170 <x-coredata://B31664A2-091E-43B8-9B36-EB7AF6C27292/Module/p3> ; data: {\n    site = nil;\n    type = conteudo;\n})"
)

但它没有@parent.site,我希望是:{/ p>

SUCCESS (
    "<Module: 0x8e54280> (entity: Module; id: 0x8cd31f0 <x-coredata://B31664A2-091E-43B8-9B36-EB7AF6C27292/Module/p4> ; data: {\n    site = www.inf.ufsc.br/~fcdocil;\n    type = noticias;\n})",
    "<Module: 0x8e666b0> (entity: Module; id: 0x8cd3180 <x-coredata://B31664A2-091E-43B8-9B36-EB7AF6C27292/Module/p2> ; data: {\n    site = www.inf.ufsc.br/~fcdocil;\n    type = fotos;\n})",
    "<Module: 0x8e66a60> (entity: Module; id: 0x8cd3170 <x-coredata://B31664A2-091E-43B8-9B36-EB7AF6C27292/Module/p3> ; data: {\n    site = www.inf.ufsc.br/~fcdocil;\n    type = conteudo;\n})"
)

我正在使用CoreData来保存数据,而我的ManagedObject就是这样,

module.h中

@interface Module : NSManagedObject

@property (nonatomic, retain) NSString * type;
@property (nonatomic, retain) NSString * site;

@end

Module.m

@implementation Module

@dynamic type;
@dynamic site;

@end

我真的没有想到我做错了什么,所以任何建议都会受到赞赏。感谢

1 个答案:

答案 0 :(得分:1)

当您将响应描述符密钥路径设置为@"content.modulos"时,您要求RestKit丢弃JSON中的所有更高和兄弟信息(在映射过程中)。所以@parent信息不存在。

您无法真正映射到当前所需的结构。 RestKit希望您将site映射到一个对象,然后将modulos映射到另一个对象(使用与RKRelationshipMapping连接的嵌套映射)并使用关系连接它们。通过这种方式,您可以使用@parent,但您不再需要。您的单个响应描述符将使用site映射和@"content"的关键路径。

相关问题