• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 为什么 NSEnumerationReverse 而不是 NSEnumerationForward

[复制链接]
菜鸟教程小白 发表于 2022-12-12 10:23:07 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在研究循环遍历一个数组,我偶然发现了这种方法 --

- (void)enumerateObjectsWithOptionsNSEnumerationOptions)opts
                         usingBlockvoid (^)(id obj, NSUInteger idx, BOOL *stop))block

NSEnumerationOptions 定义为 -

enum {
   NSEnumerationConcurrent = (1UL << 0),
   NSEnumerationReverse = (1UL << 1),
};
typedef NSUInteger NSEnumerationOptions;

这些的描述来自NSHipster's blog post about enumeration --

NSEnumerationConcurrent: Specifies that the Block enumeration should be concurrent. The order of invocation is nondeterministic and undefined; this flag is a hint and may be ignored by the implementation under some circumstances; the code of the Block must be safe against concurrent invocation.

NSEnumerationReverse: Specifies that the enumeration should be performed in reverse. This option is available for NSArray and NSIndexSet classes; its behavior is undefined for NSDictionary and NSSet classes, or when combined with the NSEnumerationConcurrent flag.

现在循环通常写成 -

for ( int i=0 ; i< count; i++ )
{
   //stuff
}

我的问题是,为什么 NSEnumerationReverse 存在而 NSEnumerationForward 不存在。为什么 Apple 认为反向循环比从第一个索引循环更好。

反向循环遍历数组有性能优势吗?还是我没有正确理解 NSEnumerationReverse



Best Answer-推荐答案


声明的 NSEnumerationOptions 用于您想要使用非标准行为的时候——前向迭代是自动使用的默认行为除非您指定不同的选项。

要向前迭代,只需将 0 传递给枚举选项参数即可。

还要注意,由于无论如何您都为选项传递了零,因此您也可以只使用不接受任何选项的 enumerateObjectsUsingBlock: 方法,否则会执行完全相同的操作(并且打字更短)。

关于ios - 为什么 NSEnumerationReverse 而不是 NSEnumerationForward,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24205509/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap