Search code examples
objective-ciosios6restkitxcode4.5

Restkit object mappings not working


I am trying to learn restkit and right now and working on object mappings. Here is the JSON that I am trying to receive and process (generated by ASP.NET MVC Web API):

[
    {
        "Id": 0,
        "Title": "Test",
        "Location": "Test",
        "Score": {
            "Hill": 10,
            "LVille": 12
        },
        "TimeStart": "2012-11-10T12:30:00",
        "TimeEnd": "2012-11-10T13:20:00",
        "Gender": 0,
        "State": 0
    },
    {
        "Id": 0,
        "Title": "ee",
        "Location": "12:00",
        "Score": {
            "Hill": 13,
            "LVille": 0
        },
        "TimeStart": "12:00 PM",
        "TimeEnd": "12:00 PM",
        "Gender": 1,
        "State": 1
    }
]

The objects that are being mapped to are (SBGame):

@interface SBGame : NSObject

@property (nonatomic, copy) NSNumber* Id;
@property (nonatomic, copy) NSString* Title;
@property (nonatomic, copy) NSString* Location;
@property (nonatomic, strong) SBScore* Score;
@property (nonatomic, copy) NSString* TimeStart;
@property (nonatomic, copy) NSString* TimeEnd;
@property (nonatomic, copy) NSNumber* Gender;
@property (nonatomic, copy) NSNumber* State;

@end

and SBScore :

@interface SBScore : NSObject
@property (nonatomic, copy) NSNumber* Hill;
@property (nonatomic, copy) NSNumber* LVille;
@end

and last but not least the object mappings

RKObjectMapping *ScoreMapping = [RKObjectMapping mappingForClass:[SBScore class]];
[ScoreMapping addAttributeMappingsFromArray:@[@"Hill", @"LVille"]];


RKObjectMapping *GameMapping = [RKObjectMapping mappingForClass:[SBGame class]];
[GameMapping addAttributeMappingsFromDictionary:@{
    @"Id" : @"Id",
    @"Title" : @"Title",
    @"Location" : @"Location",
    @"TimeStart" : @"TimeStart",
    @"TimeEnd" : @"TimeEnd",
    @"Gender" : @"Gender",
    @"State" : @"State",
 }];

[GameMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"score" toKeyPath:@"score" withMapping:ScoreMapping]];


RKResponseDescriptor *decp = [RKResponseDescriptor responseDescriptorWithMapping:GameMapping pathPattern:@"/Games" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:decp];

after all that the error that I recieve is

2012-12-29 16:49:19.553 Scoreboard[3004:c07] Failure with error Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 
"Unable to find any mappings for the given content" UserInfo=0x75c1460 {DetailedErrors=(
), NSLocalizedDescription=Unable to find any mappings for the given content, keyPath=null}

I am very new to RestKit and I have looked through The Object Mapping Tutorial but still it does not work. Any ideas are much appreciated.


Solution

  • I figured it out. When I created the default instance I made the base URL www.example.com/api and then just made the binding and the getObjects method to /Games

    What I should have done is made the base URL www.example.com set the path pattern to /api/Games and call the getObjects method on /api/games

    So from the code shown

    RKResponseDescriptor *decp 
    = [RKResponseDescriptor responseDescriptorWithMapping:GameMapping
                                              pathPattern:@"/Games" 
                                                  keyPath:nil
                                              statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    

    changes to

    RKResponseDescriptor *decp 
    = [RKResponseDescriptor responseDescriptorWithMapping:GameMapping
                                              pathPattern:@"/api/Games"
                                                  keyPath:nil
                                              statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];