-1

I am getting the error 'Extra Argument in Call' when I am trying to implement a search bar into my app.

I have read other questions that include:

Swift - Extra Argument in call

Swift 4 “Extra argument in call” Rxswift

And others but have come up with no success.

Here is my code:

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()
        }

    }
}

}

Nol4635
  • 631
  • 1
  • 14
  • 24

1 Answers1

1

You are mixing up NSPredicate and filter syntax as well as NSSortDescriptor and sorted syntax. This cannot work.

Assuming todoItems is an array of a custom struct or class the native Swift way is

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

Note: Consider that you are going to overwrite the array containing all items with the filtered array...

vadian
  • 274,689
  • 30
  • 353
  • 361