I am defining a protocol that has certain functions and variables
protocol BaseListPresenter {
associatedtype T
var values: [T] {get}
}
extension BaseListPresenter {
public func count() -> Int {
return values.count
}
public func valueAtIndex(index: Int) -> T? {
return values[index]
}
}
Now, I want to use this in a class:
class X: UIViewController {
var listPresenter: BaseListPresenter? // HERE it gives me error
// some other functions and variables here
}
Got Error in above saying: Protocol 'BaseListPresenter' can only be used as a generic constraint because it has Self or associated type requirements
Now, I define sub-class of X:
class Y: X {
func setPresenter() {
self.listPresenter = UsersPresenter() // UsersPresenter confirms BaseListPresenter protocol with value's type "User"
}
// many more functions below
}
class Z: X {
func setPresenter() {
self.listPresenter = RoomsPresenter() // RoomsPresenter confirms BaseListPresenter protocol with value's type "Room"
}
// many more functions below
}
I have achieved solution from (Protocol can only be used as a generic constraint because it has Self or associatedType requirements) already by creating UsersPresenter and RoomsPresenter. However, I want to create BaseListPresenter type variable which will take different types of value in X (a UIViewController); once Room and next time User depending on subclass of X.
How can I solve this issue and use as I want?