Using NSPredicate to filter an NSArray based on NSDictionary keys

問題:

I have an array of dictionaries. 我有一系列字典。

I want to filter the array based on a key. 我想基於密鑰過濾數組。

I tried this: 我試過這個:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(SPORT ==  %@)", @"Football"];

NSArray *filteredArray = [data filteredArrayUsingPredicate:predicate];

This doesn't work, I get no results. 這不起作用,我沒有結果。 I think I'm doing something wrong. 我想我做錯了什麼。 I know this is the method if "SPORT" was an ivar. 我知道如果“SPORT”是一個ivar,這就是方法。 I think it is probably different if it is a key. 我認爲如果它是關鍵的話可能會有所不同。

I haven't been able to find an example however. 然而,我無法找到一個例子。

Thanks 謝謝


Update 更新

I added quotes around the string I am searching for. 我在我正在搜索的字符串周圍添加了引號。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(SPORT ==  '%@')", @"Football"];

It still does not work. 它仍然無法正常工作。


Update 2 更新2

Solved it. 解決了它。 I actually had to remove the single quotes, which seems to go against what the guide says. 我實際上不得不刪除單引號,這似乎與指南所說的相反。

My real problem is I had a nested array and I wasn't actually evaluating the dictionaries. 我真正的問題是我有一個嵌套數組,我實際上並沒有評估字典。 Bone head move. 骨頭移動。


解決方案:

參考: https://stackoom.com/en/question/41Ne
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章