views:

50

answers:

4

How do we know when enumeration is finished? The docs say: the return value of

nextObject

is nil when all objects have been enumerated. I was hoping to implement some "delegate-like" behavior whereby ...

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

But I see there is no such thing as:

enumerationDidFinish:

where in the following block could I check for the enumerator to be complete?

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

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

When the while loop finishes, you know the enumeration is complete. You could call the delegate method then.

Tom Dalling
yep... just a basic thing I was forgetting. This is what happens when you just use cocoa calls nonstop and forget the basics of looping!
samfu_1
+1  A: 

Just put your code after the whole while block.

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

Chris Cooper
+1  A: 

The enumerator finishes when the value returned from nextObject is nil

Jacob Relkin
+1  A: 

How about immediatley after the while() loop. When nextObject returns nil, the enumeration is complete and the loop condition fails, continuing execution immediately after the loop body.

Barry Wark