This has probably been asked a million times, but I’m not getting anywhere from all the examples that I’m finding.
I have the following class:
@interface LoginRequest : NSObject
{
NSString* _deviceId;
}
@property (nonatomic, retain) NSString* deviceId;
@end
And I’m trying to post this to a server, encoded as form key/value in the HTTP body.
I’m using the following to setup the mapping:
objectManager.serializationMIMEType = RKMIMETypeFormURLEncoded;
RKObjectMapping* pmsg = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[pmsg mapKeyPath: @"deviceId" toAttribute:@"DeviceId"];
RKObjectMapping* pmsgSerializeMapping = [pmsg inverseMapping];
[objectManager.mappingProvider setSerializationMapping:pmsgSerializeMapping forClass:[LoginRequest class]];
[objectManager.router routeClass:[LoginRequest class] toResourcePath:@"/login" forMethod:RKRequestMethodPOST];
This was taken from the example here: https://github.com/RestKit/RestKit/wiki/Posting-Data-Objects
I’m getting the following error:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<LoginRequest 0x8c9c980> valueForUndefinedKey:]: this class is not key value coding-compliant for the key DeviceId.'
Here’s how I’m trying to send my object:
RKObjectManager *objectManager = [RKObjectManager sharedManager];
LoginRequest* request = [LoginRequest alloc];
request.deviceId = @"Test";
[objectManager postObject:request delegate: self];
Can anyone help?
Update: I can confirm that when I don’t use the custom key-code mapping stuff, it works. The following I have working:
RKObjectMapping* pmsg = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[pmsg mapAttributes:@"DeviceId", nil];
[objectManager.mappingProvider setSerializationMapping:pmsg forClass:[LoginRequest class]];
[objectManager.router routeClass:[LoginRequest class] toResourcePath:@"/login" forMethod:RKRequestMethodPOST];
Well, your property is named “deviceId”, which is lowercase. But you’re saying the attribute is “DeviceId” which starts with an uppercase “D”.