2013-11-25 47 views
4

我已經找了幾個小時的答案,我沒有意識到該怎麼做。RestKit映射數組的字符串和keyPath值在同一個對象

我有一個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,我的願望是:

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

當您將響應描述符關鍵路徑設置爲@"content.modulos"時,您要求RestKit從JSON中丟棄所有更高和同級信息(在映射過程中)。所以@parent信息不存在。

您不能真正映射到您當前所需的結構。 RestKit希望您將site映射到一個對象,然後將modulos映射到另一個對象(使用與RKRelationshipMapping連接的嵌套映射)並使用關係來連接它們。這樣你可以使用@parent,但你不再需要。您的單個響應描述符將使用site映射和一個關鍵路徑@"content"

+0

謝謝,我不想使用關係,因爲這個數據將填充一個TableView,所以當您獲取表「Module」然後使用謂詞選擇您想要的「site」時,它會容易得多。好吧,但如果是RestKit設計,我只需要遵循。我不會選擇你的答案,因爲它不是我想要的,儘管它有幫助。再次感謝你。 –

+0

謂詞可以遍歷關係,所以你可以做你的表和過濾器。唯一要記住的是,如果您使用FRC,那麼它不會更新關係內容中的更改。 – Wain