xcode “*** 由于未捕获的异常‘NSRangeException’而终止应用程序,原因:‘*** -[__NSArrayM objectAtIndex:]:索引 1 超出范围 [0 .. 0]’”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29233736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'"
提问by Manesh
I know this is a common question on Stack Overflow, but unfortunately none have been able to lead me to my solution.
我知道这是 Stack Overflow 上的一个常见问题,但不幸的是没有人能够引导我找到我的解决方案。
I'm trying to list the files in my documents directory in a UITableView
. (I also have another tableview displaying devices I'm connected to)
我正在尝试将我的文档目录中的文件列出在UITableView
. (我还有另一个 tableview 显示我连接的设备)
I know I'm getting this error because its saying that my array has 0 objects, and I'm trying to access the object at index 1.
我知道我收到此错误是因为它说我的数组有 0 个对象,而我正在尝试访问索引 1 处的对象。
But I have a file in my documents directory.
If I have 2 files, when I delete 1, I get this error. When the app is relaunched, it shows the UITableView
correctly with the file deleted.
但是我的文档目录中有一个文件。如果我有 2 个文件,当我删除 1 个文件时,会出现此错误。重新启动应用程序时,它会UITableView
正确显示已删除的文件。
I got my code from here: Objective-C: How to list files from documents directory into a UITableView?
我从这里得到我的代码: Objective-C: How to list files from documents directory into a UITableView?
this is my content:
这是我的内容:
-(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;
}
}
My delete void:
我的删除无效:
-(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];
}
回答by Pavel Gatilov
Reload your filePathArray in deleteFile: Also set breakpoint and look how many files you have in array
在 deleteFile 中重新加载 filePathArray:同时设置断点并查看数组中有多少文件
回答by Amin Negm-Awad
You should refactor your code. However:
你应该重构你的代码。然而:
I assume that you get the error in this line:
我假设您在此行中收到错误:
NSString *last = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:indexPath.row]];
What's going wrong:
出了什么问题:
A. You have an array with the paths in-memory. When the table view asks you for the number of items, you return the count of that array.
A. 你有一个包含内存中路径的数组。当表视图询问您项目的数量时,您返回该数组的计数。
B. When the table view asks for an item in a row, you read the directory. (Well, that's not that fast the method should be.)
B. 当表格视图要求连续输入一个项目时,您读取目录。(嗯,这个方法应该没有那么快。)
filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
Then you look for the file with the index of indexPath.row:
然后查找索引为 indexPath.row 的文件:
NSString *last = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:indexPath.row]];
This "works" (it is akin of working) as long as you have at least as many files in the directory as in your array.
只要目录中的文件数至少与数组中的文件数一样多,此“工作”(类似于工作)即可。
When you remove the file from the directory, it is still an item in the array. So you say to the table view that there are as many rows as items in the array. When the table view tries to get it, you look to the disk and find less files (the deleted is missed). So the array you get from disk has one item less than the items in-memory.
当您从目录中删除文件时,它仍然是数组中的一个项目。因此,您对表视图说,数组中的行数与项目数一样多。当表视图尝试获取它时,您查看磁盘并找到较少的文件(丢失的已删除)。因此,您从磁盘获得的数组比内存中的项目少一项。
To fix it: Update your in-memory array. Or make a decision: Work in-memory orwork on disk.
修复它:更新你的内存数组。或者做出决定:在内存中工作还是在磁盘上工作。