How to remove elements in NSMutableArray or NSMutableDictionary during enumeration?

前端 未结 3 417
梦毁少年i
梦毁少年i 2021-01-04 10:15

I am using block based enumeration similar to the following code:

[[[rows objectForKey:self.company.coaTypeCode] objectForKey:statementType] 
    enumerateOb         


        
3条回答
  •  鱼传尺愫
    2021-01-04 10:31

    Since you can't remove objects from an array or dictionary during enumeration, you'll have to accumulate the items you want to delete, and then delete them all after the enumeration.

    If you're dealing with an array, you can just accumulate the indices.:

    NSMutableIndexSet *indexesToDelete = [NSMutableIndexSet indexSet];
    NSUInteger currentIndex = 0;
    
    for (id obj in yourArray) {
        //do stuff with obj
        if (shouldBeDeleted(obj)) {
            [indexesToDelete addIndex:currentIndex];
        }
        currentIndex++;
    }
    
    [yourArray removeObjectsAtIndexes:indexesToDelete];
    

    Since the order of the keys in an NSDictionary is undefined, for an NSMutableDictionary you'll have to accumulate keys instead:

    NSMutableArray *keysToDelete = [NSMutableArray array];
    
    for (id obj in [yourDictionary keyEnumerator]) {
        //do stuff with obj
        if (shouldBeDeleted(obj)) {
            [keysToDelete addObject:obj];
        }
    }
    
    [yourDictionary removeObjectsForKeys:keysToDelete];
    

    It's the same thing if you're enumerating with a block. Declare the enumerator in the same scope where you declare the block and it will be retained and just work.

    Also worth looking at this question from 3 years ago: Best way to remove from NSMutableArray while iterating?.

提交回复
热议问题