0

我正在尝试删除表视图中的一行,然后将更新的表视图保存到 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:]:发送到不可变对象的变异方法”

我究竟做错了什么?

4

1 回答 1

3

尝试换行:

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

至:

NSMutableArray *stringsArray = [NSMutableArray arrayWithArray: [[NSUserDefaults standardUserDefaults] arrayForKey:@"history"]];
于 2012-04-15T09:17:16.227 回答