随着iOS的不断发展,apple也不断推出性能更高的数组遍历方式,下面将对熟悉的五种遍历方式进行列举。
首先定义一个数组,并获取数组长度
NSArray *array=@[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",];
NSInteger count =array.count;
一、for循环
for(NSInteger i=0; i
NSLog(@"%@----%@",array[i],[NSThread currentThread]);
}
打印结果如下:2017-01-0311:02:01.537MyTestWorkProduct[2013:76489]1----{number =1, name = main}2017-01-0311:02:01.538MyTestWorkProduct[2013:76489]2----{number =1, name = main}2017-01-0311:02:01.539MyTestWorkProduct[2013:76489]3----{number =1, name = main}2017-01-0311:02:01.539MyTestWorkProduct[2013:76489]4----{number =1, name = main}2017-01-0311:02:01.539MyTestWorkProduct[2013:76489]5----{number =1, name = main}2017-01-0311:02:01.540MyTestWorkProduct[2013:76489]6----{number =1, name = main}2017-01-0311:02:01.540MyTestWorkProduct[2013:76489]7----{number =1, name = main}2017-01-0311:02:01.540MyTestWorkProduct[2013:76489]8----{number =1, name = main}2017-01-0311:02:01.541MyTestWorkProduct[2013:76489]9----{number =1, name = main}2017-01-0311:02:01.541MyTestWorkProduct[2013:76489] end
二、forin 快速枚举
for(NSString *stringin array) {
NSLog(@"%@----%@",string,[NSThread currentThread]);
}
打印结果如下:2017-01-0311:04:01.990MyTestWorkProduct[2086:78292]1----{number =1, name = main}2017-01-0311:04:01.990MyTestWorkProduct[2086:78292]2----{number =1, name = main}2017-01-0311:04:01.991MyTestWorkProduct[2086:78292]3----{number =1, name = main}2017-01-0311:04:01.992MyTestWorkProduct[2086:78292]4----{number =1, name = main}2017-01-0311:04:01.992MyTestWorkProduct[2086:78292]5----{number =1, name = main}2017-01-0311:04:01.993MyTestWorkProduct[2086:78292]6----{number =1, name = main}2017-01-0311:04:01.993MyTestWorkProduct[2086:78292]7----{number =1, name = main}2017-01-0311:04:01.993MyTestWorkProduct[2086:78292]8----{number =1, name = main}2017-01-0311:04:01.994MyTestWorkProduct[2086:78292]9----{number =1, name = main}2017-01-0311:04:01.994MyTestWorkProduct[2086:78292] end
三、NSEnumerator
NSEnumerator *enumer=[array objectEnumerator];
id obj;
while(obj=[enumer nextObject]) {
NSLog(@"%@----%@",obj,[NSThread currentThread]);
}
打印结果如下:2017-01-0311:05:37.138MyTestWorkProduct[2156:79788]1----{number =1, name = main}2017-01-0311:05:37.139MyTestWorkProduct[2156:79788]2----{number =1, name = main}2017-01-0311:05:37.140MyTestWorkProduct[2156:79788]3----{number =1, name = main}2017-01-0311:05:37.140MyTestWorkProduct[2156:79788]4----{number =1, name = main}2017-01-0311:05:37.140MyTestWorkProduct[2156:79788]5----{number =1, name = main}2017-01-0311:05:37.141MyTestWorkProduct[2156:79788]6----{number =1, name = main}2017-01-0311:05:37.141MyTestWorkProduct[2156:79788]7----{number =1, name = main}2017-01-0311:05:37.141MyTestWorkProduct[2156:79788]8----{number =1, name = main}2017-01-0311:05:37.142MyTestWorkProduct[2156:79788]9----{number =1, name = main}2017-01-0311:05:37.142MyTestWorkProduct[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-0311:07:30.774MyTestWorkProduct[2229:81188]1----{number =1, name = main}2017-01-0311:07:30.775MyTestWorkProduct[2229:81188]2----{number =1, name = main}2017-01-0311:07:30.775MyTestWorkProduct[2229:81188]3----{number =1, name = main}2017-01-0311:07:30.776MyTestWorkProduct[2229:81188]4----{number =1, name = main}2017-01-0311:07:30.777MyTestWorkProduct[2229:81188]5----{number =1, name = main}2017-01-0311:07:30.778MyTestWorkProduct[2229:81188]6----{number =1, name = main}2017-01-0311:07:30.778MyTestWorkProduct[2229:81188]7----{number =1, name = main}2017-01-0311:07:30.779MyTestWorkProduct[2229:81188]8----{number =1, name = main}2017-01-0311:07:30.779MyTestWorkProduct[2229:81188]9----{number =1, name = main}2017-01-0311:07:30.780MyTestWorkProduct[2229:81188]9----{number =1, name = main}2017-01-0311:07:30.781MyTestWorkProduct[2229:81188]8----{number =1, name = main}2017-01-0311:07:30.781MyTestWorkProduct[2229:81188]7----{number =1, name = main}2017-01-0311:07:30.782MyTestWorkProduct[2229:81188]6----{number =1, name = main}2017-01-0311:07:30.782MyTestWorkProduct[2229:81188]5----{number =1, name = main}2017-01-0311:07:30.783MyTestWorkProduct[2229:81188]4----{number =1, name = main}2017-01-0311:07:30.784MyTestWorkProduct[2229:81188]3----{number =1, name = main}2017-01-0311:07:30.785MyTestWorkProduct[2229:81188]2----{number =1, name = main}2017-01-0311:07:30.786MyTestWorkProduct[2229:81188]1----{number =1, name = main}2017-01-0311:07:30.786MyTestWorkProduct[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-0311:09:00.114MyTestWorkProduct[2302:82847]1----{number =1, name = main}2017-01-0311:09:00.114MyTestWorkProduct[2302:82977]3----{number =7, name = (null)}2017-01-0311:09:00.114MyTestWorkProduct[2302:83008]2----{number =6, name = (null)}2017-01-0311:09:00.114MyTestWorkProduct[2302:83482]4----{number =8, name = (null)}2017-01-0311:09:00.114MyTestWorkProduct[2302:82847]5----{number =1, name = main}2017-01-0311:09:00.115MyTestWorkProduct[2302:82977]6----{number =7, name = (null)}2017-01-0311:09:00.115MyTestWorkProduct[2302:83008]7----{number =6, name = (null)}2017-01-0311:09:00.116MyTestWorkProduct[2302:83482]8----{number =8, name = (null)}2017-01-0311:09:00.116MyTestWorkProduct[2302:82847]9----{number =1, name = main}2017-01-0311:09:00.116MyTestWorkProduct[2302:82847] end
根据打印结果最后才打印 end 字符可知,dispatch_apply会等待每个block中的任务都执行完成后,才往下执行。
总结:从这五种遍历方式来看,第5种采用的多线程,对于处理耗时的数组遍历比较适用。