I have a loop using the syntax for (NSObject * obj in someArray) {}. Is there an easy way to tell if I'm at the last iteration of the loop (ie Without using [someArray count])
Maybe this will work?
if ( obj == [ someArray lastObject ] ) { // ... }
You can use NSArray#lastObject to determine if obj [NSArray lastObject] .
NSArray#lastObject
obj
[NSArray lastObject]
for (NSObject *obj in someArray) { if ([someArray lastObject] == obj) { NSLog(@"Last iteration"); } }
Instead of being called into an array at each iteration, it might be better to cache the last object in the array:
NSObject *lastObject = [someArray lastObject]; for (NSObject *obj in someArray) { // Loop code if (obj == lastObject) { // do what you want for the last array item } }