1
votes

I got a problem with mapping a nested object value.

I got two objects with the following properties: a)

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

b)

class Profile
@property Input myAwesomeInput;

..so a Profile contains an Input object. When I mapp the objects with RestKit (0.20) I get sth. like this:

{ myAwesomeInput_test:{"value":"xyz","title":"a title"}}

What I wanna achieve is:

{myAwesomeInput_test:"xyz"}

So I don't want to map "Input" but just the Input.value. Is that even possible?

At the moment my code looks like this:

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

EDIT: (solved)

Ok I solved it. Hope it's the way people should do it. You can directly address from within the dictionary.

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

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

1 Answers

3
votes

Use keypaths rather than multiple mappings. Try this:

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

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