我声明我的协议如下:
protocol SomeProto where Self: UIViewController {}
class MyVC: UIViewController, SomeProto {} // ok
但是当我尝试在一种方法中使用它时,出现错误:
func someMethod(vc: SomeProto) {
let rootVC = UIApplication.shared.keyWindow?.rootViewController
rootVC?.present(vc, animated: true, completion: nil)
// Cannot convert value of type 'SomeProto' to expected argument type 'UIViewController' Insert ' as! UIViewController'
}
无法将类型“ SomeProto”的值转换为预期的参数类型“ UIViewController”。 UIViewController'
Swift知道SomeProto
必须是UIViewController
类型,所以为什么我仍然需要显式地将其转换为讨厌的as!
?