Last Updated: February 25, 2016
·
854
· maxgoedjen

Python-like comprehensions for NSArray

NSArray *test = @[@1, @2, @3, @4, @5];
NSArray *comp = [test arrayWithComprehension:^id(id obj) {
    return @([obj intValue] * [obj intValue]);
}];

Filtering is supported too.

NSArray *comp = [test arrayWithComprehension:^id(id obj) {
    return @([obj intValue] * [obj intValue]);
} ifCondition:^BOOL(id obj) {
    return ([obj intValue] % 2 == 0);
}];

Download on GitHub

1 Response
Add your response

Thanks! That's really neat.

over 1 year ago ·