iOS
iOS

Reputation: 3626

Restkit ObjectMapping

I have a json as below

    {  
   "predictions":[  
      {  
         "description":"User1",
         "id":"75b8c57b4443f4b881f0efc94afd52437232aee9"
      },
      {  
         "description":"User2",
         "id":"82aa3303704041cda0e4fc34024d383dbf923604"
      },
      {  
         "description":"User3",
         "id":"f88f669a30934ef599bccf86c0236426cf2d313e"
      },
      {  
         "description":"User4",
         "id":"fa9ae7b65fa52bffdabf00d69e7db5cd37539781"
      },
      {  
         "description":"User5",
         "id":“66df3fd7e400eb31efd3ac935036aab2c02b03f0"
      }
   ],
   "status":"OK"
}

I am using RestKit to handle it. Below is how I do the object mapping

Class 1 - Description.h

@interface Description : NSObject

@property(nonatomic,strong) NSString* place;

@end

Class 2 - Predictions.h

@interface Predictions : NSObject

@property(nonatomic,strong) Description* placeDescription;

@end

and in my web interface, I do

-(RKResponseDescriptor*)rkObjectMappingforAutoSuggest:(NSString**)objectPath{

    RKObjectMapping* descriptionMapping = [RKObjectMapping mappingForClass:[Predictions class]];
    [descriptionMapping addAttributeMappingsFromDictionary:@{
                                                          @"description":@"description"}];

    RKObjectMapping* responseMapping = [RKObjectMapping mappingForClass:[Description class]];
    [responseMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"predictions"
                                                                                    toKeyPath:@"predictions"
                                                                                  withMapping:descriptionMapping]];

    RKResponseDescriptor *responseDescriptor =
    [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
                                                 method:RKRequestMethodGET
                                            pathPattern: objectPath
                                                keyPath:nil
                                            statusCodes:[NSIndexSet indexSetWithIndex:200]];

    return responseDescriptor;
}

I need only description in the response.

I guess I am doing some mistake in data mapping. The application is getting crashed with below message

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Predictions 0x15ef98d0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key description.'
***

Couldn't able to figure out the issue.

Upvotes: 0

Views: 97

Answers (1)

stonesam92
stonesam92

Reputation: 4457

You are mapping the description key in the JSON to a description key on the Predictions object, which does not exist.

The property on the Predictions class is called placeDescription, not description.

Replace the dictionary passed to addAttributeMappingsFromDictionary: with:

@{
    @"description":@"placeDescription"
}

Upvotes: 1

Related Questions