我正在使用的REST服务有两种不同的响应模式。
{
success: true,
data: { //all data goes here}
}
或
{
success: false,
error: "Error",
message: "some message"
}
我正在尝试配置RESTKit以返回我创建的某个错误对象,以存储错误响应和已配置的类(基于URL)for success = true case。
我正在尝试使用RKDynamicMapping来根据“成功”的价值来实现这一目标。
- (RKDynamicMapping *)createCompatibleMapping:(NSDictionary *)successDictionary withClass:(Class) class{
NSMutableDictionary *withSuccessDictionary = [NSMutableDictionary dictionaryWithDictionary:successDictionary];
[withSuccessDictionary addEntriesFromDictionary:@{@"success" : @"success"}];
RKObjectMapping *successMapping = [RKObjectMapping mappingForClass:class];
[successMapping addAttributeMappingsFromDictionary:withSuccessDictionary];
RKObjectMapping *restErrorMapping = [RKObjectMapping mappingForClass:[RESTErrorObject class]];
[restErrorMapping addAttributeMappingsFromDictionary:@{@"success" : @"success", @"error" : @"errorString", @"message" : @"message"}];
RKDynamicMapping *dynamicMapping = [RKDynamicMapping new];
[dynamicMapping addMatcher:[RKObjectMappingMatcher matcherWithKeyPath:@"success" expectedValue:[NSNumber numberWithBool:YES ] objectMapping:successMapping]];
[dynamicMapping addMatcher:[RKObjectMappingMatcher matcherWithKeyPath:@"success" expectedValue:[NSNumber numberWithBool:NO ] objectMapping:restErrorMapping]];
return dynamicMapping;
}
然后我将它添加到响应描述符中,如下所示:
RKResponseDescriptor * cardDetailsResponseDescriptor2 = [RKResponseDescriptor responseDescriptorWithMapping:cardDetailsResponseMapping method:RKRequestMethodGET
pathPattern:@"services/details/:cardNumber" keyPath:@"data"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[_objectManager.router.routeSet addRoute:[RKRoute routeWithName:@"getCardDetailsRoute" pathPattern:@"services/details/:cardNumber" method:RKRequestMethodGET]];
然后我打电话给你
[self.objectManager getObjectsAtPathForRouteNamed:@"getCardDetailsRoute" object:card parameters:@{@"token" : authToken} success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
我知道这是一个很重要的帖子,可能很难理解。感谢您的耐心:D 对我应该做的事感到困惑。
更改REST API实际上不是一种选择。
答案 0 :(得分:0)
尝试将期望值设置为@“true”@“false”。我认为这些值在运行动态映射时将是字符串,并且它们将仅在映射期间转换为BOOL,并且目标类型是BOOL。