1

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?

2 Answers2

2

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];
    }
}

EDIT
Fixed the NSIndexPath to NSString in the second iteration due to Thiago's advise.

Michael Kessler
  • 14,245
  • 13
  • 50
  • 64
  • Thanks it works, but i need made some modifications in code. The second forin is: for (NSString *stringToDelete in filesToDelete) { [manager removeItemAtPath:stringToDelete error:nil]; } i call viewWillAppear, coz i need that table reload everytime the tab is select –  Nov 18 '12 at 21:24
  • I still think that you shouldn't call the `viewWillAppear` directly. You are better move all the code from that method to another one and call it from both places... – Michael Kessler Nov 18 '12 at 21:31
  • Thanks, thats a good idea, i'll create another method to reload table, without call viewWillAppear –  Nov 18 '12 at 21:41
0

You need to do the deletion in reverse row order. Lets say you have 3 rows and you want to delete the rows at index 0 and 2.

If you delete the row at index 0 first, then when you try to delete the row at index 2, it crashes because now there are only 2 rows left.

If you delete the row at index 2 first, then index 0, everything will be OK.

rmaddy
  • 314,917
  • 42
  • 532
  • 579