I have two protocols with extensions LoginPresenting
and LoginDismissing
. I want LoginPresenting
extension only to apply to UIViewControllers
that also implement LoginDismissing
. I am trying to do it like so but have so far been unable to work out the syntax.
protocol LoginDismissing : class {
func loginHasCompleted(withController: UIViewController)
}
extension LoginDismissing where Self:UIViewController {
func loginHasCompleted(withController controller:UIViewController) {
//...code ommited
}
}
protocol LoginPresenting : class {
func presentLogin()
}
The following is bad code, but I think it explains how I am attempting to make LoginPresenting only apply to UIViewControllers that also implement LoginDismissing.
extension LoginPresenting where Self:UIViewController, LoginDismissing //Syntax error here
func presentLogin() {
let lc = LoginViewController()
let nav = UINavigationController(rootViewController: lc)
nav.modalPresentationStyle = .fullScreen
lc.loginDismissingDelegate = self //type LoginDismissing
self.present(nav, animated: true, completion: nil)
}
}
You should enforce as many requirements as possible on the protocol itself, not the extensions.
protocol LoginPresenting: LoginDismissing & UIViewController {
func presentLogin()
}
Also, class
is old syntax, though not deprecated.
protocol LoginDismissing: AnyObject {
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments