I'm trying to do this but I'm getting some troubles
This is CustomProtocol
protocol CustomProtocol {
}
SubCustomProtocol
protocol SubCustomProtocol: CustomProtocol {
}
SubCustomProtocolImplementation
class SubCustomProtocolImplementation: SubCustomProtocol {
}
This is CustomClass
class CustomClass<P: CustomProtocol> {
var customProtocol: P?
func doSomething() {
}
}
SubCustomClass
class SubCustomClass<P: SubCustomProtocol>: CustomSubClass {
}
And my BaseViewController
class BaseViewController<P: CustomProtocol, T: CustomClass<P>>: UIViewController {
var foo: T!
override func viewDidLoad() {
super.viewDidLoad()
foo?.doSomething()
}
}
My ViewController
class ViewController<P: SubCustomProtocolImplementation, T: SubCustomClass<P>>: BaseViewController<P,T> {
override func viewDidLoad() {
super.viewDidLoad()
}
}
In the line where I call foo?.doSomething() it says that 'T' is not a subtype of 'CustomClass<'P'>' and I don't know what I'm doing wrong
And in the ViewController declaration it says that "BaseViewController requires that T inherit from CustomClass<'P'>"
Hope you can help me!
If you want to specify your foo var type as CustomClass<P>
you should do as following instead.
class ViewController<P: CustomProtocol>: UIViewController {
var foo: CustomClass<P>?
override func viewDidLoad() {
super.viewDidLoad()
foo?.doSomething()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With