我已經找了幾個小時的答案,我沒有意識到該怎麼做。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
我真的有沒有意識到我做錯了什麼,所以任何建議,將不勝感激。謝謝
謝謝,我不想使用關係,因爲這個數據將填充一個TableView,所以當您獲取表「Module」然後使用謂詞選擇您想要的「site」時,它會容易得多。好吧,但如果是RestKit設計,我只需要遵循。我不會選擇你的答案,因爲它不是我想要的,儘管它有幫助。再次感謝你。 –
謂詞可以遍歷關係,所以你可以做你的表和過濾器。唯一要記住的是,如果您使用FRC,那麼它不會更新關係內容中的更改。 – Wain