Alan
Alan

Reputation: 796

Using RestKit to map urls

I am trying to read the following Json data using RestKit and am not really sure if I am approaching it correctly. What I do is log a user into the server and get a response with a token and some links.

example Json
{
    "links": {
        "map1": {
            "href": "https://www.website/mapToThis"
        },
        "map2": {
            "href": "https://www.website/mapToThat"
        }
    },
    "token": "12345678912345678"
}

I have created a class to map to and get the token value no problem.

.h

@interface UserApiMap : NSObject

@property (strong, nonatomic) NSString* token;
@property  (nonatomic, retain) NSArray* links;

+ (RKObjectMapping *) mapping;

@end

.m - mapping

+ (RKObjectMapping *)mapping {

    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[self class]];
    [mapping addAttributeMappingsFromDictionary:@{@"token" : @"token"}];

    // How do I map the links??

    return mapping;
}

So my question is do I need an array to hold the links, or do I need a second class to map the links. If I needed a second class would I need two properties 'map1' and 'map2' which would be third classes of type link.

Any input to get me on track would be greatly appreciated and a big help.

Upvotes: 1

Views: 61

Answers (1)

Wain
Wain

Reputation: 119031

Your initial problem is that you have NSArray* links - i.e. an array of links desired, and "links": { - i.e. a dictionary of links supplied. RestKit can't automatically convert between these data structures.

If you were to change the desired to NSDictionary* links and add a simple mapping for @"links" : @"links" then RestKit would map the dictionary out. You would then be able to interrogate the dictionary as usual (including direct access and KVC - which could give you an array of links). This is the simplest option.

If you wanted to store an array of links specifically it's much easier to:

  • Change the JSON
  • Map to a dictionary and then convert

Upvotes: 1

Related Questions