对于使用 RestKit v0.20.0 的键 Text,此类不符合键值编码

Posted

技术标签:

【中文标题】对于使用 RestKit v0.20.0 的键 Text,此类不符合键值编码【英文标题】:this class is not key value coding-compliant for the key Text using RestKit v0.20.0 【发布时间】:2013-04-16 19:46:32 【问题描述】:

2 天来,我一直在试图找出为什么我在使用 ios 6.1.3 和 Xcode 4.6.2 和 RestKit 0.20.0 时遇到错误:

"...此类不符合键 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];

在这一点上,我不知所措。

【问题讨论】:

您能否在错误消息中包含 which 类不兼容的部分? 【参考方案1】:

请在您的 btnSendClicked 方法中为您的 RKRequestDescriptor 添加一个反向映射,如下所示:

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor      
  requestDescriptorWithMapping:[requestMapping inverseMapping]
                   objectClass:[SignalMessage class]
                   rootKeyPath:@""];

【讨论】:

做到了。非常感谢!

以上是关于对于使用 RestKit v0.20.0 的键 Text,此类不符合键值编码的主要内容,如果未能解决你的问题,请参考以下文章

RESTKit 的问题,无法正确映射

Restkit - 实体(空)不符合键“行”的键值编码

使用 Restkit 0.20 映射此类与键的键值编码不兼容

RestKit - 将数组的键路径映射到该数组内的对象

动态属性作为在 iOS 中使用 Restkit 发布 JSON 的关键

RestKit 对象建模动态数据