MVVM in iOS

Heshan yodagama
Nov 5 · 2 min read

Yeah I know that you are familiar with MVC. Here I am going to introduce better architecture for your next and existing iOS application .

MVC Architecture

As you already know, Models hold data, views present an interactive interface to the user, and view controllers mediate the interaction between the model and the view.

- Weaknesses of MVC

  1. It generates massive view controllers — hard to manage .
  2. hard to test( manually or unit testing) .
  3. No better place to do networking things .

Consider for a moment that, although views and view controllers are technically distinct components, they almost always go hand-in-hand together, paired.

As I mentioned earlier , usually view controller’s logic gets massive but a lot of it is what’s called ‘presentation logic,’ in MVVM terms — things like transforming values from the model into something the view can present, like taking an Date and turning it into a formatted String.

so we can place our presentation logic separately . We’re going to call this the ‘view model’ — it will sit between the view/controller and the model.

MVVM Architecture

+ Strength of MVVM

  1. compatible with your existing MVC projects .
  2. makes your apps more testable .
  3. works best with a binding mechanisms .

We could use KVO, like with MVC, but that can quickly become unmanageable. In practice, using ReactiveCocoa is a great way to glue all the moving pieces together.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade