gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:36:44 25 4
gpt4 key购买 nike

我正在尝试删除 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com