Hi,
In VIPER pattern, we have the Presenter, and Router, etc...
I would like to have one object to conform to 2 PresenterProtocols where each PresenterProtocol defines a variable of type of RouterProtocol.
I have tried various forms but cannot get this to compile. Is this even possible?
import Foundation
// ==================================================
// A
protocol APresenterProtocol: AnyObject {
// strong
var router: ARouterProtocol { get }
}
protocol ARouterProtocol: AnyObject {
// weak
var parent: APresenterProtocol? { get set }
}
// ==================================================
// B
protocol BPresenterProtocol: AnyObject {
// strong
var router: BRouterProtocol { get }
}
protocol BRouterProtocol: AnyObject {
// weak
var parent: BPresenterProtocol? { get set }
}
// ==================================================
// Aliases and extra protocols I added to *glue* things without success
// Ideally I would like NOT to need to re-define things like these (They didn't work so far anyways)
typealias ABPresenterProtocolAlias = APresenterProtocol & BPresenterProtocol
typealias ABRouterProtocolAlias = ARouterProtocol & BRouterProtocol
protocol ABProtocol: APresenterProtocol, BPresenterProtocol {
var router: ABRouterProtocolAlias { get }
}
protocol ABRouterProtocol: ARouterProtocol, BRouterProtocol {
var parent: ABPresenterProtocolAlias? { get set }
}
// ==================================================
// I want to have:
// 1 presenter class that conforms to both A and B presenter protocols
// 1 router class that conforms to both A and B router protocols
// router instance will be used inside presenter
class RealABRouter: ABRouterProtocol {
var parent: ABPresenterProtocolAlias?
}
class RealABPresenter: ABProtocol {
var router: ABRouterProtocol
init(router: ABRouterProtocol) {
self.router = router
}
}
I tried to compile this but I cannot fix errors like below. I run out of ideas. If someone can help me it would be greatly appreciated.
Protocols.swift:44:7: error: type 'RealABRouter' does not conform to protocol 'ARouterProtocol'
class RealABRouter: ABRouterProtocol {
^
Protocols.swift:45:9: note: candidate has non-matching type 'ABPresenterProtocolAlias?' (aka 'Optional<APresenterProtocol & BPresenterProtocol>')
var parent: ABPresenterProtocolAlias?
^
Protocols.swift:44:7: error: type 'RealABRouter' does not conform to protocol 'BRouterProtocol'
class RealABRouter: ABRouterProtocol {
^
Protocols.swift:45:9: note: candidate has non-matching type 'ABPresenterProtocolAlias?' (aka 'Optional<APresenterProtocol & BPresenterProtocol>')
var parent: ABPresenterProtocolAlias?
^
Protocols.swift:11:9: note: protocol requires property 'parent' with type 'APresenterProtocol?'; do you want to add a stub?
var parent: APresenterProtocol? { get set }
^
Protocols.swift:48:7: error: type 'RealABPresenter' does not conform to protocol 'ABProtocol'
class RealABPresenter: ABProtocol {
^
Protocols.swift:49:9: note: candidate has non-matching type 'ABRouterProtocol'
var router: ABRouterProtocol
^
Protocols.swift:48:7: error: type 'RealABPresenter' does not conform to protocol 'APresenterProtocol'
class RealABPresenter: ABProtocol {
^
Protocols.swift:49:9: note: candidate has non-matching type 'ABRouterProtocol'
var router: ABRouterProtocol
^
Protocols.swift:48:7: error: type 'RealABPresenter' does not conform to protocol 'BPresenterProtocol'
class RealABPresenter: ABProtocol {
^
Protocols.swift:49:9: note: candidate has non-matching type 'ABRouterProtocol'
var router: ABRouterProtocol
^
Protocols.swift:32:9: note: protocol requires property 'router' with type 'ABRouterProtocolAlias' (aka 'ARouterProtocol & BRouterProtocol'); do you want to add a stub?
var router: ABRouterProtocolAlias { get }
^
Protocols.swift:60:8: error: value of type 'RealABRouter' has no member 'presenter'
router.presenter = presenter
~~~~~~ ^~~~~~~~~
Thanks in advance
Nacho