两天来,我一直试图找出为什么我在使用带有 Xcode 4.6.2 和 RestKit 0.20.0 的 iOS 6.1.3 时遇到错误:
“...此类不符合键 Text 的键值编码。”
奇怪的是,我可以很好地接收(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];
}
在这一点上,我不知所措。