Skip to content

Instantly share code, notes, and snippets.

@CastIrony
Created August 10, 2011 02:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CastIrony/1135898 to your computer and use it in GitHub Desktop.
Save CastIrony/1135898 to your computer and use it in GitHub Desktop.
#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;
}
@interface NSArray (BlockEnumerationTest)
-(void)enumerateObjectsUsingBlockType1:(void (^)(id, NSUInteger, BOOL *))block;
-(void)enumerateObjectsUsingBlockType2:(void (^)(id, NSUInteger, BOOL *))block;
@end
#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