Skip to content

Instantly share code, notes, and snippets.

@Adlai-Holler
Last active April 23, 2017 18:39
Show Gist options
  • Save Adlai-Holler/0d7e4c45e96e329d6c69aa521d30f65c to your computer and use it in GitHub Desktop.
Save Adlai-Holler/0d7e4c45e96e329d6c69aa521d30f65c to your computer and use it in GitHub Desktop.
Fast enumeration with index in Objective-C
/**
* Performs fast enumeration and keeps track of the enumeration index for you.
* If you don't need the index, you can use for-in directly, or pass `_` for the index name.
* Fast enumeration is _much_ faster than block enumeration, mostly due to the removal
* of many calls to -retain/-release. The only case where block enumeration is justified
* is in NSDictionary (if you need both key&value) and NSIndexSet.
*
* Example:
* PINForEach(NSString *str, self.titles, row, ({
* NSLog(@"Title %zd: %@", row, str);
* }));
*/
#define PINForEach(element, collection, index, body) ({ \
NSInteger index = -1; \
for (element in collection) { \
index++; \
body; \
} \
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment