MVC vs MVVM in Swift

Yuvraj Kale
4 min readAug 15, 2020

In today’s programming world it’s important to write code with 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 having 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 table view and going to show the status of the todo task which is completed or not. For completed task we are showing in green color and for incompleted task 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 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 class where we showing the data.

In the above code snippet, you can see that it’s cell class which is responsible for showing the data over cell and because of that this class is said as 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 model and then the model is assigned to the view/cell in form of function so that data can be shown.

This 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 Model. View and controller perform different duties as compare 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 there 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 is 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 only difference is a load of view 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 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 viewmodel only returns the color, title and status type. It’s none of view’s duty to check for the status. It only show 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 what it was in MVC 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 relief 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 model hold all the information and logical operation in ViewModel.

Where is the controller?

In this case, we are unable to find out what is controller 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 following code snippets

In above code, you can’t see any instance of Model here. As in the MVVM view controller doesn’t communicate with model directly. Here you can see the to 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

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.

--

--