Created
March 17, 2012 06:57
-
-
Save boredzo/2055905 to your computer and use it in GitHub Desktop.
Blocks for NSEnumerator
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
// 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