1

我成功地使用 RESTKit 来访问 Web 服务。但是我无法将服务器错误(如 404、403 ...)映射到 RESTKit 的标准 RKErrorMessage 类。假设我从我的服务中获得以下 JSON 响应以及 403 状态代码:

{"errors":{"message":"Some error message"}}

在我的 iOS 应用程序中,我尝试借助以下方法映射错误:

RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class
[errorMapping addPropertyMapping:[RKAttributeMapping
   attributeMappingFromKeyPath:@"message" toKeyPath:@"message"]];

statusCodes4xx = RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError);

errorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:notecardMapping 
                     pathPattern:nil keyPath:@"errors" statusCodes:statusCodes4xx];

但我得到的只是来自 RESTKit 的错误:

Failed mapping operation: No mappable values found for any of the attributes or relationship mappings

我的错在哪里?例如,它只是描述符中的错误路径吗?

4

1 回答 1

6

您似乎在使用notecardMapping而不是errorMapping在您的响应描述符中。尝试这个:

RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];

[errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"errorMessage"]];

RKResponseDescriptor *errorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping
                                                                                pathPattern:nil
                                                                                    keyPath:@"errors.message"
                                                                                statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)];

[manager addResponseDescriptorsFromArray:@[errorDescriptor]];
于 2013-01-07T22:38:41.410 回答