gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:14:47 24 4
gpt4 key购买 nike

我有一系列复杂的对象:

@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 类中的嵌套数组“问题”之前,一切都正确映射。它根据返回的数据返回正确数量的对象,但所有变量都是 nil(包括嵌套的“选择”数组)。如何使用 RestKit 映射复杂对象的嵌套数组?

更新:

2015-08-28 14:01:38.061 [4894:98833] E restkit.object_mapping:RKMappingOperation.m:682 将 keyPath 'question' 处的值转换为类型 'NSString' 的表示失败:错误域 = 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];

关于ios - 如何使用 RestKit 映射复杂对象的嵌套数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32277386/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com