Restkit失败:获取访问json返回

我正在使用Restkit .20,并尝试获取在故障块中返回的错误对象。 我已经尝试了下面的两个都返回(null)的错误对象。

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

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

这是在我的失败块打印出“errorMessage:(null)”

  failure:^(RKObjectRequestOperation *operation, NSError *error) { NSLog(@"errorMessage: %@", [[error userInfo] objectForKey:RKObjectMapperErrorObjectsKey]); } 

这里是从服务器的JSON返回的内容。

 { "status": "FAIL", "user_errors": null, "error": "Credit card type is not accepted by this merchant account.", "user": null } 

HTTP状态代码201是成功代码,而不是错误代码,因此不会尝试错误映射(因为statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError) )。

所以,更新服务器以发送适当的状态码。 如果您更新响应描述符来检查错误的“成功”响应,我认为这将起作用。