Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c UITableView提交编辑错误_Objective C_Ios_Cocoa Touch - Fatal编程技术网

Objective c UITableView提交编辑错误

Objective c UITableView提交编辑错误,objective-c,ios,cocoa-touch,Objective C,Ios,Cocoa Touch,我有一个UITableView,其中填充了从NSUserDefaults读取的可变数组。我希望用户能够删除当前的一些项目。以下是特定于编辑方法的代码,以下是整个文件: 当我单击“编辑”并删除某个项目时,应用程序崩溃,我返回: 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“-[\uu nsFarray removeObjectAtIndex:]:正在将方法发送到不可变对象” 我的具体问题是,我在这里做错了什么 // Override

我有一个UITableView,其中填充了从NSUserDefaults读取的可变数组。我希望用户能够删除当前的一些项目。以下是特定于编辑方法的代码,以下是整个文件:

当我单击“编辑”并删除某个项目时,应用程序崩溃,我返回:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“-[\uu nsFarray removeObjectAtIndex:]:正在将方法发送到不可变对象”

我的具体问题是,我在这里做错了什么

 // Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:    (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {

    // edit list of names 
    if ([listOfNames count] >= 1) {
        [tableView beginUpdates];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [listOfNames removeObjectAtIndex:[indexPath row]];

       // write updated listofnames to nsuserdefaults
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

         [[NSUserDefaults standardUserDefaults] setObject:listOfNames forKey:@"My Key"];

        [defaults synchronize];

        if ([listOfNames count] == 0) {
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
        [tableView endUpdates];
    }


}   
else if (editingStyle == UITableViewCellEditingStyleInsert) {
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}   
}

您的
listOfNames
实例变量是不可变的,因此无法从中删除对象

更改:

listOfNames = [[defaults objectForKey:@"My Key"] copy];


在您的
-视图中将出现:
viewDidLoad
方法。

Gah已将错误版本的文件复制/粘贴回实时项目。我一直在说。。“我知道我改变了。”。。谢谢你,格雷弗。当它允许我时,将选择此为正确。
listOfNames = [[defaults objectForKey:@"My Key"] mutableCopy];