Fast enumeration of an NSArray with index
suggest changeThis example traverses an NSArray and also reports each object’s index.
Suppose you have an array,
NSArray *weekDays = @[@"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday", @"Sunday"];
Now you can traverse through the array like below,
[weekDays enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
//... Do your usual stuff here
obj // the current object
idx // the index of the current object
*stop = YES; // set this to stop enumeration early
}];
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents