当前位置 : 主页 > 手机开发 > 其它 >

swift2 – 在Swift中过滤字典数组

来源:互联网 收集:自由互联 发布时间:2021-06-11
我有一个带字典的数组示例: ( { 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 3

func 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()
}
网友评论