Search code examples
objective-cfast-enumeration

Early Exit from Fast Enumerate Loop?


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?
    }

Solution

  • break; will exit any for, while, or do loop.

    For example:

    for (element in myArray)
    {
         if (someExitCondition)
         {
             break; /* leave now */
         }
    }
    

    Read this:

    http://msdn.microsoft.com/en-us/library/wt88dxx6(v=vs.80).aspx