Libor Zapletal
Libor Zapletal

Reputation: 14102

RestKit responseDescriptor mapping without KVC

I am trying to map this response from API url (...com/api/login?params...)

{"success":0,"error":"Some error"}

But I can't get it working. I have this class:

@interface LoginResponse : NSObject

@property(nonatomic) NSNumber * Success;
@property(nonatomic) NSString * Error;

@end

This I have in my AppDelegate.m (with other working response descriptors):

RKObjectMapping *loginMapping = [RKObjectMapping mappingForClass:[LoginResponse class]];
[loginMapping addAttributeMappingsFromDictionary:@{
                                                   @"success" : @"Success",
                                                   @"error" : @"Error",
                                                  }];

RKResponseDescriptor *loginResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:loginMapping
                                                                                             method:RKRequestMethodAny
                                                                                        pathPattern:@"/login"
                                                                                            keyPath:nil
                                                                                        statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[manager addResponseDescriptor:loginResponseDescriptor];

And I am calling it like this:

RKObjectManager *manager = [RKObjectManager sharedManager];

[manager getObjectsAtPath:@"/api/login"
               parameters:@{@"nickname":@"...",@"password":@"test"}
                  success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
 {
     NSArray* statuses = [mappingResult array];

 }
                  failure:^(RKObjectRequestOperation *operation, NSError *error)
 {
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                     message:[error localizedDescription]
                                                    delegate:nil
                                           cancelButtonTitle:@"OK"
                                           otherButtonTitles:nil];
     [alert show];
     NSLog(@"Hit error: %@", error);
 }];

And I get these errors:

Hit error: Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No mappable object representations were found at the key paths searched." UserInfo=0x10a2b48d0 {NSLocalizedDescription=No mappable object representations were found at the key paths searched., NSLocalizedFailureReason=The mapping operation was unable to find any nested object representations at the key paths searched: events, points, results
The representation inputted to the mapper was found to contain nested object representations at the following key paths: error, success
This likely indicates that you have misconfigured the key paths for your mappings., keyPath=null, DetailedErrors=(
)}

I just can't get it working. I am trying to look at wiki for RestKit but I think I have it okay. Just few changes to my case but I guess it should be working. What I have wrong? Thanks

Upvotes: 0

Views: 808

Answers (1)

Wain
Wain

Reputation: 119031

Your problem is path patterns in relation to your base URL. They currently don't match.

I would set the bast URL to: ...com/api/. Then both the response descriptor path pattern and the get objects path are login. Now everything matches.

If the base URL is set to ...com, then the path patterns will both need to be /api/login.

Upvotes: 1

Related Questions