使用非对称密钥的RestKit POST错误

时间:2012-08-23 21:19:18

标签: objective-c ios rest restkit

我在使用RestKit向服务器发布对象时遇到问题。我正在使用昨晚的RestKit版本,但至少从去年开始就是一个问题。我正在做的是我正在尝试将新对象保存到服务器,但是当我这样做时,我会在请求发送到服务器之前收到映射错误。问题是我的JSON响应键和我的NSObject属性是不对称的,即使RestKit文档说他们不必这样做。在添加新的POST命令之前,我用于下载JSON响应的GET命令就可以了。 RestKit对非对称密钥映射没有任何问题,并且每个工作都有效。但是,现在我正在尝试将NSObject发回服务器,我收到了NSUknownKeyException错误。如果我将密钥更改为对称,则RKObjectMappingOperation完成,POST将实际发送到服务器。这对我来说似乎是一个错误,但我想确保这不是发布的要求。

以下是我的代码详细信息。这是映射:

RKObjectMapping* restaurantReviewMapping = [RKObjectMapping mappingForClass:[RestaurantReview class] ];
[restaurantReviewMapping mapKeyPath:@"restaurant_id" toAttribute:@"restaurantId"];
[restaurantReviewMapping mapKeyPath:@"title" toAttribute:@"title"];
[restaurantReviewMapping mapKeyPath:@"review" toAttribute:@"review"];

这是NSObject:

@interface RestaurantReview : NSObject

  @property (nonatomic, retain) NSNumber* restaurantId;
  @property (nonatomic, retain) NSNumber* rating;
  @property (nonatomic, retain) NSString* title;

@end

从对象映射文档中复制的Post命令:

[[RKObjectManager sharedManager] sendObject:review toResourcePath:fullRestaurantReviewUrl usingBlock:^(RKObjectLoader *loader) {
    loader.method = RKRequestMethodPOST;
    loader.delegate = self;
    loader.serializationMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[RestaurantReview class] ];
}];

最后,在请求发送到服务器之前我收到的错误:

2012-08-23 14:57:49.118 [61331:c07] D restkit.network:RKObjectLoader.m:374 POST or PUT request for source object <RestaurantReview: 0x74d55c0>, serializing to MIME Type application/json for transport...
2012-08-23 14:57:49.119 [61331:c07] D restkit.object_mapping:RKObjectMappingOperation.m:655 Starting mapping operation...
2012-08-23 14:57:49.184 [61331:c07] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<RestaurantReview 0x74d55c0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key restaurant_id.'

这是一个错误还是我做错了什么?在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您想为inverseMapping提供serializationMapping