iOS中遍历数组的方法

1.使用普通的for循环

NSArray *ary = @[@"我",@"是",@"张",@"小",@"倍",@"er"];

for (int i = 0; i < ary.count; i ++) {

NSLog(@"%@",[ary objectAtIndex:i]);

}


2.使用for in 进行遍历

NSArray *ary = @[@"我",@"是",@"张",@"小",@"倍",@"er"];

for (NSString *str in ary) {

NSLog(@"%@",str);

}


3.使用do while

NSArray *ary = @[@"我",@"是",@"张",@"小",@"倍",@"er"];

int i = 0;

do {

NSLog(@"%@",[ary objectAtIndex:i]);

i ++;

} while (i < ary.count);

}

4.使用while do 

NSArray *ary = @[@"我",@"是",@"张",@"小",@"倍",@"er"];

int i = 0;

while (i < ary.count) {

NSLog(@"%@",[ary objectAtIndex:i]);

i ++;

}

5.使用快速枚举

NSArray *ary = @[@"我",@"是",@"张",@"小",@"倍",@"er"];

[ary enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

NSLog(@"%ld,%@,%@",idx ,[ary objectAtIndex:idx],obj);

}];

6.

NSArray *ary = @[@"我",@"是",@"张",@"小",@"倍",@"er"];

dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT);

dispatch_apply([ary count],queue, ^(size_t index){

NSLog(@"%ld,%@",index,[ary objectAtIndex:index]);

});

你可能感兴趣的:(iOS中遍历数组的方法)