我在映射嵌套对象值时遇到问题。
我有两个具有以下属性的对象:a)
class Input
@property NSString value;
@property NSString title;
b)
class Profile
@property Input myAwesomeInput;
..so Profile 包含一个 Input 对象。当我用 RestKit (0.20) 映射对象时,我得到了一些东西。像这样:
{ myAwesomeInput_test:{"value":"xyz","title":"a title"}}
我想要实现的是:
{myAwesomeInput_test:"xyz"}
所以我不想映射“输入”,而只是映射 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];