When does NSEnumerator end?

How do we know when the listing is complete? The docs say: return value

nextObject

      

equals zero when all objects have been listed. I was hoping to implement some "delegate-like behavior" resulting in ...

if (nextObject == nil) { 
    do something because we're done!
}

      

But I see that there is no such thing as:

enumerationDidFinish:

      

where in the next block I can check the completion of the enumerator?

NSArray *anArray = // ... ;
NSEnumerator *enumerator = [anArray objectEnumerator];
id object;

while ((object = [enumerator nextObject])) {
    // do something with object...
}

      

+2


a source to share


5 answers


When the loop while

ends, you know the enumeration is complete. Then you can call the delegate method.



+3


a source


Just put your code after the whole block while

.



Then when the enumeration is done, it will be done and you will know that it has reached the end.

+2


a source


The enumerator ends when the value returned from nextObject

isnil

+2


a source


How about an immediate while () loop. When nextObject returns nil, the enumeration ends and the loop condition fails, continuing immediately after the body loop.

+2


a source


Since if the returned "object" is nil, the while loop will not continue executing in the body, it will break through to the end of the loop, putting whatever you want to do with your object would be a good idea.

0


a source







All Articles