Skip to content

Instantly share code, notes, and snippets.

@boredzo
Created March 17, 2012 06:57
Show Gist options
  • Save boredzo/2055905 to your computer and use it in GitHub Desktop.
Save boredzo/2055905 to your computer and use it in GitHub Desktop.
Blocks for NSEnumerator
// Copyright (c) 2012 Colin Barrett, Springs & Struts <[email protected]>
// Released under the MIT license. http://www.opensource.org/licenses/mit-license.html
#import <Foundation/Foundation.h>
@interface NSEnumerator (SSEnumeratorWithBlocks)
- (void)ss_enumerateObjectsUsingBlock:(void (^)(id obj, BOOL *stop))enumBlock;
@end
@implementation NSEnumerator (SSEnumeratorWithBlocks)
- (void)ss_enumerateObjectsUsingBlock:(void (^)(id obj, BOOL *stop))enumBlock
{
BOOL stop = NO;
id obj = nil;
for (obj in self) {
enumBlock(obj, &stop);
if (stop)
break;
}
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSEnumerator *testEnumerator = [[NSArray arrayWithObjects:@"hello", @"world", @"DESTROY", nil] objectEnumerator];
[testEnumerator ss_enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
if ([obj isEqual:@"world"]) {
*stop = YES;
}
NSLog(@"%@", obj);
}];
}
return 0;
}
// output:
// hello
// world
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment