I’m trying to delete some items but i’m receiving this NSException:
‘NSRangeException’, reason: ‘* -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]’
Here is my code:
-(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];
}}
Anyone could help?
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:
EDIT
Fixed the
NSIndexPathtoNSStringin the second iteration due to Thiago’s advise.