VIPER Architecture Swift iOS

What is VIPER

Entity.It is similar to the model layer in the other architectures.

View. it sends actions of user to the presenter and display data accordingly.

Presenter. Its gets the data from the interactor , send that model data to the View which displays the data accordingly.

Interactor. it contains the main business logic required. The interactor fetches data from the entity or model. We have used single responsibilities principle to segregate buisness logic from UI layer.

Router. It primary function is to navigate between different view controllers.

IMPLEMENTATION

Here i have tried to implement a basic use case for Order Screen. I have tried to add label and pass text to that label using VIPER architecture in swift

// Entity

struct OrderEntity {

var title: String

}

//View

protocol OrderViewing: class {

func updateTitle(orderEntity: OrderEntity)

}

class OrderViewController: UIViewController {

@IBOutlet weak var orderTitleLabel: UILabel!

var presentor: OrderPresenting!

override func viewDidLoad() {

presentor.onViewDidLoad()

}

}

extension OrderViewController: OrderViewing {

func updateTitle(orderEntity: OrderEntity) {

orderTitleLabel.text = orderEntity.title

}

}

//Presenter

protocol OrderPresenting {

func onViewDidLoad()

}

class OrderPresenter {

weak var view: OrderViewing?

var interactor: OrderInteracting?

var router: OrderRouting?

init(view:OrderViewing, interactor: OrderInteracting, router: OrderRouting) {

self.view = view

self.interactor = interactor

self.router = router

}

}

extension OrderPresenter: OrderPresenting {

func onViewDidLoad() {

guard let orderEntity = self.interactor?.getEntityData() else {return}

DispatchQueue.main.async {

self.view?.updateTitle(orderEntity: orderEntity)

}

}

}

//Interactor

protocol OrderInteracting {

func getEntityData() -> OrderEntity

}

class OrderInteractor {

}

extension OrderInteractor: OrderInteracting {

func getEntityData() -> OrderEntity {

let orderEntity = OrderEntity(title: “Order”)

return orderEntity

}

}

// Router

protocol OrderRouting {

}

class OrderRouter {

var view: UIViewController?

init(view: UIViewController) {

self.view = view

}

}

extension OrderRouter: OrderRouting {

}

//Builder

class OrderBuilder {

static func buildOrderModule() -> UIViewController {

let view = OrderViewController()

let interactor = OrderInteractor()

let router = OrderRouter(view: view)

let presenter = OrderPresenter(view: view, interactor: interactor,router: router)

view.presenter = presenter

return view

}

}

please feel free to drop off any suggestions and improvements.

please find me below on LinkedIn and GitHub

https://github.com/raghav1786

--

--

--

Software engineer - iOS @ Y Media Labs

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

[Mobile] Solutions for various error situations in YouTube Live Streaming.

SwiftUI: Custom navigation view for your applications

How to Make UI Tests in iOS Less Annoying

Programmatic Navigation using SwiftUI

Abstract Model in Swift

Dynamic Show Pages on iOS

How to: Build A Native Widget for iOS 14 with SwiftUI

SwiftUI state and data management types

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
raghav sharma

raghav sharma

Software engineer - iOS @ Y Media Labs

More from Medium

iOS UI Testing Design Pattern: Introducing Page Object Pattern

Copiable in Swift — Clean Approach

Abstract vector created by vectorjuice — www.freepik.com

Basics of Reactive Programming, ReactiveX, RxSwift

SOLID-The open-close principle in Swift