krisacorn krisacorn - 11 months ago 83
Swift Question

How do I make a UITableViewController conform to protocol UISearchResultsUpdating?

I have a

class in which I am implementing a
. I've added the following delegates:

class EmployeesTableView: UITableViewController, NSFetchedResultsControllerDelegate,UISearchResultsUpdating{

I'm importing both
. 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?

Answer Source

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.