使用快速枚舉時,是否有辦法提前退出,即在通過數組中的每個元素之前?提早退出快速枚舉循環?
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