快速遍历方法
- 遍历数组 For-in
- 遍历字典 enumerateKeysAndObjectsUsingBlock
字典
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(@"key: %@, value: %@", key, obj);
}];
- 耗时且顺序无关的遍历,用并发方式
[dict enumerateKeysAndObjectsWithOptions:NSEnumerationReverse usingBlock:^(id key,id obj,BOOL *stop) {
NSLog(@"key:%@-----value:%@----",key,obj);
}];
数组
[array enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL * stop) {
NSLog(@"1:%@----%d",obj,(int)idx);
}];
- 并发遍历
[array enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id obj,NSUInteger idx,BOOL * stop) {
NSLog(@"2:%@----%d",obj,(int)idx);
}];
NSEnumerationReverse 倒序遍历
- 用 for - in 做倒序遍历
for (NSString *string in [array reverseObjectEnumerator]) {
NSLog(@"12:%@----",string);
}