import UIKit
protocol A {
func f()
}
protocol B {
var v: A? { get set }
}
class C: B { // Type 'C' does not conform to protocol 'B'
var v: UIViewController? = nil
init(v: UIViewController?) {
self.v = v
}
}
// Option A:
extension UIViewController: A {
func f() {
print("lol")
}
}
// Option B:
extension A where Self: UIViewController {
func f() {
print("lol")
}
}
So basically the compiler should have all the info it needs to assert that UIViewController
indeed conforms to A
and thus is safe to be put as the type of v
in class C
... But for some reason it just doesn't.
And I know using an associated type works here, but I really can't since it renders my protocol completely unusable for my use case (because of "can only be used as a generic constraint").
So is there anything I can do to fix this or do I have to resort to making v
's type AnyObject
and casting it like I would in Obj-C?
Thanks!