简体   繁体   中英

RestKit mapping array of string and a keyPath value at same Object

I've looking for an answer for hours, I have no ideia what to do.

I have a JSON that I need to map, which has an array of string with no keyPath, but also has an attribute that needed to be mapped which has a keyPath. I was using RestKit 0.21.0, and had the same error when I updated to 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)];

When I run this, it successful map the array in 3 different element, as you can see here:

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})"
)

But It didn't do the @parent.site , which I desire is:

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})"
)

I'm using CoreData to save the data, and my ManagedObject is like that,

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

I really have no ideia what I'm doing wrong, so any suggestions would be appreciated. Thanks

When you set the response descriptor key path to @"content.modulos" you are asking RestKit to throw away all of the higher and sibling information from the JSON (during the mapping process). So the @parent information doesn't exist.

You can't really map into your current desired structure. RestKit expects that you will map the site to one object and then map the modulos to another object (using a nested mapping connected with RKRelationshipMapping ) and use a relationship to connect them. In this way you could use @parent , but you would no longer need to. Your single response descriptor would use the site mapping and a key path of @"content" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM