Search code examples
swiftswift-protocolsuisearchbardelegate

Redundant conformance of <view controller> to protocol 'UISearchBarDelegate' - Swift 4


I have this error

Redundant conformance of 'TodoListViewController' to protocol 'UISearchBarDelegate'

The error is popping up over "UISearchBarDelegate" on line 1.

extension TodoListViewController: UISearchBarDelegate {

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {

    let request : NSFetchRequest<Item> = Item.fetchRequest()

    let predicate = NSPredicate(format: "title CONTAINS[cd] %@", searchBar.text!)

    request.predicate = predicate

    let sortDescriptor = NSSortDescriptor(key: "title", ascending: true)

    request.sortDescriptors = [sortDescriptor]

    do {
        itemArray = try context.fetch(request)
    } catch {
        print("Error fetching data from context \(error)")
    }

    tableView.reloadData()

  }
}

class declaration for TodoListViewController:

class TodoListViewController: UITableViewController, UISearchBarDelegate {

I have researched into this and no other questions are valid to my error.


Solution

  • You add delegate at extension extension TodoListViewController: UISearchBarDelegate {

    And at Declaration class TodoListViewController: UITableViewController, UISearchBarDelegate {

    remove it from declaration

    class TodoListViewController: UITableViewController {
    
    }