I am using VIPER architecture with Swift.
I want to use protocol for presenter to get UITableView delegate, because I do not want to repeat the declarations of those methods for all presenters.
So I have created protocol
protocol TableViewPresenterProtocol: class {
associatedtype Data
func numberOfSections()-> Int
func haeaderTitle(secton: Int)-> String?
func numberOfRows(section: Int)-> Int
func itemForCell(at indexPath: IndexPath)-> Data
func didSelect(at indexPath: IndexPath)
}
//for make optional
extension TableViewPresenterProtocol {
func numberOfSections()-> Int {
return 1
}
func haeaderTitle(secton: Int)-> String? {
return nil
}
}
class PaymentViewController {
var presenter: (PaymentPresenterProtocol & TableViewPresenterProtocol)?
}
but XCode shows error
Protocol 'TableViewPresenterProtocol' can only be used as a generic constraint because it has Self or associated type requirements
Where in the source code do I fix that?
In your
PaymentViewControllercurrently the compiler has no chance to identify what yourassociatedtypeDatarefers to. It can be aString, it can be anIntor anything else. Swift does not like that. It can't work with that because it is ambiguous what theassociatedtypereally refers to.In order to solve the issue, we can use
TableViewPresenterProtocolas a generic constraint to tell Swift actually what type we are referring to. In your case it looks like this:And let's say you create a class, which conforms to
TableViewPresenterProtocol:And now you can say:
And it will be clear to Swift that the presenter's
associatedtypeDatais aString.