从Restkit获取错误描述

时间:2012-11-22 09:56:12

标签: ios restkit

当我的Web服务返回错误时,我想显示随响应一起发回的错误消息。我在RestKit中找不到任何方法。我已经尝试设置对象管理器的错误映射属性,并使用以下建议:

Restkit json error response msg from server

但这对我不起作用。首先,RKErrorMessage似乎不再存在。

任何帮助表示感谢。

2 个答案:

答案 0 :(得分:1)

试试这个RestKit~> 0.22.0

// Setup error mapping
RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
[errorMapping addPropertyMapping: [RKAttributeMapping attributeMappingFromKeyPath:@"errorDescription" toKeyPath:@"errorMessage"]];
RKResponseDescriptor *errorResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping
                                                                                             method:RKRequestMethodAny
                                                                                        pathPattern:nil
                                                                                            keyPath:nil
                                                                                        statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)];
[[RKObjectManager sharedManager] addResponseDescriptor:errorResponseDescriptor];

答案 1 :(得分:0)

在第20节中,你必须做两件事:

首先,导入RKErrorMessage.h文件:

  

#import <RestKit/RKErrorMessage.h>

然后,将错误映射添加到objectManager:

RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
    [errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"error" toKeyPath:@"errorMessage"]];
    [objectManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:errorMapping pathPattern:nil keyPath:@"error" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)]];