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);
}];
Written by Max Goedjen
Related protips
1 Response
Thanks! That's really neat.
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Objective-c
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#