【发布时间】:2012-06-22 08:13:51
【问题描述】:
考虑以下 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];
我能够过滤这个数组以找到包含 key 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 中键的 ANY 的字典对象的数据集。
例如,使用数组:NSArray *keySet = [NSArray arrayWithObjects:@"key1", @"key3", nil]; 我想创建一个谓词,它返回包含“key1”或“key3”的any字典对象数组'(即在本例中,除了第三个对象之外的所有字典对象都将返回 - 因为它不包含 'key1' 或 'key3')。
关于我将如何实现这一目标的任何想法?我必须使用复合谓词吗?
【问题讨论】:
标签: objective-c nsarray nspredicate foundation