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

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

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

    NSArray *array=@[@"",@"",@"",@"",@"",@"",@"",@"",@"",];
NSInteger count =array.count;

一、for循环

for (NSInteger i=; i<count; i++) {
NSLog(@"%@----%@",array[i],[NSThread currentThread]);
} 打印结果如下:
-- ::01.537 MyTestWorkProduct[:] ----<NSThread: 0x600000260e80>{number = , name = main}
-- ::01.538 MyTestWorkProduct[:] ----<NSThread: 0x600000260e80>{number = , name = main}
-- ::01.539 MyTestWorkProduct[:] ----<NSThread: 0x600000260e80>{number = , name = main}
-- ::01.539 MyTestWorkProduct[:] ----<NSThread: 0x600000260e80>{number = , name = main}
-- ::01.539 MyTestWorkProduct[:] ----<NSThread: 0x600000260e80>{number = , name = main}
-- ::01.540 MyTestWorkProduct[:] ----<NSThread: 0x600000260e80>{number = , name = main}
-- ::01.540 MyTestWorkProduct[:] ----<NSThread: 0x600000260e80>{number = , name = main}
-- ::01.540 MyTestWorkProduct[:] ----<NSThread: 0x600000260e80>{number = , name = main}
-- ::01.541 MyTestWorkProduct[:] ----<NSThread: 0x600000260e80>{number = , name = main}
-- ::01.541 MyTestWorkProduct[:] end

二、forin 快速枚举

 for (NSString *string in array) {
NSLog(@"%@----%@",string,[NSThread currentThread]);
} 打印结果如下:
-- ::01.990 MyTestWorkProduct[:] ----<NSThread: 0x60000007a400>{number = , name = main}
-- ::01.990 MyTestWorkProduct[:] ----<NSThread: 0x60000007a400>{number = , name = main}
-- ::01.991 MyTestWorkProduct[:] ----<NSThread: 0x60000007a400>{number = , name = main}
-- ::01.992 MyTestWorkProduct[:] ----<NSThread: 0x60000007a400>{number = , name = main}
-- ::01.992 MyTestWorkProduct[:] ----<NSThread: 0x60000007a400>{number = , name = main}
-- ::01.993 MyTestWorkProduct[:] ----<NSThread: 0x60000007a400>{number = , name = main}
-- ::01.993 MyTestWorkProduct[:] ----<NSThread: 0x60000007a400>{number = , name = main}
-- ::01.993 MyTestWorkProduct[:] ----<NSThread: 0x60000007a400>{number = , name = main}
-- ::01.994 MyTestWorkProduct[:] ----<NSThread: 0x60000007a400>{number = , name = main}
-- ::01.994 MyTestWorkProduct[:] end

三、NSEnumerator

 NSEnumerator *enumer=[array objectEnumerator];
id obj;
while (obj=[enumer nextObject]) {
NSLog(@"%@----%@",obj,[NSThread currentThread]);
}
打印结果如下:
-- ::37.138 MyTestWorkProduct[:] ----<NSThread: 0x608000078980>{number = , name = main}
-- ::37.139 MyTestWorkProduct[:] ----<NSThread: 0x608000078980>{number = , name = main}
-- ::37.140 MyTestWorkProduct[:] ----<NSThread: 0x608000078980>{number = , name = main}
-- ::37.140 MyTestWorkProduct[:] ----<NSThread: 0x608000078980>{number = , name = main}
-- ::37.140 MyTestWorkProduct[:] ----<NSThread: 0x608000078980>{number = , name = main}
-- ::37.141 MyTestWorkProduct[:] ----<NSThread: 0x608000078980>{number = , name = main}
-- ::37.141 MyTestWorkProduct[:] ----<NSThread: 0x608000078980>{number = , name = main}
-- ::37.141 MyTestWorkProduct[:] ----<NSThread: 0x608000078980>{number = , name = main}
-- ::37.142 MyTestWorkProduct[:] ----<NSThread: 0x608000078980>{number = , name = main}
-- ::37.142 MyTestWorkProduct[:] 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]);
}];
打印结果如下:
-- ::30.774 MyTestWorkProduct[:] ----<NSThread: 0x600000070240>{number = , name = main}
-- ::30.775 MyTestWorkProduct[:] ----<NSThread: 0x600000070240>{number = , name = main}
-- ::30.775 MyTestWorkProduct[:] ----<NSThread: 0x600000070240>{number = , name = main}
-- ::30.776 MyTestWorkProduct[:] ----<NSThread: 0x600000070240>{number = , name = main}
-- ::30.777 MyTestWorkProduct[:] ----<NSThread: 0x600000070240>{number = , name = main}
-- ::30.778 MyTestWorkProduct[:] ----<NSThread: 0x600000070240>{number = , name = main}
-- ::30.778 MyTestWorkProduct[:] ----<NSThread: 0x600000070240>{number = , name = main}
-- ::30.779 MyTestWorkProduct[:] ----<NSThread: 0x600000070240>{number = , name = main}
-- ::30.779 MyTestWorkProduct[:] ----<NSThread: 0x600000070240>{number = , name = main}
-- ::30.780 MyTestWorkProduct[:] ----<NSThread: 0x600000070240>{number = , name = main}
-- ::30.781 MyTestWorkProduct[:] ----<NSThread: 0x600000070240>{number = , name = main}
-- ::30.781 MyTestWorkProduct[:] ----<NSThread: 0x600000070240>{number = , name = main}
-- ::30.782 MyTestWorkProduct[:] ----<NSThread: 0x600000070240>{number = , name = main}
-- ::30.782 MyTestWorkProduct[:] ----<NSThread: 0x600000070240>{number = , name = main}
-- ::30.783 MyTestWorkProduct[:] ----<NSThread: 0x600000070240>{number = , name = main}
-- ::30.784 MyTestWorkProduct[:] ----<NSThread: 0x600000070240>{number = , name = main}
-- ::30.785 MyTestWorkProduct[:] ----<NSThread: 0x600000070240>{number = , name = main}
-- ::30.786 MyTestWorkProduct[:] ----<NSThread: 0x600000070240>{number = , name = main}
-- ::30.786 MyTestWorkProduct[:] 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");


打印结果如下:
-- ::00.114 MyTestWorkProduct[:] ----<NSThread: 0x60000006b940>{number = , name = main}
-- ::00.114 MyTestWorkProduct[:] ----<NSThread: 0x600000876cc0>{number = , name = (null)}
-- ::00.114 MyTestWorkProduct[:] ----<NSThread: 0x600000479a80>{number = , name = (null)}
-- ::00.114 MyTestWorkProduct[:] ----<NSThread: 0x60800067e740>{number = , name = (null)}
-- ::00.114 MyTestWorkProduct[:] ----<NSThread: 0x60000006b940>{number = , name = main}
-- ::00.115 MyTestWorkProduct[:] ----<NSThread: 0x600000876cc0>{number = , name = (null)}
-- ::00.115 MyTestWorkProduct[:] ----<NSThread: 0x600000479a80>{number = , name = (null)}
-- ::00.116 MyTestWorkProduct[:] ----<NSThread: 0x60800067e740>{number = , name = (null)}
-- ::00.116 MyTestWorkProduct[:] ----<NSThread: 0x60000006b940>{number = , name = main}
-- ::00.116 MyTestWorkProduct[:] end

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

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

上一篇:nginx 负载均衡5种配置方式


下一篇:FTP文件夹打开错误,Windows无法访问此文件夹