Implement a VIPER Architecture in Swift 5
Leverage the Single Responsibility Principle
In this tutorial we will learn how to implement a VIPER architectural pattern in Swift 5.
I have created a demo app that uses The Simpsons Quote API to fetch quotes, characters, and image URLs from “The Simpsons.”
The actual images are obtained using Kingfisher.
For code generation, I’ve created my own VIPER Xcode template.
The source code of the project is available here.
NOTE: The Simpsons Quote API is published on a free platform, so it’s possible that you may get a 400 error when using network requests too often. Try not to spam the requests, otherwise you will have to wait for some time until the API becomes available again:

Simpsons Quotes


Project Structure

The root of the project is divided into three folders: Classes, Resources, and Supporting Files. The Resources folder contains Assets.xcassets, and the Supporting Files folder contains LaunchScreen.storyboard and Info.plist.
As you can see, in Classes, we have the following folders:
- App contains
AppDelegate.swift - Modules has two defined modules: Quotes and QuoteDetail
- Common contains the REST API endpoint and Entities [aka models in MV(X)]
- Managers includes classes that facilitate our work with the REST API
- Services contains QuoteService (retrieves quote entities from the REST API), ImageDataService (converts data into UIImages), and KingfisherService (obtains image data using the image URL from the quote entity)
Let’s Define a VIPER Module
A typical VIPER Module consists of five components: View, Interactor, Presenter, Entity, and Router. Here is a short description of what each component does:
Viewis passive and doesn't do much on its own. Its sole responsibility is to message events to the presenter and display UI elements.Interactoris a UIKit-independent component that performs all business logic. For example, in our app, it uses Services to communicate with the REST API in order to obtain image data from an URL.Presenteris also UIKit-independent. It receives messages from the view and decides whether to send messages to the interactor or the router. It also receives the data from the interactor and prepares it for the view to display in the suitable format.Entityis a plain model that’s used by the interactorRouter, in our app, is responsible for creating a particular module and navigating from one module to another
To Our App: Quote Module
First, we should define the contract (five protocols in our case) according to which VIPER components will interact with each other.




