ios - 尝试删除 NSUserdefault 数组中的 TableView 行

标签 ios ios5

我正在尝试删除 TableView 中的一行,然后将更新的 TableView 保存到 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] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object'

我做错了什么?

最佳答案

尝试更改行:

NSMutableArray *stringsArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"history"];

到:

NSMutableArray *stringsArray = [NSMutableArray arrayWithArray: [[NSUserDefaults standardUserDefaults] arrayForKey:@"history"]];

关于ios - 尝试删除 NSUserdefault 数组中的 TableView 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10160783/

相关文章:

ios - iOS5 的 NSURLConnection 超时是什么情况?

ios - 运行有限长度的后台任务

呈现具有不成比例的轴值的交互式时间轴的 JavaScript 库?

ios - 如何检查哪个类属于AnyObject?

ios - 仅重新排序 UITableView 部分

ios - iPad Web 应用启动画面在横向模式下无法工作

ios - 如何在播放 iPod 库时获取音乐的音频样本?

ios - 如何在 swift 5 中缓存图像?

iphone - Xcode错误语义错误值可能无法响应 'initWithFrame:image Name:'

iphone - 如何将来自服务器的图像设置为矩形按钮的背景图像