使用搜索栏时调用中的额外参数

Extra argument In Call When Using Searchbar

当我尝试在我的应用程序中实现搜索栏时出现错误 'Extra Argument in Call'。

我已阅读其他问题,包括:

Swift - Extra Argument in call

还有其他人,但都没有成功。

这是我的代码:

extension ViewController: UISearchBarDelegate {

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {

    todoItems = todoItems.filter("title CONTAINS[cd] %@", searchBar.text!).sorted(byKeyPath: "dateCreated", ascending: true) // Getting Error on this line

    tableView.reloadData()

}


func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    if searchBar.text?.count == 0 {
        loadItems()

        DispatchQueue.main.async {
            searchBar.resignFirstResponder()
        }

    }
}

}

您混淆了 NSPredicatefilter 语法以及 NSSortDescriptorsorted 语法。这行不通。

假设 todoItems 是自定义结构的数组或 class 本机 Swift 方式是

todoItems = todoItems.filter{ [=10=].title.range(of: searchBar.text!, options: [.caseInsensitive, .diacriticInsensitive]) != nil}
                     .sorted{ [=10=].dateCreated < .dateCreated}

注意:考虑到您要用过滤后的数组覆盖包含所有项目的数组...