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

How to map a nested array of complex objects with 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 class 中的嵌套数组 "questions" 之前,一切都正确映射。它 returns 基于返回数据的正确对象数,但所有变量均为 nil(包括嵌套的 "choices" 数组)。如何使用 RestKit 映射复杂对象的嵌套数组?

更新:

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

似乎映射器试图将我的整个对象映射到 NSString,而不是使用我提供的映射值。

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

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

我用一个 属性 IDAQuestion *question 创建了一个包装器并用它来映射。映射现已启动 运行.

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];