Let's say I have a protocol in Swift
protocol SearchCompletionProtocol {
func searchSuccessful(results: [AnyObject])
func searchCancelled()
}
Now let's say that I have a class and in the init method I want to pass in a single argument. The restriction is that I want this argument to be of type UIViewController
and also conform to the SearchCompletionProtocol
protocol. How would I go about doing that? Here are some examples of things I've tried and they all don't work.
class SearchDelegate: UISearchDisplayController, UISearchBarDelegate {
let completionDelegate: SearchCompletionProtocol
init<T: SearchCompletionProtocol where T: UIViewController>(completionDelegate: T) {
self.completionDelegate = completionDelegate
let _searchBar = UISearchBar()
super.init(searchBar: _searchBar, contentsController: completionDelegate)
}
}
I've also tried restricting inheritance on the protocol to only classes of type UIViewController, but that also does not work.
protocol SearchCompletionProtocol: class, UIViewController {
func searchSuccessful(results: [AnyObject])
func searchCancelled()
}
Of course I could easily just pass in two arguments to this method, one conforming to the search protocol and one being of type UIViewController, but that just seems not very Swifty.
SearchDelegate(completionDelegate: self, viewController: self)
– JoshA