ios遍历数组的方法
发表时间:2020-10-19
发布人:葵宇科技
浏览次数:46
今朝所知有七种办法
//第一种
[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]);
}
小我比较爱好第一种办法
留意:
① 个中第二种办法因为是并行,所以打印出来的器械是随机的,并不是按照次序打印的
② 第三种轻易引起主线程堵塞,所以最好本身别的创建一个线程
③ 本人学识浅,今朝所知的就这几种,迎接弥补
③








