RestKit 0.20嵌套对象映射(对象树的路径不同)

时间:2013-05-10 16:13:47

标签: ios objective-c json restkit restkit-0.20

我遇到了映射嵌套对象值的问题。

我有两个具有以下属性的对象: a)

class Input
@property NSString value;
@property NSString title;

b)

class Profile
@property Input myAwesomeInput;

..所以Profile包含一个Input对象。当我用RestKit(0.20)对对象进行mapp时,我得到了......像这样:

{ 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];

1 个答案:

答案 0 :(得分:3)

使用键路径而不是多个映射。 试试这个:

RKObjectMapping* searchProfile = [RKObjectMapping requestMapping];
[searchProfile addAttributeMappingsFromDictionary:@{ @"myAwesomeInput.value" : @"myAwesomeInput_test" }];

RKRequestDescriptor *reqDescSearchProfile = [RKRequestDescriptor requestDescriptorWithMapping:searchProfile objectClass:[SearchProfile class] rootKeyPath:nil];