RestKit 映射错误“无法将对象集合映射到非可变集合。”

Posted

技术标签:

【中文标题】RestKit 映射错误“无法将对象集合映射到非可变集合。”【英文标题】:RestKit Mapping Error "Cannot map a collection of objects onto a non-mutable collection." 【发布时间】:2013-03-05 07:35:07 【问题描述】:

好的,我有这段代码,我用它来映射来自 JSON API 的数据。一切运行良好,但随后出现此错误:

restkit.object_mapping:RKMapperOperation.m:76 Adding mapping error: Cannot map a collection of objects onto a non-mutable collection. Unexpected destination object type '__NSDictionaryI'

这是我用于映射的代码

RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];

RKObjectMapping *classMapping = [RKObjectMapping mappingForClass:[GradePeriod class]];
[classMapping addAttributeMappingsFromDictionary: @
 @"period" : @"period",
 @"class" : @"name",
 @"term" : @"term",
 @"teacher" : @"teacher",
 @"percent" : @"percent",
 @"grade" : @"grade",
 @"missing" : @"missing",
 @"update" : @"update",
 @"detail" : @"url"
 ];

// Create a Response Descriptor
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:classMapping
                                                                                   pathPattern: nil
                                                                                       keyPath: @"grades"
                                                                                   statusCodes:[NSIndexSet indexSetWithIndex:200]];
[objectManager addResponseDescriptor:responseDescriptor];
objectManager.requestSerializationMIMEType = RKMIMETypeJSON;

NSDictionary *queryParams;
queryParams = [NSDictionary dictionaryWithObjectsAndKeys:username, @"username", password, @"password", nil];

// Retrieve the Data
[objectManager postObject:queryParams
                     path:@""
               parameters:queryParams
                  success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) 
                      grades = [NSMutableArray arrayWithArray: [mappingResult array]];
                      NSLog(@"Loaded Grades: %@", grades);
                      [self.tableView reloadData];
                  
                  failure:^(RKObjectRequestOperation *operation, NSError *error) 
                      // Stuff removed for easier reading
                  ];

成绩是NSMutableArray

@property (nonatomic, retain) NSMutableArray *grades;

GradePeriod 是这样定义的

@property (strong, nonatomic) NSString *period;
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *term;
@property (strong, nonatomic) NSString *teacher;
@property (strong, nonatomic) NSString *percent;
@property (strong, nonatomic) NSString *grade;
@property (strong, nonatomic) NSString *missing;
@property (strong, nonatomic) NSString *update;
@property (strong, nonatomic) NSString *url;

我不确定我做错了什么。 非常感谢!

【问题讨论】:

【参考方案1】:

我升级到最新版本的 RestKit 并自行修复。

【讨论】:

以上是关于RestKit 映射错误“无法将对象集合映射到非可变集合。”的主要内容,如果未能解决你的问题,请参考以下文章

RestKit 映射错误的对象

RestKit 映射错误“无法将对象集合映射到非可变集合。”

Restkit 对象映射两个类错误访问

Restkit 属性映射失败的转换错误

RestKit 0.20.0-rc1 - 如何映射多个错误参数?

RestKit 映射上的关系错误