-1

这是Protocols

protocol WireFrameProtocol{
    // router for all normal cases
    // like showing login page
    
}

protocol InteractorProtocol{
    var wireFrame: WireFrameProtocol? { get set }
}

protocol HomeWireFrameProtocol: WireFrameProtocol{
    // home specific routers
}

protocol HomeInteractorProtocol: InteractorProtocol{
    var wireFrame: HomeWireFrameProtocol? { get set }
}

class Test: HomeInteractorProtocol{
    var wireFrame: HomeWireFrameProtocol?
}

extension Test: InteractorProtocol{
    
}

WireFrameProtocol将具有所有路由功能。HomeWireFrameProtocol将扩展并仅具有一些与家庭相关的路由。测试类继承自HomeInteractorProtocol,它有一个 var wireFrame: HomeWireFrameProtocol,再次HomeWireFrameProtocol扩展WireFrameProtocol

是否var wireFrame: HomeWireFrameProtocol也代表var wireFrame: WireFrameProtocol

4

2 回答 2

1

好的,我现在意识到了,并解决了我自己的问题。我所做的是

protocol HomeInteractorProtocol: InteractorProtocol{
    // do not create another variable to store HomeWireFrame
    // var wireFrame: HomeWireFrameProtocol? { get set }
}

该变量wireFrame: WireFrameProtocol还可以保存 的引用HomeWireFrameProtocol

所以在测试课我更新了:

class Test: HomeInteractorProtocol{
    // can use all features from the WireFrameProtocol
    var wireFrame: WireFrameProtocol?

    // also can use all the feature from HomeWireFrame
    // this is kind of what I want to achieve without creating two different variables in the protocols
    var homeWireFrame: HomeWireFrameProtocol? {
         return wireFrame as? HomeWireFrameProtocol
    }
}

extension Test: InteractorProtocol{
    
}
于 2020-06-29T06:59:36.307 回答
0

如果我正确理解了您的问题,那么您刚刚遇到了一个传统的问题Dimond Problem,即某个特定功能是从哪个父类继承而来的。

view和两个在和wireFrame中声明的变量。所以当你确认这两个协议中的Dimond问题就出现了。编译器对这个模棱两可的父母感到困惑。HomeViewPresenterProtocolHomeViewInteractorOutputProtocolHomeViewPresenter

最简单的解决方案是更改变量名称,因为您不能拥有相同的变量或函数签名。

于 2020-06-29T05:52:59.647 回答