Last active
April 23, 2017 18:39
-
-
Save Adlai-Holler/0d7e4c45e96e329d6c69aa521d30f65c to your computer and use it in GitHub Desktop.
Fast enumeration with index in Objective-C
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
/** | |
* 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