无法过滤使用字典数组创建的表视图

Unable to filter tableview created using array of dictionary

我是 swift 和 Xcode 的新手,我一直在使用 plist 创建具有表视图(名字为 space 姓氏的员工)的应用程序用作 NSarray 的字典。我正在尝试添加搜索功能。

这是我的代码:

var employeeSearch = Array>()

let searchController = UISearchController(searchResultsController: nil)

func getSwiftArrayFromPlist() -> (Array<Dictionary<String,String>>) {

    let path = Bundle.main.path(forResource: "Sheet1", ofType: "plist")
    var arr: NSArray?
    arr = NSArray(contentsOfFile: path!)
    return (arr as? Array<Dictionary<String,String>>)!
}


func searchBarIsEmpty() -> Bool {
    // Returns true if the text is empty or nil
    return searchController.searchBar.text?.isEmpty ?? true
}

// 上面的函数没有错误

// 我在下面的函数中遇到了一些错误

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
    employeeSearch = getSwiftArrayFromPlist().filter { [=11=]["lastname".lowercased().contains(searchText.lowercased()]}
    print(employeeSearch)
    searchActive = !employeeSearch.isEmpty
    tblData.reloadData()
}
//getting many errors in above attempt to get a filtered array of dictionary to reload data in tableview 

感谢任何帮助。谢谢

//3 月 1 日 - 终于成功了,但只有等于条件没有错误。我的期望是包含功能.. func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    if searchBar.text == nil || searchBar.text == "" {
        isSearching = false
        view.endEditing(true)
        employee1View.reloadData()
    } else {
        isSearching = true
        employeeSearch = getSwiftArrayFromPlist()

        currentEmployeeSearch = employeeSearch.filter {([=12=]["lastname"]?.lowercased() == searchText.lowercased())}
        //currentEmployeeSearch = employeeSearch.filter {([=12=]["lastname"]?.lowercased().contains(searchText.lowercased())}
        employee1View.reloadData()
    }
}

// ******************************************

我认为过滤器中的括号不正确。我相信

employeeSearch = getSwiftArrayFromPlist().filter { [=10=]["lastname".lowercased().contains(searchText.lowercased()]}

应该是

employeeSearch = getSwiftArrayFromPlist().filter { [=11=]["lastname"].lowercased().contains(searchText.lowercased())}

更新答案以帮助处理可选值

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    employeeSearch = getSwiftArrayFromPlist()

    guard !searchText.isEmpty else {
        // In here set the array used in the rest of the app equal to employeeSearch
        return employeeSearch
    }

    currentEmployeeSearch = employeeSearch.filter{
        guard let lowerCasedLastName = [=12=]["lastname"].lowercased() else {
            //If we didn't get a lowercased last name from the employeeSearchArray we return false
            // false means we don't want to include that in the search results array
            return false }
        //If we do have a lowerCasedLastName lets see if it contains the search text
        return lowerCasedLastName.contains(searchText.lowercased())
    }

    employee1View.reloadData()

    //Note: Depending on what array the rest of your application uses you will want to set it equal to the value returned from the filter or returned from the else block of the guard against searchText being empty.
}

史蒂夫的解决方案奏效了。非常感谢你! //************************ func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    if searchBar.text == nil || searchBar.text == "" {
        isSearching = false
        view.endEditing(true)
        employee1View.reloadData()
    } else {
        isSearching = true
        employeeSearch = getSwiftArrayFromPlist()

        //currentEmployeeSearch = employeeSearch.filter {([=10=]["lastname"]?.lowercased() == searchText.lowercased())}
        currentEmployeeSearch = employeeSearch.filter {([=10=]["lastname"]?.lowercased().contains(searchText.lowercased())) ?? false}
        employee1View.reloadData()
    }
}