使用谓词iOS SDK从Dictionary的数组中过滤数据
你好,我有一个字典的数组,你能告诉我如何过滤这个数据基于字典键。
( { "mall_id" = M0550; "mall_name" = "Amrita Shopping Complex"; }, { "mall_id" = M0509; "mall_name" = "Ashoka Market"; }, { "mall_id" = M0943; "mall_name" = "Biju Pattnaik Commercial Complex"; }, { "mall_id" = M0457; "mall_name" = "BMC Bhawani Mall"; }, { "mall_id" = M0460; "mall_name" = "BMC Keshari Mall"; }, { "mall_id" = M0571; "mall_name" = "BMC Market Complex"; }, { "mall_id" = M0453; "mall_name" = "Forum Mart"; }, { "mall_id" = M0609; "mall_name" = "Indradhanu Market"; }, { "mall_id" = M0558; "mall_name" = "Kalyani Plaza Market Complex"; }, { "mall_id" = M0463; "mall_name" = "Maa Barabhuja Mall"; }, { "mall_id" = M0553; "mall_name" = "Mahaveer Complex"; }, { "mall_id" = M0570; "mall_name" = "Market Building"; }, { "mall_id" = M0452; "mall_name" = "Pal Heights Mall"; }, { "mall_id" = M0466; "mall_name" = "Priyadarshini Market Complex"; }, { "mall_id" = M0677; "mall_name" = "Ruchika Market"; }, { "mall_id" = M0504; "mall_name" = "Shubham Market Complex"; }, { "mall_id" = M0564; "mall_name" = "Subhadra Complex"; }, { "mall_id" = M0559; "mall_name" = "Sultania Shopping Complex"; }, { "mall_id" = M0552; "mall_name" = "Tathastu Complex"; }, { "mall_id" = M0568; "mall_name" = "Western Tower Market Building"; } )
我想实现什么,只要我在search栏中键入任何东西,它将检查mall_name键,并返回数组中的匹配值。
感谢致敬
这会给你你想要的输出
目标 – C
NSArray *filteredData = [yourArrayContainingDictionary filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(mall_name contains[c] %@)", searchText]];
迅速
let filteredData = yourArrayContainingDictionary.filter{ let string = $0["mall_name"] as! String return string.hasPrefix("searchText") }
希望这可以帮助你:)
试试这个。 (谓词像SQL查询一样工作)
Obj C
NSArray *filterArray = [sourceArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(mall_name contains[c] %@)", searchText]];
迅速
var filterArray: [Any] = sourceArray.filter { NSPredicate(format: "(mall_name contains[c] %@)", searchText).evaluate(with: $0) }
它将返回名称中包含searchstring的条目。
使用这个解决scheme
NSArray *filtered = [yourArrayOfDictionary filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(mall_name contains[c] %@)", <your search text from search bar>]];
NSArray *filtered = [arrName filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(mall_name == %@)", requiredMailName]];
NSArray *filtered = [array_with_dict filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(mall_id == %@)", wantedMallID]];
这里是我使用谓词和经典的2个版本:
let dict = [ [ "mall_id": 1, "mall_name": "Amrita Shopping Complex" ], [ "mall_id": 2, "mall_name": "Ashoka Market" ] ] // Example using Predicate let mallNamePredicate = NSPredicate(format: "mall_name contains %@", "Ashoka") let filteredWithPredicate = (dict as NSArray).filtered(using: mallNamePredicate) // Classical filter example let filtered = dict.filter { pair in guard let mallName = pair["mall_name"] as? String else { return false } return mallName.hasPrefix("Ashoka") }
更快捷的方式这样做会是这样的
let mallArray = [ [ "mall_id": "M0550", "mall_name": "Amrita Shopping Complex" ], [ "mall_id": "M0509", "mall_name": "Ashoka Market" ] ] func isMatching(_ searchText: String) -> [[String: Any]] { let filteredArray = mallArray.filter { return $0["mall_name"]!.contains(searchText) } return filteredArray } let malls = isMatching("Ashoka") print(malls)
这给你一个只有requiredMallID
的字典数组
NSArray *filtered = [array_with_dict filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(mall_id == %@)", requiredMallID]];
例如,
NSArray *filtered = [array_with_dict filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(mall_id == %@)", "M0550"]];
给你
( { "mall_id" = M0550; "mall_name" = "Amrita Shopping Complex"; } )