简体   繁体   中英

Filter and NSDictionary of NSArray of NSDictionary's

I have an NSDictionary which contains some 150 keys of boiler manufacturers. The value for each key is an NSArray of an NSDictionary. Each NSDictionary represents a boiler with some properties:

NSDictionary boilerData =
{
     @"Alpha" = [{name: Boiler1, rating: 80}, {name:Boiler2, rating: 90}],
     @"Beta" = [{name: Boiler3, rating: 80}, {name:Boiler4, rating: 91}, {name:Boiler5, rating: 78}],
     ...
}

I would like to be able to filter so that I could get all boiler's that have a rating of 80. I know I need an NSPredicate, but I can't figure out how to build it? None of the other articles I've found seem to fit this requirement.

NSDictionary *boilerData =
@{
  @"Alpha" : @[@{@"name": @"Boiler1", @"rating": @80}, @{@"name": @"Boiler2", @"rating": @90}],
  @"Beta" : @[@{@"name": @"Boiler3", @"rating": @98}, @{@"name": @"Boiler4", @"rating": @80}, @{@"name": @"Boiler5", @"rating": @90}]
  };

NSMutableArray *filteredArray = [[NSMutableArray alloc] init];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"rating = 80"];
for (NSArray *array in [boilerData allValues]) {
    [filteredArray addObjectsFromArray:[array filteredArrayUsingPredicate:predicate]];
}
NSLog(@"all boilers with rating = 80 : %@", filteredArray);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM