OC中的各种遍历
iOS开发中我们经常遇到各种的数据处理,所以就会经常遇到数组遍历 和 字典遍历。但是不同的遍历方式,效率不同,并且还有不同的限制。
首先把数据源准备好:
_dataSource = [NSMutableArray array];
_dataDic = [NSMutableDictionary dictionary];
for (int i=0; i<5; i++) {
[_dataSource addObject:@(i)];
[_dataDic setObject:@(i) forKey:[NSString stringWithFormat:@"我是第%d个Item",i]];
}
for循环遍历
遍历数组
for (int i = 0; i<_dataSource.count; i++) {
NSLog(@"%@ --- thread:%@",[_dataSource objectAtIndex:i],[NSThread currentThread]);
}
遍历字典
// 取出字典中所有的key (无序的)
NSArray *keys = [_dataDic allKeys];
for (int i = 0; i<keys.count; i++) {
// 根据key值索引Value即可
NSString *key = [keys objectAtIndex:i];
NSString *value = [_dataDic objectForKey:key];
NSLog(@"%@ --- thread:%@",value,[NSThread currentThread]);
}
这是打印结果
2018-05-09 15:01:23.119707+0800 OCProject[78848:4457173] 0 --- thread:<NSThread: 0x604000263b40>{number = 1, name = main}
2018-05-09 15:01:23.120003+0800 OCProject[78848:4457173] 1 --- thread:<NSThread: 0x604000263b40>{number = 1, name = main}
2018-05-09 15:01:23.120262+0800 OCProject[78848:4457173] 2 --- thread:<NSThread: 0x604000263b40>{number = 1, name = main}
2018-05-09 15:01:23.120473+0800 OCProject[78848:4457173] 3 --- thread:<NSThread: 0x604000263b40>{number = 1, name = main}
2018-05-09 15:01:23.120754+0800 OCProject[78848:4457173] 4 --- thread:<NSThread: 0x604000263b40>{number = 1, name = main}
2018-05-09 15:01:23.120951+0800 OCProject[78848:4457173] 我是第3个Item --- thread:<NSThread: 0x604000263b40>{number = 1, name = main}
2018-05-09 15:01:23.121111+0800 OCProject[78848:4457173] 我是第1个Item --- thread:<NSThread: 0x604000263b40>{number = 1, name = main}
2018-05-09 15:01:23.121231+0800 OCProject[78848:4457173] 我是第4个Item --- thread:<NSThread: 0x604000263b40>{number = 1, name = main}
2018-05-09 15:01:23.121377+0800 OCProject[78848:4457173] 我是第2个Item --- thread:<NSThread: 0x604000263b40>{number = 1, name = main}
2018-05-09 15:01:23.121539+0800 OCProject[78848:4457173] 我是第0个Item --- thread:<NSThread: 0x604000263b40>{number = 1, name = main}
【分析】
- 优点:简单
- 缺点:遍历字典有太多中间变量,会有一部分开销
for-in遍历
遍历数组
// 遍历数组
for (NSNumber *obj in _dataSource) {
NSLog(@"%@ --- thread:%@",obj,[NSThread currentThread]);
}
遍历字典
// 遍历字典
NSArray *keys = [_dataDic allKeys];
for (NSString *key in keys) {
NSString *value = [_dataDic objectForKey:key];
NSLog(@"%@ --- thread:%@",value,[NSThread currentThread]);
}
打印结果:
2018-05-09 15:17:28.993429+0800 OCProject[79116:4479093] 0 --- thread:<NSThread: 0x60400006c0c0>{number = 1, name = main}
2018-05-09 15:17:28.993769+0800 OCProject[79116:4479093] 1 --- thread:<NSThread: 0x60400006c0c0>{number = 1, name = main}
2018-05-09 15:17:28.993980+0800 OCProject[79116:4479093] 2 --- thread:<NSThread: 0x60400006c0c0>{number = 1, name = main}
2018-05-09 15:17:28.994204+0800 OCProject[79116:4479093] 3 --- thread:<NSThread: 0x60400006c0c0>{number = 1, name = main}
2018-05-09 15:17:28.994375+0800 OCProject[79116:4479093] 4 --- thread:<NSThread: 0x60400006c0c0>{number = 1, name = main}
2018-05-09 15:17:28.994505+0800 OCProject[79116:4479093] 我是第3个Item --- thread:<NSThread: 0x60400006c0c0>{number = 1, name = main}
2018-05-09 15:17:28.994693+0800 OCProject[79116:4479093] 我是第1个Item --- thread:<NSThread: 0x60400006c0c0>{number = 1, name = main}
2018-05-09 15:17:28.994861+0800 OCProject[79116:4479093] 我是第4个Item --- thread:<NSThread: 0x60400006c0c0>{number = 1, name = main}
2018-05-09 15:17:28.995037+0800 OCProject[79116:4479093] 我是第2个Item --- thread:<NSThread: 0x60400006c0c0>{number = 1, name = main}
2018-05-09 15:17:28.995253+0800 OCProject[79116:4479093] 我是第0个Item --- thread:<NSThread: 0x60400006c0c0>{number = 1, name = main}
这里需要注意的是的 for-in 遍历时如果对数组进行增删操作会导致程序crash。
【分析】
- 优点:
- 语法简洁
- 效率高
- 缺点:无法获得当前遍历操作所针对的下标。
枚举器
遍历数组
/*
* 正向枚举:objectEnumerator
* 反向枚举:reverseObjectEnumerator
*/
NSEnumerator *enumetorArr = [_dataSource objectEnumerator];
id obj ;
while (obj=[enumetorArr nextObject]) {
NSLog(@"%@ --- thread:%@",obj,[NSThread currentThread]);
}
遍历字典
NSEnumerator *enumtorValue = [_dataDic objectEnumerator];// Value枚举器 reverseObjectEnumerator反向顺序
NSEnumerator *enumtorKey = [_dataDic keyEnumerator];// key 枚举器
id value;
while (value=[enumtorDic nextObject]) {
NSLog(@"%@ --- thread:%@",value,[NSThread currentThread]);
}
打印结果:
2018-05-09 15:35:49.961318+0800 OCProject[79558:4515099] 0 --- thread:<NSThread: 0x604000065300>{number = 1, name = main}
2018-05-09 15:35:49.961554+0800 OCProject[79558:4515099] 1 --- thread:<NSThread: 0x604000065300>{number = 1, name = main}
2018-05-09 15:35:49.961666+0800 OCProject[79558:4515099] 2 --- thread:<NSThread: 0x604000065300>{number = 1, name = main}
2018-05-09 15:35:49.961788+0800 OCProject[79558:4515099] 3 --- thread:<NSThread: 0x604000065300>{number = 1, name = main}
2018-05-09 15:35:49.961909+0800 OCProject[79558:4515099] 4 --- thread:<NSThread: 0x604000065300>{number = 1, name = main}
2018-05-09 15:35:49.962030+0800 OCProject[79558:4515099] 我是第3个Item --- thread:<NSThread: 0x604000065300>{number = 1, name = main}
2018-05-09 15:35:49.962130+0800 OCProject[79558:4515099] 我是第1个Item --- thread:<NSThread: 0x604000065300>{number = 1, name = main}
2018-05-09 15:35:49.962257+0800 OCProject[79558:4515099] 我是第4个Item --- thread:<NSThread: 0x604000065300>{number = 1, name = main}
2018-05-09 15:35:49.962369+0800 OCProject[79558:4515099] 我是第2个Item --- thread:<NSThread: 0x604000065300>{number = 1, name = main}
2018-05-09 15:35:49.962485+0800 OCProject[79558:4515099] 我是第0个Item --- thread:<NSThread: 0x604000065300>{number = 1, name = main}
【分析】
优点:对于不同的数据类型,遍历的语法相似;内部可以简单的通过reverseObjectEnumerator设置进行反向遍历。
缺点:代码量稍大。
快速枚举遍历
遍历数组
// 按照顺序来的
[_dataSource enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"sort:0 %@ -- %lu --- %@ --- %@",obj,(unsigned long)idx,stop?@"YES":@"NO",[NSThread currentThread]);
}];
// 随机的顺序遍历,开启了多个子线程 NSEnumerationReverse反向遍历,并发遍历 NSEnumerationConcurrent 开启了多个子线程
[_dataSource enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"sort:1 %@ -- %lu --- %@ --- %@",obj,(unsigned long)idx,stop?@"YES":@"NO",[NSThread currentThread]);
}];
// 指定循环范围 ,循环次数多可以开辟子线程
[_dataSource enumerateObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, _dataSource.count)] options:NSEnumerationConcurrent usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"sort:2 %@ -- %lu --- %@ --- %@",obj,(unsigned long)idx,stop?@"YES":@"NO",[NSThread currentThread]);
}];
打印结果:
2018-05-09 18:31:25.808197+0800 OCProject[923:284717] sort:0 0 -- 0 --- YES --- <NSThread: 0x137e0bb50>{number = 1, name = main}
2018-05-09 18:31:25.808692+0800 OCProject[923:284717] sort:0 1 -- 1 --- YES --- <NSThread: 0x137e0bb50>{number = 1, name = main}
2018-05-09 18:31:25.808975+0800 OCProject[923:284717] sort:0 2 -- 2 --- YES --- <NSThread: 0x137e0bb50>{number = 1, name = main}
2018-05-09 18:31:25.809250+0800 OCProject[923:284717] sort:0 3 -- 3 --- YES --- <NSThread: 0x137e0bb50>{number = 1, name = main}
2018-05-09 18:31:25.809499+0800 OCProject[923:284717] sort:0 4 -- 4 --- YES --- <NSThread: 0x137e0bb50>{number = 1, name = main}
2018-05-09 18:31:25.809890+0800 OCProject[923:284717] sort:1 0 -- 0 --- YES --- <NSThread: 0x137e0bb50>{number = 1, name = main}
2018-05-09 18:31:25.810140+0800 OCProject[923:284717] sort:1 1 -- 1 --- YES --- <NSThread: 0x137e0bb50>{number = 1, name = main}
2018-05-09 18:31:25.810388+0800 OCProject[923:284717] sort:1 2 -- 2 --- YES --- <NSThread: 0x137e0bb50>{number = 1, name = main}
2018-05-09 18:31:25.810635+0800 OCProject[923:284717] sort:1 3 -- 3 --- YES --- <NSThread: 0x137e0bb50>{number = 1, name = main}
2018-05-09 18:31:25.810883+0800 OCProject[923:284717] sort:1 4 -- 4 --- YES --- <NSThread: 0x137e0bb50>{number = 1, name = main}
2018-05-09 18:31:25.813959+0800 OCProject[923:284717] sort:2 0 -- 0 --- YES --- <NSThread: 0x137e0bb50>{number = 1, name = main}
2018-05-09 18:31:25.814289+0800 OCProject[923:284717] sort:2 1 -- 1 --- YES --- <NSThread: 0x137e0bb50>{number = 1, name = main}
2018-05-09 18:31:25.814552+0800 OCProject[923:284717] sort:2 2 -- 2 --- YES --- <NSThread: 0x137e0bb50>{number = 1, name = main}
2018-05-09 18:31:25.814806+0800 OCProject[923:284717] sort:2 3 -- 3 --- YES --- <NSThread: 0x137e0bb50>{number = 1, name = main}
2018-05-09 18:31:25.815054+0800 OCProject[923:284717] sort:2 4 -- 4 --- YES --- <NSThread: 0x137e0bb50>{number = 1, name = main}
当我把数组内的元素个数增加到 10,请看前缀是sort:1
的打印结果如下:
2018-05-09 18:34:41.806499+0800 OCProject[928:286028] sort:1 0 -- 0 --- YES --- <NSThread: 0x145e0bb50>{number = 1, name = main}
2018-05-09 18:34:41.806982+0800 OCProject[928:286068] sort:1 1 -- 1 --- YES --- <NSThread: 0x145eb08d0>{number = 3, name = (null)}
2018-05-09 18:34:41.809488+0800 OCProject[928:286068] sort:1 3 -- 3 --- YES --- <NSThread: 0x145eb08d0>{number = 3, name = (null)}
2018-05-09 18:34:41.809518+0800 OCProject[928:286028] sort:1 2 -- 2 --- YES --- <NSThread: 0x145e0bb50>{number = 1, name = main}
2018-05-09 18:34:41.809788+0800 OCProject[928:286068] sort:1 4 -- 4 --- YES --- <NSThread: 0x145eb08d0>{number = 3, name = (null)}
2018-05-09 18:34:41.809840+0800 OCProject[928:286028] sort:1 5 -- 5 --- YES --- <NSThread: 0x145e0bb50>{number = 1, name = main}
2018-05-09 18:34:41.810045+0800 OCProject[928:286068] sort:1 6 -- 6 --- YES --- <NSThread: 0x145eb08d0>{number = 3, name = (null)}
2018-05-09 18:34:41.810462+0800 OCProject[928:286028] sort:1 7 -- 7 --- YES --- <NSThread: 0x145e0bb50>{number = 1, name = main}
2018-05-09 18:34:41.810989+0800 OCProject[928:286068] sort:1 8 -- 8 --- YES --- <NSThread: 0x145eb08d0>{number = 3, name = (null)}
2018-05-09 18:34:41.811034+0800 OCProject[928:286028] sort:1 9 -- 9 --- YES --- <NSThread: 0x145e0bb50>{number = 1, name = main}
遍历字典
// 主线程 无序
[_dataDic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSLog(@"key:%@ -- value:%@ --- %@",key,obj,[NSThread currentThread]);
}];
// 无序; NSEnumerationReverse反向遍历,并发遍历 NSEnumerationConcurrent
[_dataDic enumerateKeysAndObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSLog(@"key:%@ -- value:%@ --- %@",key,obj,[NSThread currentThread]);
}];
打印结果:
2018-05-09 18:33:48.223268+0800 OCProject[926:285509] key:3 -- value:我是第3个Item --- <NSThread: 0x107e0b250>{number = 1, name = main}
2018-05-09 18:33:48.224066+0800 OCProject[926:285509] key:1 -- value:我是第1个Item --- <NSThread: 0x107e0b250>{number = 1, name = main}
2018-05-09 18:33:48.225122+0800 OCProject[926:285509] key:4 -- value:我是第4个Item --- <NSThread: 0x107e0b250>{number = 1, name = main}
2018-05-09 18:33:48.226368+0800 OCProject[926:285509] key:2 -- value:我是第2个Item --- <NSThread: 0x107e0b250>{number = 1, name = main}
2018-05-09 18:33:48.226915+0800 OCProject[926:285509] key:0 -- value:我是第0个Item --- <NSThread: 0x107e0b250>{number = 1, name = main}
2018-05-09 18:33:48.227419+0800 OCProject[926:285509] key:3 -- value:我是第3个Item --- <NSThread: 0x107e0b250>{number = 1, name = main}
2018-05-09 18:33:48.228593+0800 OCProject[926:285509] key:1 -- value:我是第1个Item --- <NSThread: 0x107e0b250>{number = 1, name = main}
2018-05-09 18:33:48.229978+0800 OCProject[926:285509] key:4 -- value:我是第4个Item --- <NSThread: 0x107e0b250>{number = 1, name = main}
2018-05-09 18:33:48.231440+0800 OCProject[926:285509] key:2 -- value:我是第2个Item --- <NSThread: 0x107e0b250>{number = 1, name = main}
2018-05-09 18:33:48.231977+0800 OCProject[926:285509] key:0 -- value:我是第0个Item --- <NSThread: 0x107e0b250>{number = 1, name = main}
【分析】
- 优点:
- 遍历时可以直接从block中获得需要的所有信息,包括下标、值等。特别相对于字典而言,不需要做多余的编码即可同时获得key和value的值。
- 能够直接修改block中key或者obj的类型为真实类型,可以省去类型转换的工作。
- 可以通过NSEnumerationConcurrent枚举值开启并发迭代功能。
- 缺点:暂无
说明:基于Block的遍历方式在实现反向遍历的时候也非常简单,使用enumerateObjectsWithOptions方法,传递NSEnumerationReverse作为参数即可,在处理遍历操作的时候推荐基于Block的遍历方式。
迭代
遍历数组
dispatch_queue_t queue =dispatch_queue_create("apply并行队列", DISPATCH_QUEUE_CONCURRENT);
dispatch_apply(_dataSource.count, queue, ^(size_t index) {
NSLog(@"%@----%@",self->_dataSource[index],[NSThread currentThread]);
});
NSLog(@"end1");
遍历字典
NSArray *allkeys = [_dataDic allKeys];
NSArray *allValue = [_dataDic allValues];
dispatch_apply(_dataDic.count, queue, ^(size_t index) {
NSString *key = [allkeys objectAtIndex:index];
NSLog(@"dic: %@----%@",[self->_dataDic objectForKey:key],[NSThread currentThread]);
});
打印结果:
2018-05-09 19:24:03.875390+0800 OCProject[975:301673] 0----<NSThread: 0x121e0ba50>{number = 1, name = main}
2018-05-09 19:24:03.875913+0800 OCProject[975:301673] 2----<NSThread: 0x121e0ba50>{number = 1, name = main}
2018-05-09 19:24:03.876194+0800 OCProject[975:301673] 3----<NSThread: 0x121e0ba50>{number = 1, name = main}
2018-05-09 19:24:03.876445+0800 OCProject[975:301673] 4----<NSThread: 0x121e0ba50>{number = 1, name = main}
2018-05-09 19:24:03.876688+0800 OCProject[975:301673] 5----<NSThread: 0x121e0ba50>{number = 1, name = main}
2018-05-09 19:24:03.876926+0800 OCProject[975:301673] 6----<NSThread: 0x121e0ba50>{number = 1, name = main}
2018-05-09 19:24:03.877162+0800 OCProject[975:301673] 7----<NSThread: 0x121e0ba50>{number = 1, name = main}
2018-05-09 19:24:03.877398+0800 OCProject[975:301673] 8----<NSThread: 0x121e0ba50>{number = 1, name = main}
2018-05-09 19:24:03.877669+0800 OCProject[975:301673] 9----<NSThread: 0x121e0ba50>{number = 1, name = main}
2018-05-09 19:24:03.878021+0800 OCProject[975:301852] 1----<NSThread: 0x1231b4750>{number = 3, name = (null)}
2018-05-09 19:24:03.878310+0800 OCProject[975:301673] end1
2018-05-09 19:24:03.878752+0800 OCProject[975:301673] dic: 我是第7个Item----<NSThread: 0x121e0ba50>{number = 1, name = main}
2018-05-09 19:24:03.879072+0800 OCProject[975:301673] dic: 我是第3个Item----<NSThread: 0x121e0ba50>{number = 1, name = main}
2018-05-09 19:24:03.879372+0800 OCProject[975:301673] dic: 我是第8个Item----<NSThread: 0x121e0ba50>{number = 1, name = main}
2018-05-09 19:24:03.879672+0800 OCProject[975:301673] dic: 我是第4个Item----<NSThread: 0x121e0ba50>{number = 1, name = main}
2018-05-09 19:24:03.879971+0800 OCProject[975:301673] dic: 我是第0个Item----<NSThread: 0x121e0ba50>{number = 1, name = main}
2018-05-09 19:24:03.880271+0800 OCProject[975:301673] dic: 我是第9个Item----<NSThread: 0x121e0ba50>{number = 1, name = main}
2018-05-09 19:24:03.880568+0800 OCProject[975:301673] dic: 我是第5个Item----<NSThread: 0x121e0ba50>{number = 1, name = main}
2018-05-09 19:24:03.883701+0800 OCProject[975:301673] dic: 我是第6个Item----<NSThread: 0x121e0ba50>{number = 1, name = main}
2018-05-09 19:24:03.883947+0800 OCProject[975:301673] dic: 我是第2个Item----<NSThread: 0x121e0ba50>{number = 1, name = main}
2018-05-09 19:24:03.880890+0800 OCProject[975:301852] dic: 我是第1个Item----<NSThread: 0x1231b4750>{number = 3, name = (null)}
【分析】
- 优点:开启多条线程并发处理遍历任务,执行效率高。
- 缺点:
- 对于字典和集合的处理需借助数组
- 无法实现反向遍历。