Skip to content

Instantly share code, notes, and snippets.

@khanlou
Created October 8, 2015 20:19
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 khanlou/7b9037c8b8c7a1e870c2 to your computer and use it in GitHub Desktop.
Save khanlou/7b9037c8b8c7a1e870c2 to your computer and use it in GitHub Desktop.
@interface MappedArray ()
@property (nonatomic) NSArray *backingArray;
@end
@implementation MappedArray
- (instancetype)initWithArray:(NSArray *)array transformationBlock:(id (^)(id object))block {
self = [super init];
if (!self) return nil;
NSMutableArray *mappedArray = [NSMutableArray arrayWithCapacity:array.count];
for (NSInteger i = 0; i < array.count; i++) {
[mappedArray addObject:block(array[i]) ?: [NSNull null]];
}
_backingArray = [mappedArray copy];
return self;
}
- (NSUInteger)count {
return self.backingArray.count;
}
- (id)objectAtIndex:(NSUInteger)index {
return [self.backingArray objectAtIndex:index];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment