powerj1984
powerj1984

Reputation: 2226

Map top level array of strings with RestKit

I'm finding it difficult to deal with a webservice that has a top level array with no keypath and the array contains only strings, i.e.:

["foo", "bar", "baz"]

It would be easy to map if it were something like:

{"array_of_strings": ["foo", "bar", "baz"]} 

I thought perhaps having a nil keyPath and mappingForClass:[NSString class] might do the trick, but it didn't seem to (I'm not sure that mapping for NSString even makes sense, but thought it might be a special case RestKit could handle).

Upvotes: 0

Views: 431

Answers (1)

Wain
Wain

Reputation: 119021

You need to use a nil key path, but you're expected to map into an object where the string is an attribute. Something like:

RKEntityMapping *objectMapping = [RKEntityMapping mappingForEntityForName:...];
[objectMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"name"]];
objectMapping.identificationAttributes = @[ @"name" ];

If you just want to map into an array of strings then you don't need RestKit and you should just use AFJSONRequestOperation.

Upvotes: 1

Related Questions