使用快速枚举时,是否有办法提前退出,即在通过数组中的每个元素之前?提早退出快速枚举循环?
for (element in myArray)
{
//is there a way to exit before running through every element in myArray?
}
使用快速枚举时,是否有办法提前退出,即在通过数组中的每个元素之前?提早退出快速枚举循环?
for (element in myArray)
{
//is there a way to exit before running through every element in myArray?
}
break;
将退出任何for
,while
,或do
循环。
例如:
for (element in myArray)
{
if (someExitCondition)
{
break; /* leave now */
}
}
阅读:
http://msdn.microsoft.com/en-us/library/wt88dxx6(v=vs.80).aspx
更好的办法是,使用块
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL *stop) {
stop = YES // To break the loop }];
这种方法有什么好处? –
在原来的文章中,他们已经在使用循环标准,所以我没有理由在没有需要时将其更改为使用块。 –
我不是说他/她应该使用这个,但根据苹果enumerateKeysAndObjectsUsingBlock比普通循环和快速枚举更快。 – AAV
非常感谢! – VikR