Class 不符合带有协议扩展的协议
Class does not conform to protocol with protocol extension
我确实遇到了与 类似的问题,但那里的解决方案对我不起作用,我不明白为什么。
我想创建一个带有扩展的协议来处理我的应用程序的登录行为。
首先,我不确定是否应该将带有扩展名的协议放在一个单独的文件中,或者只放在 class 声明上方的 AppDelegate 中。这两种方式都行不通。 AppDelegate 知道协议,但不知道扩展名,所以它要我在 class 中实现 handleLogin()
。
协议
protocol LoginFlowHandler {
func handleLogin()
}
协议扩展
public extension LoginFlowHandler {
func handleLogin(withWindow window: UIWindow?) {
// Just for testing purpose
let userIsLoggedIn = false
if userIsLoggedIn == true {
self.showMainApp(withWindow: window)
} else {
self.showLogin(withWindow: window)
}
}
func showLogin(withWindow window: UIWindow?) {
window?.subviews.forEach { [=12=].removeFromSuperview() }
window?.rootViewController = nil
window?.rootViewController = UIStoryboard(name: "Login", bundle: nil).instantiateInitialViewController()
window?.makeKeyAndVisible()
}
func showMainApp(withWindow window: UIWindow?) {
window?.rootViewController = nil
window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
window?.makeKeyAndVisible()
}
}
AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate, LoginFlowHandler {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow.init(frame: UIScreen.main.bounds)
handleLogin(withWindow: window)
return true
}
}
提前致谢!
it wants me to implement handleLogin() in the class.
是的,因为那是 你 告诉它要做的。这就是你说
时的意思
protocol LoginFlowHandler {
func handleLogin()
}
然后声明您的 class 为 LoginFlowHandler 的采纳者:
class AppDelegate: UIResponder, UIApplicationDelegate, LoginFlowHandler {
那 意味着 这个 class 必须有 handleLogin()
的实现。它没有。
如果你只是从协议中删除要求,你的代码对我来说编译得很好:
protocol LoginFlowHandler {
//func handleLogin()
}
不清楚您认为您在满足该要求方面取得了什么成果,因此删除它并没有什么坏处。
或者,如果你出于某种原因坚持要求,那么这样写:
protocol LoginFlowHandler {
func handleLogin(withWindow:UIWindow?)
}
这样,你写的协议扩展就满足了需求。
这是因为你的协议没有参数,而它的实现有。
协议:
handleLogin()
实施:
handleLogin(withWindow window: UIWindow?)
如果您从扩展中删除参数或在协议中添加参数,它将起作用。
我确实遇到了与
我想创建一个带有扩展的协议来处理我的应用程序的登录行为。
首先,我不确定是否应该将带有扩展名的协议放在一个单独的文件中,或者只放在 class 声明上方的 AppDelegate 中。这两种方式都行不通。 AppDelegate 知道协议,但不知道扩展名,所以它要我在 class 中实现 handleLogin()
。
协议
protocol LoginFlowHandler {
func handleLogin()
}
协议扩展
public extension LoginFlowHandler {
func handleLogin(withWindow window: UIWindow?) {
// Just for testing purpose
let userIsLoggedIn = false
if userIsLoggedIn == true {
self.showMainApp(withWindow: window)
} else {
self.showLogin(withWindow: window)
}
}
func showLogin(withWindow window: UIWindow?) {
window?.subviews.forEach { [=12=].removeFromSuperview() }
window?.rootViewController = nil
window?.rootViewController = UIStoryboard(name: "Login", bundle: nil).instantiateInitialViewController()
window?.makeKeyAndVisible()
}
func showMainApp(withWindow window: UIWindow?) {
window?.rootViewController = nil
window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
window?.makeKeyAndVisible()
}
}
AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate, LoginFlowHandler {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow.init(frame: UIScreen.main.bounds)
handleLogin(withWindow: window)
return true
}
}
提前致谢!
it wants me to implement handleLogin() in the class.
是的,因为那是 你 告诉它要做的。这就是你说
时的意思protocol LoginFlowHandler {
func handleLogin()
}
然后声明您的 class 为 LoginFlowHandler 的采纳者:
class AppDelegate: UIResponder, UIApplicationDelegate, LoginFlowHandler {
那 意味着 这个 class 必须有 handleLogin()
的实现。它没有。
如果你只是从协议中删除要求,你的代码对我来说编译得很好:
protocol LoginFlowHandler {
//func handleLogin()
}
不清楚您认为您在满足该要求方面取得了什么成果,因此删除它并没有什么坏处。
或者,如果你出于某种原因坚持要求,那么这样写:
protocol LoginFlowHandler {
func handleLogin(withWindow:UIWindow?)
}
这样,你写的协议扩展就满足了需求。
这是因为你的协议没有参数,而它的实现有。
协议:
handleLogin()
实施:
handleLogin(withWindow window: UIWindow?)
如果您从扩展中删除参数或在协议中添加参数,它将起作用。