我有一个带字典的数组示例: ( { Email = "kate-bell@mac.com"; Name = "Kate Bell"; Number = "(555) 564-8583"; }, { Email = "d-higgins@mac.com"; Name = "Daniel Higgins"; Number = "555-478-7672"; }) 我想根据密钥“名称”过滤
( { Email = "kate-bell@mac.com"; Name = "Kate Bell"; Number = "(555) 564-8583"; }, { Email = "d-higgins@mac.com"; Name = "Daniel Higgins"; Number = "555-478-7672"; } )
我想根据密钥“名称”过滤这个字典
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) { let predicate = NSPredicate(format:"Name == %@", searchText) let filteredArray = (arrContact as NSMutableArray).filteredArrayUsingPredicate(predicate) print(filteredArray) if(filteredArray.count == 0){ searchActive = false; } else { searchActive = true; } tblData.reloadData() }
我总是从上面的swift代码得到空数组.请帮我解决这个问题.谢谢
试试这个swift 3func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { // Put your key in predicate that is "Name" let searchPredicate = NSPredicate(format: "Name CONTAINS[C] %@", searchText) let array = (arrContact as NSArray).filtered(using: searchPredicate) print ("array = \(array)") if(array.count == 0){ searchActive = false; } else { searchActive = true; } self.aTable.reloadData() }