考虑以下 NSArray:
NSArray *dataSet = [[NSArray alloc] initWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:@"abc", @"key1", @"def", @"key2", @"hij", @"key3", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"klm", @"key1", @"nop", @"key2", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"qrs", @"key2", @"tuv", @"key4", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"wxy", @"key3", nil],
nil];
我能够过滤此数组以查找包含键的字典对象 key1
// Filter our dataSet to only contain dictionary objects with a key of 'key1'
NSString *key = @"key1";
NSPredicate *key1Predicate = [NSPredicate predicateWithFormat:@"%@ IN self.@allKeys", key];
NSArray *filteretSet1 = [dataSet filteredArrayUsingPredicate:key1Predicate];
NSLog(@"filteretSet1: %@",filteretSet1);
哪个适当地返回:
filteretSet1: (
{
key1 = abc;
key2 = def;
key3 = hij;
},
{
key1 = klm;
key2 = nop;
}
)
现在,我想过滤包含NSArray 中任何键的字典对象的数据集。
例如,使用数组:NSArray *keySet = [NSArray arrayWithObjects:@"key1", @"key3", nil];
我想创建一个谓词,该谓词返回包含“key1”或“key3”的任何字典对象的数组 (即在此示例中,将返回除第三个对象之外的所有字典对象 - 如它不包含“key1”或“key3”)。
关于我将如何实现这一目标的任何想法?我必须使用复合谓词吗?