我正在尝试删除表视图中的行,然后将更新的表视图保存到NSUserdefault中的数组。它看起来像这样:
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableArray *stringsArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"history"];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSMutableArray *history = [NSMutableArray arrayWithArray:[prefs objectForKey:@"history"]];
NSUInteger row = [indexPath row];
NSUInteger count = [stringsArray count];
if (row < count) {
[stringsArray removeObjectAtIndex:row];
[prefs setObject:history forKey:@"history"];
[[NSUserDefaults standardUserDefaults] synchronize];
[tableView reloadData];
NSLog(@"tabort %d", row);
NSLog(@"tabort %d", count);
NSLog(@"tabort %@", stringsArray);
}
}
当我运行它时,我可以删除一行,但它不会被保存,当我尝试删除下一行时,它会收到以下消息:
2012-04-15 11:02:58.704 test220 [88546:f803] * 终止应用程序 未捕获的异常'NSInternalInconsistencyException',原因: ' - [__ NSCFArray removeObjectAtIndex:]:发送变异方法 不可变对象'
我做错了什么?
答案 0 :(得分:3)
尝试更改行:
NSMutableArray *stringsArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"history"];
为:
NSMutableArray *stringsArray = [NSMutableArray arrayWithArray: [[NSUserDefaults standardUserDefaults] arrayForKey:@"history"]];