Asis
Asis

Reputation: 733

Protocol Oriented Programming in swift iOS

Here is the 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 will have all the routing functions. HomeWireFrameProtocol will extend and have some home relating routing only. The test class inherits from HomeInteractorProtocol, which has a var wireFrame: HomeWireFrameProtocol, again HomeWireFrameProtocol is extending WireFrameProtocol.

Is var wireFrame: HomeWireFrameProtocol also represent var wireFrame: WireFrameProtocol?

Upvotes: -1

Views: 556

Answers (2)

Tapas Pal
Tapas Pal

Reputation: 7207

If I understand your question correctly then you have just encountered a traditional Dimond Problem where it is ambiguous as to which parent class a particular feature is inherited from.

Your view and wireFrame both the variable declared in HomeViewPresenterProtocol and HomeViewInteractorOutputProtocol. So when you confirm these two protocols in HomeViewPresenter the Dimond problem arise. Compiler is confusing to this ambiguous parent.

The easiest solution is to change the variable name as you can't have the same variable or function signature.

Upvotes: 0

Asis
Asis

Reputation: 733

Ok I realise it now, and fixed my own problem. What I did was

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

The variable wireFrame: WireFrameProtocol can also hold the reference of HomeWireFrameProtocol.

so in Test class I updated:

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{
    
}

Upvotes: 1

Related Questions