如何使用RestKit映射嵌套的复杂对象数组

时间:2015-08-28 18:15:08

标签: ios objective-c mapping restkit

我有一系列复杂的对象:

@interface IDAQuizResponse : NSObject

@property (nonatomic) IDACallResponse *result;
@property (nonatomic) NSString *appId;
@property (nonatomic) IDAQuiz *quiz;

@end


@interface IDAQuiz : NSObject

@property (nonatomic) int quizId;
@property (nonatomic) NSString *state;
@property (nonatomic) NSString *result;
@property (nonatomic) NSArray *questions;

@end

@interface IDAQuestion : NSObject

@property (nonatomic) int questionId;
@property (nonatomic) NSString *question;
@property (nonatomic) NSArray *choices;

@end

@interface IDAChoice : NSObject

@property (nonatomic) NSString *choice;

@end

我正在尝试使用RestKit映射它们:

RKObjectMapping* quizResponseMapping = [RKObjectMapping mappingForClass:[IDAQuizResponse class]];
    [quizResponseMapping addAttributeMappingsFromArray:@[ @"appId" ]];

    RKObjectMapping* callResponseMapping = [RKObjectMapping mappingForClass:[IDACallResponse class]];
    [callResponseMapping addAttributeMappingsFromArray:@[ @"httpStatusCode", @"messageCode", @"message", @"moreInfo" ]];
    [quizResponseMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"result" toKeyPath:@"result" withMapping:callResponseMapping]];

    RKObjectMapping* quizMapping = [RKObjectMapping mappingForClass:[IDAQuiz class]];
    [quizMapping addAttributeMappingsFromArray:@[ @"quizId", @"state", @"result" ]];
    [quizResponseMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"quiz" toKeyPath:@"quiz" withMapping:quizMapping]];

    RKObjectMapping* questionMapping = [RKObjectMapping mappingForClass:[IDAQuestion class]];
    [questionMapping addAttributeMappingsFromArray:@[ @"id", @"question" ]];
    [quizMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"questions" toKeyPath:@"questions" withMapping:questionMapping]];

    RKObjectMapping* choiceMapping = [RKObjectMapping mappingForClass:[IDAChoice class]];
    [choiceMapping addAttributeMappingsFromArray:@[ @"choice" ]];
    [questionMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"choices" toKeyPath:@"choices" withMapping:choiceMapping]];

    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:quizResponseMapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    NSURL *URL = [NSURL URLWithString:<fancy_url>];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
    [request setHTTPMethod:@"POST"];
    RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors: @[responseDescriptor]];
    [objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
        RKLogInfo(@"Successfully retrieved data");
        NSLog(@"Successfully retrieved data");
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        RKLogError(@"Operation failed with error: %@", error);
        NSLog(@"Operation failed with error: %@", error);
    }];
    [objectRequestOperation start];

所有内容都正确映射,直到我到达IDAQuiz类中的嵌套数组“questions”。它根据返回的数据返回正确数量的对象,但所有变量都是nil(包括嵌套的“choices”数组)。如何使用RestKit映射复杂对象的嵌套数组?

更新

2015-08-28 14:01:38.061 [4894:98833] E restkit.object_mapping:RKMappingOperation.m:682在keyPath'proth'处将值转换为'NSString'类型的表示失败:Error Domain = org。 restkit.RKValueTransformers.ErrorDomain Code = 3002“值转换失败

看来mapper正在尝试将我的整个对象映射到NSString,而不是使用我提供的映射值。

1 个答案:

答案 0 :(得分:1)

json返回的数据为每个问题对象都有一个键:

result": "GENERATED",
"questions":[
{
"question":{

我创建了一个包含单个属性IDAQuestion *问题的包装器,并将其用于映射。映射现已启动并运行。

RKObjectMapping* quizResponseMapping = [RKObjectMapping mappingForClass:[IDAQuizResponse class]];
    [quizResponseMapping addAttributeMappingsFromArray:@[ @"appId" ]];

    RKObjectMapping* callResponseMapping = [RKObjectMapping mappingForClass:[IDACallResponse class]];
    [callResponseMapping addAttributeMappingsFromArray:@[ @"httpStatusCode", @"messageCode", @"message", @"moreInfo" ]];
    [quizResponseMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"result" toKeyPath:@"result" withMapping:callResponseMapping]];

    RKObjectMapping* quizMapping = [RKObjectMapping mappingForClass:[IDAQuiz class]];
    [quizMapping addAttributeMappingsFromArray:@[ @"quizId", @"state", @"result" ]];
    [quizResponseMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"quiz" toKeyPath:@"quiz" withMapping:quizMapping]];

    RKObjectMapping *questionWrapperMapping = [RKObjectMapping mappingForClass:[IDAQuestionWrapper class]];
    [quizMapping addRelationshipMappingWithSourceKeyPath:@"questions" mapping:questionWrapperMapping];

    RKObjectMapping* questionMapping = [RKObjectMapping mappingForClass:[IDAQuestion class]];
    [questionMapping addAttributeMappingsFromDictionary:@{
                                                          @"questionId": @"id",
                                                          @"question":@"question"
                                                          }];
    [questionWrapperMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"question" toKeyPath:@"question" withMapping:questionMapping]];

    RKObjectMapping* choiceMapping = [RKObjectMapping mappingForClass:[IDAChoice class]];
    [choiceMapping addAttributeMappingsFromArray:@[ @"choice" ]];
    [questionMapping addRelationshipMappingWithSourceKeyPath:@"choices" mapping: choiceMapping];