好的,我有这个代码用于映射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;
我不确定我做错了什么。 非常感谢!
答案 0 :(得分:0)
我升级到最新版本的RestKit并自行修复。