Objective-C 中的快速枚举是否保证迭代顺序? [英] Does fast enumeration in Objective-C guarantee the order of iteration?

查看:46
本文介绍了Objective-C 中的快速枚举是否保证迭代顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以期望它从数组的开头到结尾按顺序排列吗?在文档中找不到关于此的任何内容.

Can I expect it to go from the start of an array to the end in order? Can't find anything in the docs about this.

即是

for (id val in array)
{
   NSLog(@"%@", val);
}

总是会打印出与

for (int i = 0; i < [array count]; ++i)
{
   NSLog(@"%@", [array objectAtIndex:i]);
}

推荐答案

来自 Apples 的 Objective-C 文档关于快速枚举:

From Apples' Objective-C documentation on fast enumeration:

对于具有明确定义顺序的集合或枚举器(例如从数组派生的 NSArray 或 NSEnumerator 实例),枚举按该顺序进行,因此如果需要,只需计算迭代次数即可为您提供正确的集合索引.

For collections or enumerators that have a well-defined order—such as NSArray or NSEnumerator instance derived from an array—the enumeration proceeds in that order, so simply counting iterations will give you the proper index into the collection if you need it.

这篇关于Objective-C 中的快速枚举是否保证迭代顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆