的一個NSArray過濾考慮下面的NSArray:NSPredicate「或」基於鍵
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 [email protected]", key];
NSArray *filteretSet1 = [dataSet filteredArrayUsingPredicate:key1Predicate];
NSLog(@"filteretSet1: %@",filteretSet1);
字典對象
其中適當返回:
filteretSet1: (
{
key1 = abc;
key2 = def;
key3 = hij;
},
{
key1 = klm;
key2 = nop;
}
)
現在,我想過濾數據集的字典對象,其中包含ANY的NSArray中的鍵。
例如,使用陣列:NSArray *keySet = [NSArray arrayWithObjects:@"key1", @"key3", nil];
我想創建返回和陣列的是包含「KEY1」 或「KEY3」(即在這個例子中所有字典對象將任何字典對象的謂詞。除第三個對象外 - 因爲它不包含'key1'或'key3')。
關於如何實現這一點的任何想法?我將不得不使用複合謂詞嗎?
*任何*是我失蹤。我不知道爲什麼我發現NSPredicate非常難以讓我頭腦發熱。感謝您的回答。 Monolo +1 :) –
是否區分大小寫?我最終會發現,但我希望它已經。稍後再回複評論。 – coolcool1994
@ coolcool1994上面的謂詞中的ANY和IN(以及SELF)不區分大小寫(事實上,我經常在全部大寫中寫出它們以使它們脫穎而出),鍵/方法名稱是,所以'@ allKeys' _is_區分大小寫。 – Monolo