1

When using fast enumeration, is there a way to exit early, i.e. before going through every element in the array?

    for (element in myArray)
    {
        //is there a way to exit before running through every element in myArray?
    }
4

2 回答 2

4

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

于 2013-07-12T18:02:42.883 回答
2

更好的方法是使用块

[dict enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL *stop) {
stop = YES // To break the loop }];
于 2013-07-12T18:32:53.177 回答