• iOS开发中数组常用的五种遍历方式


    随着iOS的不断发展,apple也不断推出性能更高的数组遍历方式,下面将对熟悉的五种遍历方式进行列举。

    首先定义一个数组,并获取数组长度

        NSArray *array=@[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",];
        NSInteger count =array.count;

    一、for循环

    for (NSInteger i=0; i<count; i++) {
       NSLog(@"%@----%@",array[i],[NSThread currentThread]);
    }
    
    打印结果如下:
    2017-01-03 11:02:01.537 MyTestWorkProduct[2013:76489] 1----<NSThread: 0x600000260e80>{number = 1, name = main}
    2017-01-03 11:02:01.538 MyTestWorkProduct[2013:76489] 2----<NSThread: 0x600000260e80>{number = 1, name = main}
    2017-01-03 11:02:01.539 MyTestWorkProduct[2013:76489] 3----<NSThread: 0x600000260e80>{number = 1, name = main}
    2017-01-03 11:02:01.539 MyTestWorkProduct[2013:76489] 4----<NSThread: 0x600000260e80>{number = 1, name = main}
    2017-01-03 11:02:01.539 MyTestWorkProduct[2013:76489] 5----<NSThread: 0x600000260e80>{number = 1, name = main}
    2017-01-03 11:02:01.540 MyTestWorkProduct[2013:76489] 6----<NSThread: 0x600000260e80>{number = 1, name = main}
    2017-01-03 11:02:01.540 MyTestWorkProduct[2013:76489] 7----<NSThread: 0x600000260e80>{number = 1, name = main}
    2017-01-03 11:02:01.540 MyTestWorkProduct[2013:76489] 8----<NSThread: 0x600000260e80>{number = 1, name = main}
    2017-01-03 11:02:01.541 MyTestWorkProduct[2013:76489] 9----<NSThread: 0x600000260e80>{number = 1, name = main}
    2017-01-03 11:02:01.541 MyTestWorkProduct[2013:76489] end

    二、forin 快速枚举

     for (NSString *string in array) {
            NSLog(@"%@----%@",string,[NSThread currentThread]);
     }
    
    打印结果如下:
    2017-01-03 11:04:01.990 MyTestWorkProduct[2086:78292] 1----<NSThread: 0x60000007a400>{number = 1, name = main}
    2017-01-03 11:04:01.990 MyTestWorkProduct[2086:78292] 2----<NSThread: 0x60000007a400>{number = 1, name = main}
    2017-01-03 11:04:01.991 MyTestWorkProduct[2086:78292] 3----<NSThread: 0x60000007a400>{number = 1, name = main}
    2017-01-03 11:04:01.992 MyTestWorkProduct[2086:78292] 4----<NSThread: 0x60000007a400>{number = 1, name = main}
    2017-01-03 11:04:01.992 MyTestWorkProduct[2086:78292] 5----<NSThread: 0x60000007a400>{number = 1, name = main}
    2017-01-03 11:04:01.993 MyTestWorkProduct[2086:78292] 6----<NSThread: 0x60000007a400>{number = 1, name = main}
    2017-01-03 11:04:01.993 MyTestWorkProduct[2086:78292] 7----<NSThread: 0x60000007a400>{number = 1, name = main}
    2017-01-03 11:04:01.993 MyTestWorkProduct[2086:78292] 8----<NSThread: 0x60000007a400>{number = 1, name = main}
    2017-01-03 11:04:01.994 MyTestWorkProduct[2086:78292] 9----<NSThread: 0x60000007a400>{number = 1, name = main}
    2017-01-03 11:04:01.994 MyTestWorkProduct[2086:78292] end

    三、NSEnumerator

     NSEnumerator *enumer=[array objectEnumerator];
        id obj;
        while (obj=[enumer nextObject]) {
            NSLog(@"%@----%@",obj,[NSThread currentThread]);
        }
    打印结果如下:
    2017-01-03 11:05:37.138 MyTestWorkProduct[2156:79788] 1----<NSThread: 0x608000078980>{number = 1, name = main}
    2017-01-03 11:05:37.139 MyTestWorkProduct[2156:79788] 2----<NSThread: 0x608000078980>{number = 1, name = main}
    2017-01-03 11:05:37.140 MyTestWorkProduct[2156:79788] 3----<NSThread: 0x608000078980>{number = 1, name = main}
    2017-01-03 11:05:37.140 MyTestWorkProduct[2156:79788] 4----<NSThread: 0x608000078980>{number = 1, name = main}
    2017-01-03 11:05:37.140 MyTestWorkProduct[2156:79788] 5----<NSThread: 0x608000078980>{number = 1, name = main}
    2017-01-03 11:05:37.141 MyTestWorkProduct[2156:79788] 6----<NSThread: 0x608000078980>{number = 1, name = main}
    2017-01-03 11:05:37.141 MyTestWorkProduct[2156:79788] 7----<NSThread: 0x608000078980>{number = 1, name = main}
    2017-01-03 11:05:37.141 MyTestWorkProduct[2156:79788] 8----<NSThread: 0x608000078980>{number = 1, name = main}
    2017-01-03 11:05:37.142 MyTestWorkProduct[2156:79788] 9----<NSThread: 0x608000078980>{number = 1, name = main}
    2017-01-03 11:05:37.142 MyTestWorkProduct[2156:79788] end

    四、快速遍历

    //顺序遍历
        [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSLog(@"%@----%@",array[idx],[NSThread currentThread]);
        }];
        //倒序遍历
        [array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSLog(@"%@----%@",array[idx],[NSThread currentThread]);
        }];
    打印结果如下:
    2017-01-03 11:07:30.774 MyTestWorkProduct[2229:81188] 1----<NSThread: 0x600000070240>{number = 1, name = main}
    2017-01-03 11:07:30.775 MyTestWorkProduct[2229:81188] 2----<NSThread: 0x600000070240>{number = 1, name = main}
    2017-01-03 11:07:30.775 MyTestWorkProduct[2229:81188] 3----<NSThread: 0x600000070240>{number = 1, name = main}
    2017-01-03 11:07:30.776 MyTestWorkProduct[2229:81188] 4----<NSThread: 0x600000070240>{number = 1, name = main}
    2017-01-03 11:07:30.777 MyTestWorkProduct[2229:81188] 5----<NSThread: 0x600000070240>{number = 1, name = main}
    2017-01-03 11:07:30.778 MyTestWorkProduct[2229:81188] 6----<NSThread: 0x600000070240>{number = 1, name = main}
    2017-01-03 11:07:30.778 MyTestWorkProduct[2229:81188] 7----<NSThread: 0x600000070240>{number = 1, name = main}
    2017-01-03 11:07:30.779 MyTestWorkProduct[2229:81188] 8----<NSThread: 0x600000070240>{number = 1, name = main}
    2017-01-03 11:07:30.779 MyTestWorkProduct[2229:81188] 9----<NSThread: 0x600000070240>{number = 1, name = main}
    2017-01-03 11:07:30.780 MyTestWorkProduct[2229:81188] 9----<NSThread: 0x600000070240>{number = 1, name = main}
    2017-01-03 11:07:30.781 MyTestWorkProduct[2229:81188] 8----<NSThread: 0x600000070240>{number = 1, name = main}
    2017-01-03 11:07:30.781 MyTestWorkProduct[2229:81188] 7----<NSThread: 0x600000070240>{number = 1, name = main}
    2017-01-03 11:07:30.782 MyTestWorkProduct[2229:81188] 6----<NSThread: 0x600000070240>{number = 1, name = main}
    2017-01-03 11:07:30.782 MyTestWorkProduct[2229:81188] 5----<NSThread: 0x600000070240>{number = 1, name = main}
    2017-01-03 11:07:30.783 MyTestWorkProduct[2229:81188] 4----<NSThread: 0x600000070240>{number = 1, name = main}
    2017-01-03 11:07:30.784 MyTestWorkProduct[2229:81188] 3----<NSThread: 0x600000070240>{number = 1, name = main}
    2017-01-03 11:07:30.785 MyTestWorkProduct[2229:81188] 2----<NSThread: 0x600000070240>{number = 1, name = main}
    2017-01-03 11:07:30.786 MyTestWorkProduct[2229:81188] 1----<NSThread: 0x600000070240>{number = 1, name = main}
    2017-01-03 11:07:30.786 MyTestWorkProduct[2229:81188] end

    五、快速迭代

    //将block中的任务,逐个放到queue中,然后进行dispatch_sync执行
        //多线程同步循环
        
        dispatch_queue_t queue =dispatch_queue_create("apply并行队列", DISPATCH_QUEUE_CONCURRENT);
        dispatch_apply(count, queue, ^(size_t index) {
            NSLog(@"%@----%@",array[index],[NSThread currentThread]);
        });

          NSLog(@"end");

    
    
    打印结果如下:
    2017-01-03 11:09:00.114 MyTestWorkProduct[2302:82847] 1----<NSThread: 0x60000006b940>{number = 1, name = main}
    2017-01-03 11:09:00.114 MyTestWorkProduct[2302:82977] 3----<NSThread: 0x600000876cc0>{number = 7, name = (null)}
    2017-01-03 11:09:00.114 MyTestWorkProduct[2302:83008] 2----<NSThread: 0x600000479a80>{number = 6, name = (null)}
    2017-01-03 11:09:00.114 MyTestWorkProduct[2302:83482] 4----<NSThread: 0x60800067e740>{number = 8, name = (null)}
    2017-01-03 11:09:00.114 MyTestWorkProduct[2302:82847] 5----<NSThread: 0x60000006b940>{number = 1, name = main}
    2017-01-03 11:09:00.115 MyTestWorkProduct[2302:82977] 6----<NSThread: 0x600000876cc0>{number = 7, name = (null)}
    2017-01-03 11:09:00.115 MyTestWorkProduct[2302:83008] 7----<NSThread: 0x600000479a80>{number = 6, name = (null)}
    2017-01-03 11:09:00.116 MyTestWorkProduct[2302:83482] 8----<NSThread: 0x60800067e740>{number = 8, name = (null)}
    2017-01-03 11:09:00.116 MyTestWorkProduct[2302:82847] 9----<NSThread: 0x60000006b940>{number = 1, name = main}
    2017-01-03 11:09:00.116 MyTestWorkProduct[2302:82847] end

    根据打印结果最后才打印 end 字符可知,dispatch_apply会等待每个block中的任务都执行完成后,才往下执行。

    总结:从这五种遍历方式来看,第5种采用的多线程,对于处理耗时的数组遍历比较适用。

  • 相关阅读:
    Python 爬虫-正则表达式
    Python 爬虫-信息的标记xml,json,yaml
    Python 爬虫-BeautifulSoup
    bzoj 1491
    bzoj 1406 数论
    Codeforces Round #496 (Div. 3) E2
    2017-2018 ACM-ICPC Northern Eurasia (Northeastern European Regional) Contest (NEERC 17) 日常训练
    Codeforces Round #496 (Div. 3) F
    bzoj 1415 期望dp + 记忆化搜索
    bzoj 1483 链表 + 启发式合并
  • 原文地址:https://www.cnblogs.com/zhou--fei/p/6244171.html
Copyright © 2020-2023  润新知