隨著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種採用的多執行緒,對於處理耗時的陣列遍歷比較適用。