VIP Architecture in Swift

Mahdi Saedi
3 min readMay 18, 2023

--

VIP Architecture Cycle

What is VIP Architecture?

VIP (View, Interactor, Presenter) is a clean architecture pattern that emphasizes the separation of concerns in your iOS application. It provides a clear and organized structure, making maintaining and testing code easier.

In this article, we’ll explore the VIP architecture in Swift and demonstrate how to implement it using a simple example. By the end of this tutorial, you’ll better understand the VIP pattern and how it can help you create more robust and modular iOS apps.

Understanding the VIP Components

The View is for displaying Data on the user Screen and capturing user interactions. It consists of Views and ViewControllers.

  1. View: This Component is for displaying data on the screen and capturing user interactions. It consists of Views and ViewControllers.
  2. Interactor: The Interactor is responsible for handling business logic and data manipulation. It communicates with the Presenter to update the View with new data.
  3. Presenter: The Presenter serves as a bridge between the View and the Interactor.
  4. Router: The Router is used for navigation logic.
  5. Worker: The Worker is used for requests to API or Database to fetch data.
  6. Configurator: The Configurator configures all VIP Cycles cleanly and quickly.

Config VIP Cycle in the project

View:

Interactor:

Presenter:

Router:

Worker:

Configurator:

Where should we configure a View?

SceneFactory is for Creating and configuring the ViewController and other Components needed for this Scene.

Example of a simple VIP Cycle in application:

Application Design Example

Story:

In this application, we should fill Title, Description, Pay Type, Vehicle Type, Food Type, Button Title, and set Tour Image.
All these data fetch from API or Database.

Explain Cycle:

ViewController function calls interactor to fetch data from API/Database.
The interactor brings data from API/Database and sends it as a model to Presenter.
The presenter calls ViewController to Display data.
At this time ViewController Display all data.

What does the button do when it presses?

Send the ID of the tour to the next ViewController to reserve it.
You should know that ViewController cant store any variables in self.
At this time ViewController Call Interactor to get the Current Tour ID.
interactor sends the current tour ID to the Presenter, and Presenter calls ViewController To Routing View
ViewController Call Router to Navigate with Tour ID.

Now you know that ViewController can’t store any Variables or object

What do TableView and CollectionView Do in this way?

When we use TableView, and we want to get the Number of rows or Number of sections and Cell Data, we should store Object in Interactor ( create variables in DataStore Protocol in Interactor ) and use it in ViewController.

Example:

Interactor:

protocol CleanDataStore {
func TourInfo(at indexPath: IndexPath) -> Tour
var ToursCount: Int { get }
}

ViewController:

extension CleanViewController: UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
dataStore?.ToursCount ?? 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let data =
guard let tour = dataStore?.TourInfo(at: indexPath) else {
fatalError("Data Store in Nil")
}
cell.textLabel?.text = tour.name
return cell
}

}

--

--