Last Updated: February 25, 2016
·
1.963K
· ulrikdamm

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"

1 Response
Add your response

yes and on sets too ;)

over 1 year ago ·