I have a UITableViewController
class in which I am implementing a UISearchController
. I've added the following delegates:
class EmployeesTableView: UITableViewController, NSFetchedResultsControllerDelegate,UISearchResultsUpdating{
I'm importing both UIKit
and CoreData
. I'm getting the following error:
"Type 'CustomTableViewController' does not conform to protocol UISearchResultsUpdating"
What do I need to do to make the controller conform to the protocol?
Swift 3:
func updateSearchResults(for searchController: UISearchController) {
// code here
}
When you add protocols to your class definition, the easiest way is to mouse over the protocol name and command click its name. This will pull up its definition. With protocol definitions, they usually have methods immediately following them. If a method is required it will be at the top, if it has optional in front, then it isn't required in order to conform.
In the case of `UISearchResultsUpdating, it only has one method and it is required. Just copy the method, or multiple methods and click the back arrow to get back to your class. Paste the methods into your class, and implement them. If they were optional methods (in this case there are no optional methods), remove optional from the front. This is what I copied from the definition.
func updateSearchResultsForSearchController(searchController: UISearchController)
Then you update it to do what you want to do.
func updateSearchResultsForSearchController(searchController: UISearchController) {
//do whatever with searchController here.
}
As an additional example, command click on NSFechedResultsControllerDelegate
. You will see that it has no required methods, but lots of optional ones. This information is usually found in the documentation as well, but I've found command + click to be the fastest way to find what I'm looking for.
Swift 3.0
//Make sure to import UIKit
import Foundation
import UIKit
class ViewController: UIViewController, UISearchBarDelegate {
var searchController = UISearchController()
override func viewDidLoad() {
//Setup search bar
searchController = UISearchController(searchResultsController: nil)
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
//Set delegate
searchController.searchResultsUpdater = self
//Add to top of table view
tableView.tableHeaderView = searchController.searchBar
}
}
extension ViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
print(searchController.searchBar.text!)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With