使用搜索栏搜索NSDictionary

时间:2016-10-23 19:03:27

标签: swift nsdictionary uisearchbar

我想知道如何使用filter NSDictionary searchBar

我声明了2个变量var usersArray = [NSDictionary?]()var filteredUsers = [NSDictionary?]()

现在我试图像这样过滤它们:

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        self.filteredUsers = self.usersArray.filter{ user in
        let username = user!["username"] as? String
        return(username?.lowercased().contains(searchText.lowercased()))!
    }
        if searchText != ""{
            shouldShowSearchResults = true
            followUsersTableView.reloadData()
        }else{
            shouldShowSearchResults = false
            followUsersTableView.reloadData()
        }

    }

我做了updateSearchResults

filteredUsers.removeAll(keepingCapacity: false)

        let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
        let array = (usersArray as NSDictionary).filtered(using: searchPredicate)
        filteredUsers = array as! [String]

        self.followUsersTableView.reloadData()

但由于某种原因,它没有过滤它。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

在操场上掀起这个,工作正常

c_str()

PS我会改变你的if语句来检查!filteredUsers.isEmpty而不是searchText。因为你真的只想重新加载,如果该数组有东西要显示,无论searchText是否为nil

enter image description here