Easy NSArray filtering
Like when filtering Core Data queries, NSPredicate can be used on arrays:
NSArray *robocatApps = @[ @"Thermo", @"Outside", @"101st Airborne", @"Ultraviolet", @"Treasure Trouble", @"Dunk" ];
NSArray *shortNameApps = [robocatApps filteredArrayUsingPredicate:[NSPredicate predicateWithFormat: @"self.length <= 7"]];
NSArray *longNameApps = [robocatApps filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id obj, NSDictionary *bindings){
return ([obj length] > 7);
}]];
NSLog(@"short name apps: %@", shortNameApps);
// Thermo, Outside, Dunk
NSLog(@"long name apps: %@", longNameApps);
// "101st Airborne", Ultraviolet, "Treasure Trouble"
Written by Ulrik Flænø Damm
Related protips
1 Response
yes and on sets too ;)
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#