我正在努力做到这一点,但我遇到了一些麻烦
这是CustomProtocol
protocol CustomProtocol {
}
SubCustomProtocol
protocol SubCustomProtocol: CustomProtocol {
}
SubCustomProtocolImplementation
class SubCustomProtocolImplementation: SubCustomProtocol {
}
这是CustomClass
class CustomClass<P: CustomProtocol> {
var customProtocol: P?
func doSomething() {
}
}
SubCustomClass
class SubCustomClass<P: SubCustomProtocol>: CustomSubClass {
}
我的BaseViewController
class BaseViewController<P: CustomProtocol, T: CustomClass<P>>: UIViewController {
var foo: T!
override func viewDidLoad() {
super.viewDidLoad()
foo?.doSomething()
}
}
我的ViewController
class ViewController<P: SubCustomProtocolImplementation, T: SubCustomClass<P>>: BaseViewController<P,T> {
override func viewDidLoad() {
super.viewDidLoad()
}
}
在我调用foo?.doSomething()的行中,它表示'T'不是'CustomClass&lt;'P'&gt;'的子类型我不知道我做错了什么
在ViewController声明中,它表示“BaseViewController需要 T继承自CustomClass&lt;'P'&gt;“
希望你能帮助我!
答案 0 :(得分:2)
如果要将foo var类型指定为CustomClass<P>
,则应执行以下操作。
class ViewController<P: CustomProtocol>: UIViewController {
var foo: CustomClass<P>?
override func viewDidLoad() {
super.viewDidLoad()
foo?.doSomething()
}
}