iOS中数组遍历的方法及比较
2016-01-12 10:41
676 查看
目前所知有七种方法
//第一种
[arr enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop){
NSLog(@"%ld,%@",idx,[arr objectAtIndex:idx]);
}];
//第二种
dispatch_apply([arr count], dispatch_get_global_queue(0, 0), ^(size_t index){//并行
NSLog(@"%ld,%@",index,[arr objectAtIndex:index]);
});
//第三种
dispatch_apply([arr count], dispatch_get_main_queue(), ^(size_t index){//串行,容易引起主线程堵塞,可以另外开辟线程
NSLog(@"%ld,%@",index,[arr objectAtIndex:index]);
});
//第四种
for (NSString*str in arr) {
NSLog(@"%@",str);
}
//第五种,do-while
int i = 0;
do {
NSLog(@"%@",[arr objectAtIndex:i]);
i++;
} while (i<[arr count]);
//第六种,while-do
int j = 0;
while (j<[arr count]) {
NSLog(@"%@",[arr objectAtIndex:j]);
j++;
}
//第七种,普通for循环
for (int m = 0; m<[arr count]; m++) {
NSLog(@"%@",[arr objectAtIndex:m]);
}
注意:
① 其中第二种方法由于是并行,所以打印出来的东西是随机的,并不是按照顺序打印的
② 第三种容易引起主线程堵塞,所以最好自己另外创建一个线程
http://blog.csdn.net/sakulafly/article/details/31765351
数组遍历是编码中很常见的一种需求,我们来扒一拔iOS里面都有什么样的方法来实现,有什么特点。
因为iOS是兼容C语言的,所以C语言里面的最最常见的for循环遍历是没有问题的。
本文中用的数组是获取的系统的语言数组,大约有30多个数据,虽然还不够模拟大批量的数据,但对于方法的验证是没有问题的了。
[objc] view
plaincopy
NSArray *langArray = [[NSUserDefaults standardUserDefaults] arrayForKey:@"AppleLanguages"];
第一种方法是最最熟悉的C语言演化过来的:
[objc] view
plaincopy
for (int i = 0; i<langArray.count; i++) {
NSLog(@"langArray[%d]=%@", i, langArray[i]);
}
这个方法最普通,效率也一般,但它也有好处,一是方便针对下标的处理,就是说如果我要处理i==10的情况是很简便的,另一个是可以比较方便的反向遍历。
Objective-C 1.0里面的NSEnumerator也可以进行遍历,代码如下:
[objc] view
plaincopy
NSEnumerator *enumerator = [langArray objectEnumerator];
id object;
while ((object = [enumerator nextObject]) != nil) {
NSLog(@"langArray=%@", object);
}
这里我们可以看到没有下标了,通过nextObject的方法来遍历。这个方法的好处是对于遍历NSDictionary和NSSet代码也比较类似,不便的是对于下标的处理会不方便,另外反向遍历需要用reverseObjectEnumerator方法。
Objective-C发展到2.0时又有了快速遍历功能,代码如下:
[objc] view
plaincopy
for (id object in langArray) {
NSLog(@"langArray=%@", object);
}
这里代码简洁清晰,很长时间是我写代码的首选,号称效率也最高,不过不便之处同样明显,如果算法要求知道数组的下标,这个方法就抓瞎了。另外,反向需要通过[langArray reverseObjectEnumerator]来实现。
等到block出来后,iOS里面新增加了enumerateObjectsUsingBlock:的方法,代码如下:
[objc] view
plaincopy
[langArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOLBOOL *stop) {
NSLog(@"idx=%d, id=%@", idx, obj);
}];
这里我们看到block里面的参数包括object,下标以及是否停止遍历,应该说,这个能满足基本所有的遍历需求了,有下标,有运行的对象,还有是否继续遍历的标志。不过反向遍历呢?苹果提供了另外一个方法:
[objc] view
plaincopy
[langArray enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOLBOOL *stop) {
NSLog(@"idx=%d, id=%@", idx, obj);
}];
这个enumerateObjectsWithOptions:usingBlock:方法比enumerateObjectsUsingBlock:方法多传了一个参数,这个参数指定了遍历的顺序。
说到这里,如果我们选择正向遍历,那么这两种方法是一样的么?答案也是否定的。在enumerateObjectsWithOptions:usingBlock:方法里面,如果指定了NSEnumerationConcurrent顺序,那么底层通过GCD来处理并发执行事宜,具体实现可能会用到dispatch group。也就是说,这个会用多线程来并发实现,并不保证按照顺序执行,但效率肯定是杠杠的!
我们来看一下打印结果:
[objc] view
plaincopy
2014-06-17 15:46:44.413 testStoryboard[2703:3503] idx=32, id=hu
2014-06-17 15:46:44.413 testStoryboard[2703:1303] idx=16, id=ru
2014-06-17 15:46:44.416 testStoryboard[2703:3503] idx=33, id=vi
2014-06-17 15:46:44.412 testStoryboard[2703:60b] idx=0, id=zh-Hant
2014-06-17 15:46:44.417 testStoryboard[2703:1303] idx=17, id=pl
2014-06-17 15:46:44.417 testStoryboard[2703:60b] idx=1, id=zh-Hans
2014-06-17 15:46:44.417 testStoryboard[2703:1303] idx=18, id=tr
2014-06-17 15:46:44.419 testStoryboard[2703:60b] idx=2, id=en
2014-06-17 15:46:44.419 testStoryboard[2703:1303] idx=19, id=uk
2014-06-17 15:46:44.421 testStoryboard[2703:60b] idx=3, id=fr
2014-06-17 15:46:44.421 testStoryboard[2703:1303] idx=20, id=ar
2014-06-17 15:46:44.421 testStoryboard[2703:60b] idx=4, id=de
2014-06-17 15:46:44.422 testStoryboard[2703:60b] idx=5, id=ja
2014-06-17 15:46:44.422 testStoryboard[2703:60b] idx=6, id=nl
2014-06-17 15:46:44.421 testStoryboard[2703:1303] idx=21, id=hr
2014-06-17 15:46:44.423 testStoryboard[2703:60b] idx=7, id=it
2014-06-17 15:46:44.423 testStoryboard[2703:1303] idx=22, id=cs
2014-06-17 15:46:44.423 testStoryboard[2703:60b] idx=8, id=es
2014-06-17 15:46:44.424 testStoryboard[2703:1303] idx=23, id=el
2014-06-17 15:46:44.424 testStoryboard[2703:60b] idx=9, id=ko
2014-06-17 15:46:44.424 testStoryboard[2703:1303] idx=24, id=he
2014-06-17 15:46:44.425 testStoryboard[2703:60b] idx=10, id=pt
2014-06-17 15:46:44.425 testStoryboard[2703:60b] idx=11, id=pt-PT
2014-06-17 15:46:44.425 testStoryboard[2703:1303] idx=25, id=ro
2014-06-17 15:46:44.426 testStoryboard[2703:60b] idx=12, id=da
2014-06-17 15:46:44.426 testStoryboard[2703:1303] idx=26, id=sk
2014-06-17 15:46:44.426 testStoryboard[2703:60b] idx=13, id=fi
2014-06-17 15:46:44.426 testStoryboard[2703:1303] idx=27, id=th
2014-06-17 15:46:44.427 testStoryboard[2703:60b] idx=14, id=nb
2014-06-17 15:46:44.427 testStoryboard[2703:1303] idx=28, id=id
2014-06-17 15:46:44.428 testStoryboard[2703:60b] idx=15, id=sv
2014-06-17 15:46:44.428 testStoryboard[2703:1303] idx=29, id=ms
2014-06-17 15:46:44.429 testStoryboard[2703:1303] idx=30, id=en-GB
2014-06-17 15:46:44.429 testStoryboard[2703:1303] idx=31, id=ca
从这个结果我们可以看出,确实遍历了整个数组,但并发按照顺序从头到尾——也就是说,用到了dispatch group。这在遍历大数组而有相互独立时对于效率的提高是相当有利的,赞一个!
在iOS中,除数组外,还有NSDictionary和NSSet数据也是称为collection数据的,遍历有类似的地方,不过遍历没有数组那么频繁,方法上是差不多的。
//第一种
[arr enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop){
NSLog(@"%ld,%@",idx,[arr objectAtIndex:idx]);
}];
//第二种
dispatch_apply([arr count], dispatch_get_global_queue(0, 0), ^(size_t index){//并行
NSLog(@"%ld,%@",index,[arr objectAtIndex:index]);
});
//第三种
dispatch_apply([arr count], dispatch_get_main_queue(), ^(size_t index){//串行,容易引起主线程堵塞,可以另外开辟线程
NSLog(@"%ld,%@",index,[arr objectAtIndex:index]);
});
//第四种
for (NSString*str in arr) {
NSLog(@"%@",str);
}
//第五种,do-while
int i = 0;
do {
NSLog(@"%@",[arr objectAtIndex:i]);
i++;
} while (i<[arr count]);
//第六种,while-do
int j = 0;
while (j<[arr count]) {
NSLog(@"%@",[arr objectAtIndex:j]);
j++;
}
//第七种,普通for循环
for (int m = 0; m<[arr count]; m++) {
NSLog(@"%@",[arr objectAtIndex:m]);
}
注意:
① 其中第二种方法由于是并行,所以打印出来的东西是随机的,并不是按照顺序打印的
② 第三种容易引起主线程堵塞,所以最好自己另外创建一个线程
http://blog.csdn.net/sakulafly/article/details/31765351
数组遍历是编码中很常见的一种需求,我们来扒一拔iOS里面都有什么样的方法来实现,有什么特点。
因为iOS是兼容C语言的,所以C语言里面的最最常见的for循环遍历是没有问题的。
本文中用的数组是获取的系统的语言数组,大约有30多个数据,虽然还不够模拟大批量的数据,但对于方法的验证是没有问题的了。
[objc] view
plaincopy
NSArray *langArray = [[NSUserDefaults standardUserDefaults] arrayForKey:@"AppleLanguages"];
第一种方法是最最熟悉的C语言演化过来的:
[objc] view
plaincopy
for (int i = 0; i<langArray.count; i++) {
NSLog(@"langArray[%d]=%@", i, langArray[i]);
}
这个方法最普通,效率也一般,但它也有好处,一是方便针对下标的处理,就是说如果我要处理i==10的情况是很简便的,另一个是可以比较方便的反向遍历。
Objective-C 1.0里面的NSEnumerator也可以进行遍历,代码如下:
[objc] view
plaincopy
NSEnumerator *enumerator = [langArray objectEnumerator];
id object;
while ((object = [enumerator nextObject]) != nil) {
NSLog(@"langArray=%@", object);
}
这里我们可以看到没有下标了,通过nextObject的方法来遍历。这个方法的好处是对于遍历NSDictionary和NSSet代码也比较类似,不便的是对于下标的处理会不方便,另外反向遍历需要用reverseObjectEnumerator方法。
Objective-C发展到2.0时又有了快速遍历功能,代码如下:
[objc] view
plaincopy
for (id object in langArray) {
NSLog(@"langArray=%@", object);
}
这里代码简洁清晰,很长时间是我写代码的首选,号称效率也最高,不过不便之处同样明显,如果算法要求知道数组的下标,这个方法就抓瞎了。另外,反向需要通过[langArray reverseObjectEnumerator]来实现。
等到block出来后,iOS里面新增加了enumerateObjectsUsingBlock:的方法,代码如下:
[objc] view
plaincopy
[langArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOLBOOL *stop) {
NSLog(@"idx=%d, id=%@", idx, obj);
}];
这里我们看到block里面的参数包括object,下标以及是否停止遍历,应该说,这个能满足基本所有的遍历需求了,有下标,有运行的对象,还有是否继续遍历的标志。不过反向遍历呢?苹果提供了另外一个方法:
[objc] view
plaincopy
[langArray enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOLBOOL *stop) {
NSLog(@"idx=%d, id=%@", idx, obj);
}];
这个enumerateObjectsWithOptions:usingBlock:方法比enumerateObjectsUsingBlock:方法多传了一个参数,这个参数指定了遍历的顺序。
说到这里,如果我们选择正向遍历,那么这两种方法是一样的么?答案也是否定的。在enumerateObjectsWithOptions:usingBlock:方法里面,如果指定了NSEnumerationConcurrent顺序,那么底层通过GCD来处理并发执行事宜,具体实现可能会用到dispatch group。也就是说,这个会用多线程来并发实现,并不保证按照顺序执行,但效率肯定是杠杠的!
我们来看一下打印结果:
[objc] view
plaincopy
2014-06-17 15:46:44.413 testStoryboard[2703:3503] idx=32, id=hu
2014-06-17 15:46:44.413 testStoryboard[2703:1303] idx=16, id=ru
2014-06-17 15:46:44.416 testStoryboard[2703:3503] idx=33, id=vi
2014-06-17 15:46:44.412 testStoryboard[2703:60b] idx=0, id=zh-Hant
2014-06-17 15:46:44.417 testStoryboard[2703:1303] idx=17, id=pl
2014-06-17 15:46:44.417 testStoryboard[2703:60b] idx=1, id=zh-Hans
2014-06-17 15:46:44.417 testStoryboard[2703:1303] idx=18, id=tr
2014-06-17 15:46:44.419 testStoryboard[2703:60b] idx=2, id=en
2014-06-17 15:46:44.419 testStoryboard[2703:1303] idx=19, id=uk
2014-06-17 15:46:44.421 testStoryboard[2703:60b] idx=3, id=fr
2014-06-17 15:46:44.421 testStoryboard[2703:1303] idx=20, id=ar
2014-06-17 15:46:44.421 testStoryboard[2703:60b] idx=4, id=de
2014-06-17 15:46:44.422 testStoryboard[2703:60b] idx=5, id=ja
2014-06-17 15:46:44.422 testStoryboard[2703:60b] idx=6, id=nl
2014-06-17 15:46:44.421 testStoryboard[2703:1303] idx=21, id=hr
2014-06-17 15:46:44.423 testStoryboard[2703:60b] idx=7, id=it
2014-06-17 15:46:44.423 testStoryboard[2703:1303] idx=22, id=cs
2014-06-17 15:46:44.423 testStoryboard[2703:60b] idx=8, id=es
2014-06-17 15:46:44.424 testStoryboard[2703:1303] idx=23, id=el
2014-06-17 15:46:44.424 testStoryboard[2703:60b] idx=9, id=ko
2014-06-17 15:46:44.424 testStoryboard[2703:1303] idx=24, id=he
2014-06-17 15:46:44.425 testStoryboard[2703:60b] idx=10, id=pt
2014-06-17 15:46:44.425 testStoryboard[2703:60b] idx=11, id=pt-PT
2014-06-17 15:46:44.425 testStoryboard[2703:1303] idx=25, id=ro
2014-06-17 15:46:44.426 testStoryboard[2703:60b] idx=12, id=da
2014-06-17 15:46:44.426 testStoryboard[2703:1303] idx=26, id=sk
2014-06-17 15:46:44.426 testStoryboard[2703:60b] idx=13, id=fi
2014-06-17 15:46:44.426 testStoryboard[2703:1303] idx=27, id=th
2014-06-17 15:46:44.427 testStoryboard[2703:60b] idx=14, id=nb
2014-06-17 15:46:44.427 testStoryboard[2703:1303] idx=28, id=id
2014-06-17 15:46:44.428 testStoryboard[2703:60b] idx=15, id=sv
2014-06-17 15:46:44.428 testStoryboard[2703:1303] idx=29, id=ms
2014-06-17 15:46:44.429 testStoryboard[2703:1303] idx=30, id=en-GB
2014-06-17 15:46:44.429 testStoryboard[2703:1303] idx=31, id=ca
从这个结果我们可以看出,确实遍历了整个数组,但并发按照顺序从头到尾——也就是说,用到了dispatch group。这在遍历大数组而有相互独立时对于效率的提高是相当有利的,赞一个!
在iOS中,除数组外,还有NSDictionary和NSSet数据也是称为collection数据的,遍历有类似的地方,不过遍历没有数组那么频繁,方法上是差不多的。
相关文章推荐
- 设置导航栏透明
- Xcode7中创建静态库
- 实现多个按钮,点选一个其它都取消选中状态的方法
- iOS设置状态栏和导航栏
- iOS倒计时,显示剩余天、时、分、秒
- iOS关于图片点到像素转换之杂谈
- iOS关于图片点到像素转换之杂谈
- iOS关于图片点到像素转换之杂谈
- iOS-项目搭建
- iOS视图控制器之间delegate传值教程
- 不让苹果开发者账号折磨我
- iOS中可能用到的尺寸
- IOS后台 返回 /Date(1445340103367)/ 时间,转换成 2015-10-20 19:21 这种格式
- iOS开发之 Cocoapods MBProgressHUD 安装步骤
- 芒果iOS开发之定位、地理编码
- IOS程序异常crash捕获与拦截
- <iOS>AFNetworking 3.0的使用
- ios去时间戳
- IOS-调用系统资源
- 蓝懿ios技术交流和心得分享16.1。11