VIPER Architecture Implementation in Swift
Architecting your iOS apps in a better, clean way it’s important not only for development but for maintenance too. VIPER is an acronym that stands for:
- View → responsible for views and user interactions
- Interactor → responsible for business logic like fetching data requested by the presenter
- Presenter → responsible for communication between layers View, Interactor & Router, Its backbone of architecture
- Entity → holds data model
- Router → responsible for navigation/routing of module
On this post, I have created an example application to demonstrate how to apply VIPER in your design.
Here is the folder structure for a demo app fetching posts and display them in a list in a table view with Swift 5:
A better way is to keep a list of protocols of a module in a separate file. As in demo project named PostProtocols.
Let’s start with Protocols file. Create PostProtocols file. Add all protocols required for communication between layers of architecture.
Create View Controller class that will be using Presenter and will conform to PresenterToViewProtocol
Create Presenter class that will conform to ViewToPresenterProtocol & InteractorToPresenterProtocol
Create Interactor class that will conform to PresenterToInteractorProtocol
Create Router class that will conform to PresenterToRouterProtocol
Data Model used for posts data which will be treated as Entity for post module
Here is how to initialize your module. Use static method CreateModule of RouterClass
Here you can download the sample project: https://github.com/farhanmirza27/VIPER
If need any help drop me a comment.