Member-only story
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…