Created
October 8, 2015 20:19
-
-
Save khanlou/7b9037c8b8c7a1e870c2 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
@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