RestKit - 尝试将对象发布到服务器

时间:2012-09-26 21:44:50

标签: objective-c restkit

这可能已被问过一百万次,但我找不到所有我发现的例子。

我有以下课程:

@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];

1 个答案:

答案 0 :(得分:0)

好吧,你的属性被命名为“deviceId”,它是小写的。但是你说的是属性是“DeviceId”,它以大写的“D”开头。