考虑以下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;
}
)现在,我希望筛选字典对象的dataSet,其中包含NSArray中的任何键。
例如,使用数组:NSArray *keySet = [NSArray arrayWithObjects:@"key1", @"key3", nil];,我希望创建一个谓词,返回包含“key1”或“key3”(即“key3”)的任何字典对象的数组。在本例中,除了第三个对象之外,将返回所有字典对象--因为它既不包含'key1‘也不包含'key3')。
对我如何做到这一点有什么想法吗?我要用复合谓词吗?
发布于 2012-06-20 09:00:11
ANY操作符NSPredicate涵盖了以下内容:
NSSet *keys = [NSSet setWithObjects:@"key1", @"key3", nil];
NSPredicate *key1Predicate = [NSPredicate predicateWithFormat:@"any self.@allKeys in %@", keys];https://stackoverflow.com/questions/11115377
复制相似问题