iOS常见遍历方法汇总
一。for循环
NSArray *iosArray = @[@"L", @"O", @"V", @"E", @"I", @"U", @"T"];
for (int i = 0; i < iosArray.count; i++) {
//处理数组中数据
NSLog(@"%@", iosArray[i]);
}
二。快速遍历(for...in)
NSArray *iosArray = @[@"L", @"O", @"V", @"E", @"I", @"U", @"T"];
for (NSString *obj in iosArray) {
//处理数组中的数据
NSLog(@"%@", obj);
}
三。NSEnumerator(NSEnumerator是一个抽象的基类)
NSArray *iosArray = @[@"L", @"O", @"V", @"E", @"I", @"U", @"T"];
NSMutableArray *Array = [NSMutableArray arrayWithArray:iosArray];
//并发遍历
[Array enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
obj = [NSString stringWithFormat:@"_%@", obj];
[Array replaceObjectAtIndex:idx withObject:obj];
NSLog(@"%@", obj);
if ([obj isEqualToString:@"_I"]) {
*stop = YES;
}
}];
// 正向遍历
[iosArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"----%@", obj);
if ([obj isEqualToString:@"E"]) {
*stop = YES;
}
}];
// 反向遍历
[iosArray enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@", obj);
if ([obj isEqualToString:@"E"]) {
*stop = YES;
}
}];