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?
    }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
VikR
  • 4,818
  • 8
  • 51
  • 96

2 Answers2

4

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

Justin Meiners
  • 10,754
  • 6
  • 50
  • 92
2

Better way to do is, use blocks

[dict enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL *stop) {
stop = YES // To break the loop }];
AAV
  • 3,785
  • 8
  • 32
  • 59