0
好吧,所以我使用了這個代碼來映射來自JSON API的數據。一切都正常運行,但後來我得到這個錯誤:RestKit映射錯誤「無法將對象集合映射到不可變集合。」
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;
我不確定我做錯了什麼。 非常感謝!