In Objective-C, is it possible to restart to the first iteration of a for loop? I don't want to do anything with my objects until I find a "good" object, at which point I want to go back and do stuff with every object up to that good object.
So,
bool someflag = false;
for (id object in array)
{
if(object is good)
{
someflag = true;
//restart to first object in loop
}
if(someflag)
{
doStuffWithObject(object);
}
}
Or is there a different/better way to do what I'm trying to do?
Obviously the easy way would be to just have two separate for
loops -- so if I could get a second opinion telling me that's the best way, that's just what I'll shoot for. For some reason I have a feeling there's got to be a better way, though.
Not with fast enumeration, no (with the exception of goto), however if you use the indexed-access approach, you could:
NSUInteger count = [array count];
for (NSUInteger i = 0; i < count; i++)
{
bool someflag = false;
id object = array[i];
if (isgood(object))
{
someflag = true;
//restart to first object in loop
i = 0;
}
if(someflag)
{
doStuffWithObject(object);
}
}