我在映射嵌套对象值时遇到问题。
我有两个具有以下属性的对象:
一种)
class Input
@property NSString value;
@property NSString title;
b)
class Profile
@property Input myAwesomeInput;
..so概要文件包含一个Input对象。当我用RestKit(0.20)映射对象时,我得到了……。像这样:
{ myAwesomeInput_test:{"value":"xyz","title":"a title"}}
我想要实现的是:
{myAwesomeInput_test:"xyz"}
因此,我不想映射“Input”,而只映射Input.value。那有可能吗?
目前,我的代码如下所示:
RKObjectMapping* inputMapping = [RKObjectMapping requestMapping];
[inputMapping addAttributeMappingsFromArray:@[@"value"]];
RKRequestDescriptor *reqDescInput = [RKRequestDescriptor requestDescriptorWithMapping:inputMapping objectClass:[Input class] rootKeyPath:nil];
RKObjectMapping* searchProfile = [RKObjectMapping requestMapping];
RKRequestDescriptor *reqDescSearchProfile = [RKRequestDescriptor requestDescriptorWithMapping:searchProfile objectClass:[SearchProfile class] rootKeyPath:nil];
[searchProfile addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"myAwesomeInput" toKeyPath:@"myAwesomeInput_test" withMapping:inputMapping]];
编辑:(已解决)
好的,我解决了。希望这是人们应该这样做的方式。您可以直接在字典中找到地址。
RKObjectMapping* searchProfile = [RKObjectMapping requestMapping];
[aeSearchProfile addAttributeMappingsFromDictionary:@{
@"myAwesomeInput.value": @"myAwesomeInput_test"
}];
RKRequestDescriptor *reqDescSearchProfile = [RKRequestDescriptor requestDescriptorWithMapping:searchProfile objectClass:[SearchProfile class] rootKeyPath:nil];
最佳答案
使用键路径而不是多个映射。
尝试这个:
RKObjectMapping* searchProfile = [RKObjectMapping requestMapping];
[searchProfile addAttributeMappingsFromDictionary:@{ @"myAwesomeInput.value" : @"myAwesomeInput_test" }];
RKRequestDescriptor *reqDescSearchProfile = [RKRequestDescriptor requestDescriptorWithMapping:searchProfile objectClass:[SearchProfile class] rootKeyPath:nil];
关于ios - RestKit 0.20嵌套对象映射(对象树的路径不同),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16486516/