Created
October 11, 2013 05:26
-
-
Save ethereal-engineer/6929981 to your computer and use it in GitHub Desktop.
An example of how enumerators can be used to separate the collection of objects from the processing code. It's worthwhile also noting that enumerators can be created to return objects in any desired order.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
void enumerate(NSEnumerator *enumerator, NSString *name) | |
{ | |
id obj = [enumerator nextObject]; | |
NSLog(@"Enumerating using '%@'...", name); | |
while (obj) | |
{ | |
NSLog(@"%@", obj); | |
obj = [enumerator nextObject]; | |
} | |
} | |
int main(int argc, char *argv[]) | |
{ | |
@autoreleasepool | |
{ | |
NSArray *myArray = @[@"alpha", @"beta", @"gamma", @"delta"]; | |
enumerate([myArray objectEnumerator], @"Forwards"); | |
enumerate([myArray reverseObjectEnumerator], @"Backwards"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment