简体   繁体   English

NSDictionary的NSArray的过滤器和NSDictionary

[英]Filter and NSDictionary of NSArray of NSDictionary's

I have an NSDictionary which contains some 150 keys of boiler manufacturers. 我有一个NSDictionary,其中包含大约150个锅炉制造商的钥匙。 The value for each key is an NSArray of an NSDictionary. 每个键的值是NSDictionary的NSArray。 Each NSDictionary represents a boiler with some properties: 每个NSDictionary代表具有某些属性的锅炉:

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? 我希望能够进行过滤,以便获得所有额定值为80的锅炉。我知道我需要一个NSPredicate,但是我不知道如何构建它? 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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