'NSRangeException',原因:'*

编程入门 行业动态 更新时间:2024-10-26 16:27:19
本文介绍了'NSRangeException',原因:'*-[__ NSArrayM objectAtIndex:]:索引2超出范围[0 .. 1]'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试删除一些项目,但是我收到此NSException:

I'm trying to delete some items but i'm receiving this NSException:

"NSRangeException",原因:" * -[__ NSArrayM objectAtIndex:]:索引2超出范围[0 .. 1]"

'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'

这是我的代码:

-(void)deletePressed:(id)sender { if (data.count > 0) { NSString *path = [NSHomeDirectory() stringByAppendingString:@"/Documents/Galeria/"]; NSFileManager *manager = [NSFileManager defaultManager]; for (NSIndexPath *indexPath in itensSelecionados) { NSString *result = [path stringByAppendingFormat:@"%@", [[manager contentsOfDirectoryAtPath:path error:nil] objectAtIndex:indexPath.row]]; [manager removeItemAtPath:result error:nil]; } [self viewWillAppear:YES]; }}

有人可以帮忙吗?

推荐答案

您无法从要迭代的数组中删除对象.可能没有适合您的解决方案.

You can't remove objects from an array that you are iterating through. There may be few solutions for you.

一种方法是使用另一个可变数组,该数组将保存所有应删除的对象,然后对其进行迭代并从原始数组中删除这些对象:

One is to use an additional mutable array that will hold all the objects that should be deleted and then iterate through it and remove the objects from the original array:

-(void)deletePressed:(id)sender { if (data.count > 0) { NSString *path = [NSHomeDirectory() stringByAppendingString:@"/Documents/Galeria/"]; NSFileManager *manager = [NSFileManager defaultManager]; NSMutableArray *filesToDelete = [NSMutableArray array]; // Build a list of files to delete for (NSIndexPath *indexPath in itensSelecionados) { NSString *result = [path stringByAppendingFormat:@"%@", [[manager contentsOfDirectoryAtPath:path error:nil] objectAtIndex:indexPath.row]]; [filesToDelete addObject:result]; } // Actually delete the files for (NSString *indexPathString in filesToDelete) { [manager removeItemAtPath:indexPathString error:nil]; } // Why do you call viewWillAppear directly ?? [self viewWillAppear:YES]; } }

编辑 由于Thiago的建议,在第二次迭代中将 NSIndexPath 修复为 NSString .

更多推荐

'NSRangeException',原因:'*

本文发布于:2023-10-18 10:35:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1503979.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:原因   NSRangeException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!