I’m new to RestKit so this question may be dumb: When using [objectManager sendObject:…], how do I tell RestKit which mapping it should use for the result? More specific: I am sending GET and POST data to a server which in return responds with a JSON encoded message:
{"purchaseresult":{"status":"ok","errormsg":""}}
The Objective-C code I use looks like this:
RKObjectMapping *purchaseResultMapping = [RKObjectMapping mappingForClass:[PurchaseResult class]];
[purchaseResultMapping mapKeyPathsToAttributes:@"status", @"status", @"errormsg", @"errorMessage",nil];
[objectManager.mappingProvider setMapping:purchaseResultMapping forKeyPath:@"purchaseresult"];
[[RKObjectManager sharedManager].mappingProvider setErrorMapping:purchaseResultMapping];
[objectManager sendObject:queryParams toResourcePath:@"/purchase/post" usingBlock:^(RKObjectLoader* loader) {
loader.method = RKRequestMethodPOST;
loader.resourcePath = @"/purchase/post";
loader.params = queryParams;
loader.objectMapping = purchaseResultMapping;
}];
This returns an RestKit error:
restkit.network:RKObjectLoader.m:216 Encountered errors during mapping: Expected an object mapping for class of type ‘__NSDictionaryI’, provider returned one for ‘PurchaseResult’
Any ideas what I am doing wrong here?
Thanks
Christian
The object that you’re sending is a NSDictionary so RestKit is looking for an object mapping for an NSDictionary but you are providing a PurchaseResult mapping. If you send an instance of PurchaseResult or provide your mapping for the NSDictionary class then this error should be avoided.
An alternative to this would be as follows:
Just make sure you’ve defined your mapping for the objects coming back in and you should be good.