MVC Vs MVVM In Swift

In today’s programming world it’s important to write code in a standard way. For that, we are studying different types of architectural models. Out of that MVC and MVVM are mostly used design patterns and everybody has a good idea about MVC. But when it comes to MVVM developers are not able to differentiate it properly. So I am trying to explain it simply with the same example for both Architectures so that we can understand it properly.

Before understanding the MVC and MVVM I want to add an example which we are going to use.

We are going to show the todo list of users in the table view and going to show the status of the todo task which is completed or not. For completed tasks we are showing in green color and for not uncompleted tasks showing the yellow color.

 

This is how we are getting the response from API

{

"userId": 1,

"id": 1,

"title": "delectus aut autem",

"completed": false

}

What Is MVC?

Everybody knows MVC stands for Model-View-Controller. Let’s see in detail.

Model

Model is nothing but it holds all the properties of the particular object i.e it holds all the data of your app. The work of Model is only providing the data to the required fields not doing any type of operation on it.

By considering the above problem statement, the following is the model of our app.

View

This is the face of your app. Means this class does not contain domain-specific logic. In our case, Storyboard and cell is our view of the class where we are showing the data.

In the above code snippet, you can see that it’s a cell class which is responsible for showing the data over the cell, and because of that this class is said to be the view.

Controller

The controller is a way of doing communication between model and view. So basically the class which is responsible for doing the communication between View and model is called a controller.

In the above example, you are seeing the controller is responsible for fetching the todo list data and getting it in form of the model and then the model is assigned to the view/cell in form of a function so that data can be shown.

This is how MVC architecture looks like. Now we will move forward towards MVVM taking consideration the same example

What Is MVVM?

MVVM Stands for Model-View-ViewModel. Now similarities between MVC and MVVM are only models. View and controller perform different duties as compared to MVC. The model doesn’t have direct communication with View and Controller. What is the meaning of not direct communication with View and controller? It means View and Controller don’t have any instance of the model in their class.

Also In MVC, you can see there is a logical part of the cell. But in MVVM there is not any type of logical code as it only assigns data to the view. Let’s see in Detail:

Model

The model is the same as what we discussed in MVC. So I am not going to discuss it here again.

View

In MVVM View is also the face of your app, the only difference is a load of views is distributed and no logical part is included in View. It only gets data from the view model and shows the data. And the view doesn’t have direct communication with the model. So how does View get data? So the view is communicating with ViewModel for data. Let’s see the code snippets so you can get the exact idea about it.

Here you can see the function showData is commented and also there is no instance of todomodel1 in code instead you can see the todoViewModel. Also, the logical part where we are showing the status completion is also not there. So this is the major difference between MVC and MVVM View. Here you can see the ViewModel only returns the color, title, and status type. It’s none of the view’s duty to check for the status. It only shows what ViewModel sending.

ViewModel

This is the new thing added in MVVM instead of the Controller. It doesn’t mean the controller is not playing any role here. The controller is playing the same role that it was in MVC. The only difference I already told is it doesn’t communicate with the model directly instead it communicates with the View-model. View model performs all the logical work and relieves the controller and view doing this work. So that they can only focus on their major work of representation of data.

Let’s check what ViewModel is doing the job here.

You can see ViewModel getting an instance of model class and doing all logical work of checking status and assigning color. View models hold all the information and logical operation in ViewModel.

Where Is The Controller?

In this case, we are unable to find out what the controller is doing. So basically the controller is doing all the work which it was doing in MVC only difference is the controller doesn’t know about the model it holds the ViewModel only. Let’s check the following code snippets:

In the above code, you can’t see any instance of Model here. As in the MVVM, the view controller doesn’t communicate with the model directly. Here you can see the ViewModel instance which we get. And we are directly assigning it to Cell.

So in MVVM, the role of View and controller is only assigning the value not calculating the value.

Source Code

You can find code for MVVM and MVC by using the following link.

MVVM – https://github.com/iYuvraj-kale/MVC_to_MVVM.git
MVC – https://github.com/iYuvraj-kale/MVCDemo.git

coma

Conclusion

The difference between the MVC and MVVM is View and controller are responsible for calculating the value and assigning the value so the load is more on View and Controller where in MVVM View and Controller are only responsible for assigning the value not calculating the value.

Yuvraj

Tech Expert

Yuvraj is a mobile application developer with over 5+ years of experience with all stages of the development cycle for dynamic projects. Having in-depth knowledge including Swift, Objective C, Version Controlling, Firebase, Cocoatouch I strive continuously toward achieving new heights for creating new benchmarks always. He worked closely with the Hardware team while working on the BLE / IoT projects and Self-assured in – Efficient Time Management, Co-ordination, and Independent correspondence.

Get in touch for a detailed discussion.

Hear From Our 100+ Customers
coma

Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.

author
ADDIE WOOTTEN
CEO, SMILINGMIND
coma

We had very close go live timeline and MindBowser team got us live a month before.

author
Shaz Khan
CEO, BuyNow WorldWide
coma

They were a very responsive team! Extremely easy to communicate and work with!

author
Kristen M.
Founder & CEO, TotTech
coma

We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.

author
Chacko Thomas
Co-Founder, TEAM8s
coma

Mindbowser is one of the reasons that our app is successful. These guys have been a great team.

author
Dave Dubier
Founder & CEO, MangoMirror
coma

Mindbowser was very helpful with explaining the development process and started quickly on the project.

author
Hieu Le
Executive Director of Product Development, Innovation Lab
coma

The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.

author
Alex Gobel
Co-Founder, Vesica
coma

Mindbowser is professional, efficient and thorough. 

author
MacKenzie R
Consultant at XPRIZE
coma

Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.

author
Laurie Mastrogiani
Founder, S.T.A.R.S of Wellness
coma

MindBowser was great; they listened to us a lot and helped us hone in on the actual idea of the app.” “They had put together fantastic wireframes for us.

author
Bennet Gillogly
Co-Founder, Flat Earth
coma

They're very tech-savvy, yet humble.

author
Uma Nidmarty
CEO, GS Advisorate, Inc.
coma

Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.

author
Katie Taylor
Founder, Child Life On Call
coma

As a founder of a budding start-up, it has been a great experience working with Mindbower Inc under Ayush's leadership for our online digital platform design and development activity.

author
Radhika Kotwal
Founder of Courtyardly
coma

The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!

author
Michael Wright
Chief Executive Officer, SDOH2Health LLC
coma

They are focused, patient and; they are innovative. Please give them a shot if you are looking for someone to partner with, you can go along with Mindbowser.

author
David Cain
CEO, thirty2give
coma

We are a small non-profit on a budget and they were able to deliver their work at our prescribed budgets. Their team always met their objectives and I'm very happy with the end result. Thank you, Mindbowser team!!

author
Bart Mendel
Founder, Mindworks