0

好的,所以我有这段代码,我用它来映射来自 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;

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

4

1 回答 1

0

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

于 2013-03-06T00:44:35.823 回答