So *_* It 8 objective-c foundation nsarray nspredicate
考虑以下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];
Run Code Online (Sandbox Code Playgroud)
我能够过滤此数组以查找包含密钥的字典对象 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);
Run Code Online (Sandbox Code Playgroud)
适当回报:
filteretSet1: (
{
key1 = abc;
key2 = def;
key3 = hij;
},
{
key1 = klm;
key2 = nop;
}
)
Run Code Online (Sandbox Code Playgroud)
现在,我想过滤包含NSArray 中任何键的字典对象的dataSet .
例如,使用数组:NSArray *keySet = [NSArray arrayWithObjects:@"key1", @"key3", nil];我想创建一个谓词,返回包含'key1' 或 'key3' 的任何字典对象的数组(即在此示例中,除第三个对象外,将返回所有字典对象 - 如它不包含'key1' 或 'key3').
有关如何实现这一目标的任何想法?我是否必须使用复合谓词?
该ANY运营商NSPredicate覆盖这样的:
NSSet *keys = [NSSet setWithObjects:@"key1", @"key3", nil];
NSPredicate *key1Predicate = [NSPredicate predicateWithFormat:@"any self.@allKeys in %@", keys];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4714 次 |
| 最近记录: |