这可能已被问过一百万次,但我没有从我找到的所有示例中得到任何结果。
我有以下课程:
@interface LoginRequest : NSObject
{
NSString* _deviceId;
}
@property (nonatomic, retain) NSString* deviceId;
@end
我正在尝试将其发布到服务器,在 HTTP 正文中编码为表单键/值。
我正在使用以下内容来设置映射:
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];
这取自此处的示例:https ://github.com/RestKit/RestKit/wiki/Posting-Data-Objects
我收到以下错误:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<LoginRequest 0x8c9c980> valueForUndefinedKey:]: this class is not key value coding-compliant for the key DeviceId.'
这是我尝试发送对象的方式:
RKObjectManager *objectManager = [RKObjectManager sharedManager];
LoginRequest* request = [LoginRequest alloc];
request.deviceId = @"Test";
[objectManager postObject:request delegate: self];
任何人都可以帮忙吗?
更新:我可以确认,当我不使用自定义键码映射的东西时,它可以工作。以下是我的工作:
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];