• Home
  • About
  • Our Apps
  • Learn
  • Contact
Login

Register

Login
Seemu Apps Seemu Apps
  • Home
  • About
  • Our Apps
  • Learn
  • Contact

Swift – Model View View Model Explained

Home business Swift – Model View View Model Explained

Swift – Model View View Model Explained

Mar 31, 2017 | Posted by Andrew | business, ios, swift, xcode |

In the last tutorial we covered the Model View Controller (MVC) design pattern in swift. Today we are going to look at the Model View View Model (MVVM), and how it works and why you would use it over MVC.

What is MVVM?

MVVM stands for Model-ViewModel-Model. The following diagram outlines how it works at a high level.

  • The View is the actual view shown to the user. It contains UI elements and the like, but it does not have any information as to the values these views. It owns the View Model.
  • The View Model contains the values of the elements in your view. It has no idea of the existence of the view. It owns the Model. Think of it as an interface update information to the view, it gets relevant information from the model, might manipulate it then finally the view will be updated.
  • The Model is where your data lies. Things such as objects, network code, parsing code and so on reside here. The controller (View) has no connection to the Model.

So now we know what each is right?

Lets imagine you have a Dog class, it will create a Dog for you with 4 legs and will bark. This Dog is in the Model layer. You would then initialize this object with your View Model. Now imagine we have a View – this view needs to show information about different types of animals to the vet.

When you load the animal screen and it displays a animal the View goes to the View Model. The view model will then go to the Dog object in the Model and do any setup necessary. The view model will then return to the View anything it needs to know about the Dog, or any other animal. It could even be a Cat. Let’s take a look at how this would be used in code:

How MVVM is used

We have setup an app to show you this in action. We have a storyboard that shows the user the animal name and how many legs the animal has, it is a part of the View.

Connect the “Pet Name”, “0” and “Name Legs Description” label as outlets to your view controller as follows.

    @IBOutlet var petName: UILabel!
    @IBOutlet var petLegs: UILabel!
    @IBOutlet var petDesc: UILabel!

Now Go File–>New–>File and create two new Swift files names

Dog.swift
DogViewModel.swift

The Dog.swift is the Model,  it’s code is:

// Model
class Dog {
    var name: String
    var legs: Int
    
    init(dogname: String) {
        name = dogname
        legs = 4
    }
}

The DogViewModel.swift is the View Model, it’s code is:

// View Model
class DogViewModel {
    private var myDog: Dog
    
    init(name: String) {
        self.myDog = Dog(dogname: name)
    }
    
    var dogName: String {
        return myDog.name
    }
    
    var dogLegs: String {
        return "\(myDog.legs)"
    }
    
    var dogNameAndLegs: String {
        return "The Dog \(myDog.name) has \(myDog.legs) legs"
    }
    
}

And in the ViewController.swift which is the view, add the following code to viewDidLoad(). This will set the View based of the values in the View Model.

        let model = DogViewModel(name: "Fido")
        
        petName.text = model.dogLegs
        petLegs.text = model.dogLegs
        petDesc.text = model.dogNameAndLegs

Now you know what MVVM is and how to use it, you can begin to implement it in your app. When used properly it will reduce the amount of code that lives in your View Controller’s. As a result of this you can create unit test’s that are easy to run as they can be based on the View Model which has no View/User Interface Logic contained in it.

download source code

Tags: controllermodel viewmvcmvvc
1
Share

About Andrew

Andrew is a 24 year old from Sydney. He loves developing iOS apps and has done so for two years.

You also might be interested in

Swift – Model View Controller (MVC) for beginners

Mar 30, 2017

MVC - sometimes also known as "Massive View Controller" is[...]

Welcome

Hi I am Andrew and welcome to Seemu Apps! Have a look around, we provide tutorials for primarily iOS apps.
Bluehost website hosting discount

Seemu’s Studio Setup

Blue Yeti Microphone
Rode Stand
Spider Shock Mount
Mac Keyboard Cover
Screenflow - recording software

Contact Us

We're currently offline. Send us an email and we'll get back to you, asap.

Send Message

Footer

:)

© 2025 · Your Website. Theme by HB-Themes.

  • Home
  • About
  • Our Apps
  • Learn
  • Contact
Prev Next