2天后,我一直试图找出为什么我使用iOS 6.1.3使用Xcode 4.6.2和RestKit 0.20.0收到错误:
“...此类不符合关键值的关键值。”
奇怪的是我可以接收(GET)JSON对象。当我创建示例SignalMessage对象然后尝试将其重新发送回服务器时,会发生错误。
JSON如下:
{"Text":"New Message","HasMessage":"true"}
SignalMessage对象如下所示:
#import <Foundation/Foundation.h>
@interface SignalMessage : NSObject {
}
@property (nonatomic, copy) NSString *signalText;
@property (nonatomic, retain) NSNumber *isHasMessage;
@end
这样的实现:
#import "SignalMessage.h"
@implementation SignalMessage
@synthesize isHasMessage, signalText;
@end
我正确工作的getMessage函数如下所示:
- (IBAction)getMessage:(id)sender;
{
NSLog(@"%@", @"Getting message... ");
NSURL *url = [NSURL URLWithString:@"http://ec2-54-243-148-145.compute-1.amazonaws.com/TabletPractice/api/signal?clientIdentifier=2"];
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:url];
RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[SignalMessage class]];
[responseMapping addAttributeMappingsFromDictionary:@{@"Text":@"signalText", @"HasMessage": @"isHasMessage"}];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[manager addResponseDescriptor:responseDescriptor];
[manager getObject:nil path:@"" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result)
{
NSArray *theresults = [result array];
for (SignalMessage *item in theresults) {
self.txtMessage.text = item.signalText;
[self hideControls];
}
} failure:^(RKObjectRequestOperation * operation, NSError * error)
{
NSLog (@"Server WS call failure: operation: %@ \n\nerror: %@", operation, error);
}];
}
这是sendClicked消息让我感到悲伤:
- (IBAction)btnSendClicked:(id)sender;
{
if ([txtMessage.text length] < 1)
return;
NSURL *url = [NSURL URLWithString:@"http://ec2-54-243-148-145.compute-1.amazonaws.com/TabletPractice/api/signal?clientIdentifier=2"];
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:url];
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:@{@"Text":@"signalText", @"HasMessage": @"isHasMessage"}];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
objectClass:[SignalMessage class]
rootKeyPath:@""];
[manager addRequestDescriptor:requestDescriptor];
SignalMessage *newMessage = [[SignalMessage alloc] init];
newMessage.signalText = @"Test Message";
BOOL isMsg = TRUE;
NSNumber *boolAsNumber = [NSNumber numberWithBool:isMsg];
newMessage.isHasMessage = boolAsNumber;
[manager putObject:newMessage path:@"" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
NSLog(@"We object mapped the response with the following result: %@", result);
} failure:^(RKObjectRequestOperation * operation, NSError * error)
{
NSLog (@"Server WS call failure: operation: %@ \n\nerror: %@", operation, error);
}];
[self hideControls];
}
此时,我很茫然。
答案 0 :(得分:2)
请在btnSendClicked方法中为您的RKRequestDescriptor添加逆映射,如下所示:
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor
requestDescriptorWithMapping:[requestMapping inverseMapping]
objectClass:[SignalMessage class]
rootKeyPath:@""];