Traverse the array to delete multiple elements in the array

When we traverse the variable array, it is best not to delete elements in the array.
Because the delete operation may cause changes in the capacity of the array, causing problems such as array out of bounds.
Previously encountered this problem when using for loop traversal.
At that time, the practice was to use enumerateObjectsUsingBlock:, but when I encountered this problem again this time, I tested for, for in, enumerateObjectsUsingBlock: by the way.
The experimental results are as follows:

NSMutableArray *array = [NSMutableArray arrayWithArray:@[@"1",@"2",@"3",@"4",@"5"]];
[array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    if ([obj isEqualToString:@"3"]) {
        [array removeObject:obj];
    }
}];
NSLog(@"%@",array);

// 输出结果:(1,2,4,5)

The result is normal.

Use for in:

NSMutableArray *array = [NSMutableArray arrayWithArray:@[@"1",@"2",@"3",@"4",@"5"]];
for (NSString *obj in array) {
    if ([obj isEqualToString:@"3"]) {
        [array removeObject:obj];
    }
}
NSLog(@"%@",array);

The result is a crash, the information is as follows:

2016-11-11 22:48:06.886 Solutions[15297:2231002] *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x1001060b0> was mutated while being enumerated.'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff929c14f2 __exceptionPreprocess + 178
    1   libobjc.A.dylib                     0x00007fff8fcb2f7e objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff92a2815c __NSFastEnumerationMutationHandler + 300
    3   Solutions                           0x0000000100000d1c main + 508
    4   libdyld.dylib                       0x00007fff90cd05ad start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Use for:

NSMutableArray *array = [NSMutableArray arrayWithArray:@[@"1",@"2",@"3",@"4",@"5"]];
for (int i = 0; i < array.count; i++) {
    NSString *obj = array[i];
    if ([obj isEqualToString:@"3"]) {
        [array removeObject:obj];
    }
}
NSLog(@"%@",array);
// 输出结果是:(1,2,4,5)

The result is normal. It may be that Apple has made corrections to the internal processing of the ordinary for loop. In the past, writing this way would cause an error.

In fact, the most correct and safe solution is to traverse the array in reverse order, and then delete the elements.

For example, reverse traversal of for in:

NSMutableArray *array = [NSMutableArray arrayWithArray:@[@"1",@"2",@"3",@"4",@"5"]];
for (NSString *obj in array.reverseObjectEnumerator) {
    if ([obj isEqualToString:@"3"]) {
        [array removeObject:obj];
    }
}
NSLog(@"%@",array);
// 输出结果:(1,2,4,5)

use

NSMutableArray *array = [NSMutableArray arrayWithArray:@[@"1",@"2",@"3",@"4",@"5"]];
[array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isEqualToString:@"3"]) {
                [array removeObject:obj];
            }
        }];

NSLog(@"%@",array);
// 输出结果:(1,2,4,5)

On the reverse order of for

NSMutableArray *array = [NSMutableArray arrayWithArray:@[@"1",@"2",@"3",@"4",@"5"]];
for (int i = array.count - 1; i >= 0; i--) {
    NSString *obj = array[i];
    if ([obj isEqualToString:@"3"]) {
        [array removeObject:obj];
    }
}
NSLog(@"%@",array);
// 输出结果:(1,2,4,5)

Guess you like

Origin blog.csdn.net/woruosuifenglang/article/details/78466557