我成功使用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
我的错在哪里?例如,它只是描述符中的错误路径吗?
答案 0 :(得分: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]];