Created
August 10, 2011 02:01
-
-
Save CastIrony/1135898 to your computer and use it in GitHub Desktop.
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 "NSArray+BlockEnumerationTest.h" | |
int main(int argc, char *argv[]) | |
{ | |
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; | |
//Beginning of Sam Soffes's code | |
NSArray *array = [[NSArray alloc] initWithObjects:@"foo", @"bar", @"bat", nil]; | |
__block NSString *string = @""; | |
[array enumerateObjectsUsingBlockType1:^(id obj, NSUInteger idx, BOOL *stop) { | |
string = [(NSString *)obj stringByAppendingString:string]; | |
}]; | |
NSLog(@"string: %@", string); // Crashes here | |
[array release]; | |
//Ending of Sam Soffes's code | |
[pool release]; | |
return 0; | |
} |
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
@interface NSArray (BlockEnumerationTest) | |
-(void)enumerateObjectsUsingBlockType1:(void (^)(id, NSUInteger, BOOL *))block; | |
-(void)enumerateObjectsUsingBlockType2:(void (^)(id, NSUInteger, BOOL *))block; | |
@end |
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 "NSArray+BlockEnumerationTest.h" | |
@implementation NSArray (BlockEnumerationTest) | |
-(void)enumerateObjectsUsingBlockType1:(void (^)(id, NSUInteger, BOOL *))block | |
{ | |
if(!block) { return; } | |
for(NSUInteger i = 0; i < self.count; i++) | |
{ | |
BOOL stop = NO; | |
block([self objectAtIndex:i], i, &stop); | |
if(stop) { break; } | |
} | |
} | |
-(void)enumerateObjectsUsingBlockType2:(void (^)(id, NSUInteger, BOOL *))block | |
{ | |
if(!block) { return; } | |
for(NSUInteger i = 0; i < self.count; i++) | |
{ | |
BOOL stop = NO; | |
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; | |
block([self objectAtIndex:i], i, &stop); | |
[pool release]; | |
if(stop) { break; } | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment