Since you are iterating through an array that is changing throughout the iteration, you'll have to use a while-loop with the condition as an index being smaller than the size of the array, like so:
NSMutableArray *arrayContents = [NSMutableArray arrayWithObjects: @[@"abc", @"def"],@[@1,@2,@3], @[], nil];
NSLog( @"Pre while-loop: \n%@", arrayContents);
NSUInteger index = 0;
while (index < [arrayContents count]) {
BOOL didRemove = false;
if ([[arrayContents objectAtIndex: index] count] == 0) {
[arrayContents removeObjectAtIndex: index];
didRemove = true;
}
if (didRemove == false)
index += 1;
}
NSLog( @"Post while-loop: \n%@", arrayContents);
If nothing was removed, you would increment index
, but if something was removed, you don't increment the index
. Instead you loop again at the same index
, because if you increase the index after removing, you'll "skip" an iteration.
Hope this works for your situation :)