Skip to content

Instantly share code, notes, and snippets.

@jakeheis
Created March 17, 2013 20:21
Show Gist options
  • Save jakeheis/5183448 to your computer and use it in GitHub Desktop.
Save jakeheis/5183448 to your computer and use it in GitHub Desktop.
NSMutableArray+SKBlock - Initialize an array using a block
#import <Foundation/Foundation.h>
@interface NSMutableArray (SKBlock)
+(id)arrayWithObjectBlock:(id (^)(NSInteger index))block;
-(id)initWithObjectBlock:(id (^)(NSInteger index))block;
@end
#import "NSMutableArray+SKBlock.h"
@implementation NSMutableArray (SKBlock)
+(id)arrayWithObjectBlock:(id (^)(NSInteger index))block {
return [[NSMutableArray alloc] initWithObjectBlock:block];
}
-(id)initWithObjectBlock:(id (^)(NSInteger index))block {
NSInteger counter = 0;
NSMutableArray *newArray = [NSMutableArray array];
while (YES) {
id obj = block(counter++);
if (!obj)
break;
[newArray addObject:obj];
}
return newArray;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment