16.多用块枚举,少用for循环

1、四种遍历方式
基本for循环、NSEnumerator、forin快速遍历、块枚举

2、多用快枚举

[array enumerateObjectsWithOptions:NSEnumerationConcurrent | NSEnumerationReverse
                        usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    
}];

[dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
    
}];

数组可同时访问元素和提供下标,字典可同时提供键和值。
块枚举通过GCD来并发执行遍历操作,开启了并发迭代功能。
若提前知道待遍历集合中含何种对象,则应该修改块签名,指出对象的具体类型。

你可能感兴趣的:(16.多用块枚举,少用for循环)