“*** 由于未捕获的异常 'NSRangeException' 而终止应用程序,原因:'*** -[__NSArrayM objectAtIndex:]:索引 1 超出范围 [0 .. 0]'”

"*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'"

我知道这是 Stack Overflow 上的一个常见问题,但不幸的是 none 已经能够引导我找到我的解决方案。

我试图在 UITableView 中列出我的文档目录中的文件。 (我还有另一个 tableview 显示我连接的设备)

我知道我收到这个错误是因为它说我的数组有 0 个对象,而我正在尝试访问索引 1 处的对象。

但是我的文档目录中有一个文件。 如果我有 2 个文件,当我删除 1 个时,会出现此错误。当应用程序重新启动时,它会正确显示 UITableView 并删除文件。

我从这里得到了我的代码: Objective-C: How to list files from documents directory into a UITableView?

这是我的内容:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == _tblConnectedDevices){
    return [_arrConnectedDevices count];
    }
    if ([filePathsArray count] > 0){
        return [filePathsArray count];
    }
    else 
    return 1;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == _tblConnectedDevices){
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
    }

    cell.textLabel.text = [_arrConnectedDevices objectAtIndex:indexPath.row];

    return cell;
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];
        NSString *last = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:indexPath.row]];
        NSString *last2 = [[last lastPathComponent] stringByDeletingPathExtension];
        cell.textLabel.text = last2;
        return cell;
    }
}

我的删除无效:

-(IBAction)deleteFile:(id)sender{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask,
                                                         YES);
    NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt", fileName]];
    NSError *error;
        [[NSFileManager defaultManager] removeItemAtPath:fullPath error:&error];
    [_cellView reloadData];
}

你应该重构你的代码。然而:

我假设您在这一行中遇到了错误:

NSString *last = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:indexPath.row]];

出了什么问题:

一个。您有一个包含内存中路径的数组。当 table 视图询问您项目的数量时,您 return 该数组的计数。

乙。当 table 视图连续请求一个项目时,您将读取目录。 (好吧,这个方法应该没有那么快。)

filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];

然后你找索引为indexPath.row的文件:

NSString *last = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:indexPath.row]];

这 "works"(这类似于工作)只要目录中的文件至少与数组中的文件一样多。

当您从目录中删除文件时,它仍然是数组中的一个项目。因此,您对 table 视图说,行数与数组中的项一样多。当 table 视图试图获取它时,您查看磁盘并找到较少的文件(删除的文件丢失了)。因此,您从磁盘获得的数组比内存中的项目少了一项。

解决方法:更新您的内存数组。或者做出决定:在内存中工作在磁盘上工作。

在 deleteFile 中重新加载您的 filePathArray:同时设置断点并查看数组中有多少文件