У меня есть следующий NSArray, содержащий NSDictionary(s):
NSArray *data = [[NSArray alloc] initWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:1], @"bill", [NSNumber numberWithInt:2], @"joe", nil],
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:3], @"bill", [NSNumber numberWithInt:4], @"joe", [NSNumber numberWithInt:5], @"jenny", nil],
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:6], @"joe", [NSNumber numberWithInt:1], @"jenny", nil],
nil];
Я хочу создать отфильтрованный NSArray, который содержит только объекты, в которых NSDictionary сопоставляет несколько «ключей» с помощью NSPredicate.
Например:
Кто-нибудь может объяснить формат NSPredicate для достижения этой цели?
Изменить: Я могу добиться результата, аналогичного желаемому NSPredicate, используя:
NSMutableArray *filteredSet = [[NSMutableArray alloc] initWithCapacity:[data count]];
NSString *keySearch1 = [NSString stringWithString:@"bill"];
NSString *keySearch2 = [NSString stringWithString:@"joe"];
for (NSDictionary *currentDict in data){
// objectForKey will return nil if a key doesn't exists.
if ([currentDict objectForKey:keySearch1] && [currentDict objectForKey:keySearch2]){
[filteredSet addObject:currentDict];
}
}
NSLog(@"filteredSet: %@", filteredSet);
Я полагаю, что NSPredicate был бы более элегантным, если бы он существовал?