16

I stumbled across the following shortcut in setting up a for loop (shortcut compared to the textbook examples I have been using):

for (Item *i in items){ ... }

As opposed to the longer format:

for (NSInteger i = 0; i < [items count]; i++){ ... } //think that's right

If I'm using the shorter version, is there a way to remove the item currently being iterated over (ie 'i')? Or do I need to use the longer format?

Ben Packard
  • 26,102
  • 25
  • 102
  • 183
  • The first for loop is known as the enhanced FOR loop, and the second one is the old standard FOR loop. I learn this when doing my Java certification. – Popeye Aug 10 '12 at 21:58
  • If you really need to remove items from the array while iterating it and you can't remove them afterwards using `removeObjectsInArray:` or `removeObjectsAtIndexes:` like @Vladimir suggested, you can always iterate over a copy of the original array. – Tiago Aug 04 '14 at 16:14

5 Answers5

32

You cannot remove objects from array while fast-enumerating it:

numeration is “safe”—the enumerator has a mutation guard so that if you attempt to modify the collection during enumeration, an exception is raised.

Anyway why do you need to change you container while enumerating it? Consider storing elements that need to be deleted and remove them from your container using removeObjectsInArray: or removeObjectsAtIndexes: method.

Beltalowda
  • 4,558
  • 2
  • 25
  • 33
Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • Thanks. I am changing it while enumerating it because it is a temporary queue. I'm moving all the items to another array. – Ben Packard May 03 '10 at 18:27
  • 1
    If you are moving *all* the objects to another array, try the following: `[destinationArray addObjectsFromArray: sourceArray]; [sourceArray removeAllObjects];` – JeremyP May 04 '10 at 08:29
9

Just add keyword break; after removing the item...

for(id item in items) {
    if([item isEqual:itemToDelete]) {
        [items removeObject:item];
        break; // A very important line 
    }
}
Ahmad Labeeb
  • 1,056
  • 11
  • 21
RaiderF
  • 107
  • 1
  • 2
  • 4
    @Abdullah71 with id you never use *. – rptwsthi Jul 05 '13 at 06:36
  • 1
    This will remove one object and stop iterating, thus missing possible other objects that should be removed. So only use this if you need to remove zero to one object per iteration. – marsbear Aug 18 '16 at 15:34
7

An Objective-C collection must not be modified during enumeration.

You may use this variant to delete objects from collection:

for (NSInteger i = items.count - 1; i >= 0 ; i--) {
   [items removeObjectAtIndex:i];
}
serhii
  • 141
  • 2
  • 6
  • 1
    Imho the shortest way to do it properly, given that it's fine with your business logic to traverse the array from back to front. – marsbear Aug 18 '16 at 15:37
1

The former loop is a "for-each" loop in Objective C.

*i is a pointer to the direct item in the items-Array (most of the time this will be NSMutableArray).

This way you can operate directly on the item:

[items removeObject: i];

This (should) work - I am currently not working on my Mac and can't check it. However it might be that Objective-C Prevents removing objects while iterating over the collection (that is quite common in most languages).

BergmannF
  • 9,727
  • 3
  • 37
  • 37
  • It is indeed the case that you can't remove (or mutate) an `NSMutableArray` while enumerating over it, so the `removeObject:` call will raise an exception. (as explained by Vladimir in his answer) – Nick Forge May 03 '10 at 10:07
0

I use this code for this:

for (NSUInteger i = [items count] - 1; ; i--) {
     [items removeObjectAtIndex:i];
}
Kristijan Iliev
  • 4,901
  • 10
  • 28
  • 47
slava
  • 9
  • 1