错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object'
The Code:
.h
@property (nonatomic, strong) NSMutableArray *history;
.m
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSString *de = [[self.history objectAtIndex:indexPath.row] objectForKey:@"des"];
NSString *dest = [de stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *jsonURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://example.com/rm_history.php?user_id=%@&destinations=%@",self.uID,dest]];
NSData *jsonData = [NSData dataWithContentsOfURL:jsonURL];
[self.history removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]
withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
NSLog(@"create");
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
[self.tableView reloadData];
}
在设置了断点以捕获错误后,它会停在这一行:
[self.history removeObjectAtIndex:indexPath.row];
有什么想法导致这个问题的出现吗?
history
属性分配什么值?它是在哪里创建的? - David Rönnqvistself.history
可能被声明为不可变的,因此应该使用NSArray
而不是NSMutableArray
。 - Volkerself.history = [dataDictionary objectForKey:@"ds_history"];
,其中dataDictionary
是数据字典。 - user3405229NSArray
赋给了self.history
,而不是NSMutableArray
。您需要查找每个设置self.history
的代码位置来确定问题所在。请注意,如果dataDictionary
为键ds_history
分配了一个数组,则由您决定是确保它实际上是一个NSMutableArray
还是调用self.history = [[dataDictionary objectForKey:@"ds_history"] mutableCopy]
。 - Robotic Cat