VikR
VikR

Reputation: 5142

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

Upvotes: 1

Views: 320

Answers (2)

AAV
AAV

Reputation: 3803

Better way to do is, use blocks

[dict enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL *stop) {
stop = YES // To break the loop }];

Upvotes: 2

Justin Meiners
Justin Meiners

Reputation: 11113

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

Upvotes: 4

Related Questions