MVVM architectural pattern on iOS using Swift

Łukasz Osiennik
4 min readDec 11, 2018

--

What MVVM is in general?

MVVM (Model View ViewModel) is one of the high-level architectural design patterns used to separate layers in programming code.

It’s been invented by Microsoft architects Ken Cooper and Ted Peters to simplify event-driven programming used in UIs and it’s part of Microsoft’s WPF and Silverlight subsystems.

Apart from MVVM pattern, there are commonly known 2 more, those are MVC (Model View Controller) and MVP (Model View Presenter).

In this article I’m going to describe MVVM only. I’ll explain here all required details related with this pattern. And also I’ll describe here why it’s worth to use it in your iOS app and how to use it basing on real-life example.

Pleasant reading!

What MVVM is with more details?

MVVM separates code into 3 high-level layers. Looking from the lowest to the highest layer they are as follows:

  1. The lowest Model layer which is responsible for handling app’s domain models which are ready to be delivered directly to higher ViewModel layer. Apart from domain models definitions this layer provides services to store, update, delete and fetch data used in higher layer.
  2. The middle ViewModel layer which is responsible for handling app’s business logic using data provided by lower Model layer and reacting with logic processing on users interactions coming from higher View layer.
  3. The highest View layer which is responsible for presenting proper app’s UI basing on ViewModel’s business logic and processed there Model’s data but also is responsible for users’ interactions capturing and passing them to be processed by lower ViewModel layer.

In iOS code, when the base classes for each larger components are UIViewController and UIView, those classes act as View layer.

Below you can find UML component diagram presenting all MVVM layers with mutual dependencies.

UML component diagram with all MVVM layers with mutual dependencies

Why to use MVVM?

The answer is simple, to separate 3 base layers responsible for models, business logic and views which are always part of each iOS app. And also to make them independent from each other and not tightly coupled but complementing mutually. Having them prepared using MVVM pattern make them also reusable in more than only one place and testable independently what affects on better code stability and quality. Thanks to that all the code looks clean, readable and pleasant, that’s why it’s very easy to find exactly what you need not concentrating on the all not important details. And what’s the most important to software architects and software engineers the code is well-organised and this is what we all should always aspire!

Let’s implement MVVM on iOS using Swift

Let’s assume we would like to implement very simple app where the users will be able to search for people’s personal public data basing on 2 different search engines, let’s assume it will be Facebook and LinkedIn. From UI perspective those users will be able to select one of those mentioned engines, to put some text to search and to click search button which results with returning search results to UI.

Below is an exemplary iOS Swift code prototype presenting usage of MVVM pattern basing on the above description.

Model components

ViewModel components

View components

Below you can find UML class diagram presenting all MVVM components with mutual dependencies which code has been shown above.

UML class diagram with all MVVM components with mutual dependencies

Summary & Conclusions

As you can see in the all above descriptions but mainly in presented code and UML diagrams, MVVM architectural design pattern seems to be very useful when you want to have your iOS Swift code layered, especially on a level described in a section titled “Why to use MVVM?”. That’s why if you care about the requirements mentioned there and you want your code to fulfill them all you should definitely consider to use this approach in your apps.

I’d like to add and highlight only that this is not the only way to achieve those goals, there are more. That’s why I recommend you to familiarize with the rest of the existing patterns (at least MVC, MVP but try also VIPER as a very popular pattern used in iOS apps) and select what is the most suitable for you and your purposes. You should also try to experiment with existing approaches and modify them. Maybe you will find something new worth attention and use. Or maybe you will find something non-existent so far, being generic and reusable enough, what will not be only perfect choice for yourself but also for thousands of us, software architects and software engineers.

Good luck and successful searches!

--

--