For an array

    NSArray *array = @[@"111",@"222",@"333",@"444",@"555",@"666",@"777",@"888",@"999",];

    NSInteger count =array.count;
Copy the code

1. The for loop

for (NSInteger i=0; i<count; i++) {
   NSLog(@"%@----%@",array[i],[NSThread currentThread]);
}
Copy the code

2.for in quick enumeration

for (NSString *string in array) {
   NSLog(@"%@----%@",string,[NSThread currentThread]);
}
Copy the code

When there are many objects in the collection, the traversal speed of for in is very fast. But a small traversal is not as fast as a for loop.

3. Enumerator NSEnumerator

NSEnumerator *enumer = [array objectEnumerator]; NSEnumerator *enumer2 = [array reverseObjectEnumerator]; // reverse order id obj; While (obj = [enumer nextObject]) {// nextObject is nil, NSLog(@"%@----%@",obj,[NSThread currentThread]); }Copy the code

4. EnumerateObjectsUsingBlock method

/ / sequence traversal [array enumerateObjectsUsingBlock: ^ (id _Nonnull obj, NSUInteger independence idx, BOOL * _Nonnull stop) {NSLog(@"%@----%@",array[IDx],[NSThread currentThread]); if (idx == 5) {*stop = YES; }]; / / reverse traversal [array enumerateObjectsWithOptions: NSEnumerationReverse usingBlock: ^ (id _Nonnull obj, NSUInteger independence idx. BOOL * _Nonnull stop) {NSLog(@"%@----%@",array[IDx],[NSThread currentThread]); if (idx == 5) {*stop = YES; }];Copy the code

Code within a Block can be executed concurrently.

Dictionary case

    NSDictionary * dic = [NSDictionary dictionary];
    
    [dic enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
        NSLog(@"value for key %@ is %@ ", key, value);
        if ([@"key2" isEqualToString:key]) {
            *stop = YES;
        }
    }];
Copy the code

This is recommended when iterating over dictionary types. Dictionary traversal can take elements in both AllKeys and AllValues.

5. Multi-threaded dispatch_apply

// dispatch_queue_t queue = dispatch_queue_create(" ZZZ ", DISPATCH_QUEUE_CONCURRENT); dispatch_apply(count, queue, ^(size_t index) { NSLog(@"%@----%@",array[index],[NSThread currentThread]); });Copy the code

This is suitable for array traversal where each loop has a time-consuming task.

6.NSSet

NSArray *arr1 = .... ; NSArray *arr2 =.... ; NSSet *aaaSet = [NSSet setWithArray:arr2]; for (NSUInteger i = 0; i < arr1.count; ++i) { UIView *targetView = arr1[i]; if ([aaaSet containsObject:targetView]) { //.... }}Copy the code

Sort an array

NSArray * allKeys = @[@"2",@"1",@"4",@"8"]; // Sort the array. NSArray *sortedKeys = [allKeys sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) { NSNumber *number1 = [NSNumber numberWithInt:[obj1 intValue]]; NSNumber *number2 = [NSNumber numberWithInt:[obj2 intValue]]; NSComparisonResult result = [number1 compare:number2]; return result == Descending; // descending}];Copy the code
NSMutableArray *temp = [NSMutableArray arrayWithObjects:@"5",@"1",@"4",@"2",nil]; temp = (NSMutableArray *)[[temp reverseObjectEnumerator] allObjects];Copy the code
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"5",@"1",@"4",@"2",nil]; NSMutableArray *array = [NSMutableArray alloc] initWithObjects:@"5",@"1",@"4",@"2",nil] [array sortUsingSelector:@selector(compare:)]; NSEnumerator *enumerator = [array reverseObjectEnumerator]; array =[[NSMutableArray alloc]initWithArray: [enumerator allObjects]];Copy the code

www.jianshu.com/p/2fa53fa56…