當前位置:成語大全網 - 新華字典 - iOS中遍歷數組的方法

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]);

});