Learning Swift Design Pattern-MVC.

Sridhar Muthineni
4 min readSep 26, 2024

During programming we encounter various types of problems. Some of these problems are repetitive and we encounter them in every project. Imagine if a bunch of different developers faced a similar problem in their projects and they wrote a bunch of different solutions. All of these solutions are correct and work fine. But among these solutions some can be suboptimal, some might break in a different situation, and some might be the ones which are the best and will work fine in all circumstances.

For such common problems, the industry has developed standard solutions which have proven the test of time and work great. These types of problems and their solutions are commonly known as design patterns.

In this article, we are going to discuss most common and the most loved Design Pattern - MVC.

MVC (Model View Controller) Pattern

The MVC pattern is the most fundamental design pattern in iOS, being baked directly into UIKit. It is named after the main components used in this pattern.

Model: Holds the data and business logic.

View: The UI layer responsible for displaying data and receiving user input.

Controller: This layer acts as a glue between model and view. It feeds the data from the model to the view. It also takes input from view and handover the data back to the model so that model can process it according to the business logic.

Pros

● MVC clearly separates the app’s data (Model), UI (View), and business logic (Controller), making the structure of the code more organized and easier to understand.

● The separation between the View and the Controller allows Views to be reused across multiple parts of the application without affecting the underlying business logic.

● MVC is the default pattern in UIKit, and many iOS frameworks are designed with MVC in mind.

● Since the Model is independent of the View and Controller, it’s easier to unit test. You can focus on business logic and data handling without involving the UI.

● MVC enables quick prototyping, as it’s relatively straightforward to implement for simple applications.

Cons

● In practice, view controllers in MVC often become too large and end up handling too much logic, which defeats the purpose of separation of concerns. This is called fat controller problem.

● The View and Controller in iOS are often tightly coupled, which makes it harder to reuse controllers with different views.

● Since controllers in iOS manage both business logic and UI updates, they become hard to unit test.

● MVC doesn’t handle complex business logic or multi-module applications well. Managing state, asynchronous operations, and transitions between different views can become cumbersome.

Example

Let’s see how MVC works by creating an example project. Create an iOS app with Swift/Storyboard.

Since UIKit works on MVC pattern out of the box, you will get a ViewController which is our controller object and a Main.storyboard file which is our view. For our model class we will introduce an object that holds the data and do the processing when required.

For the model, we will create a Person class that will hold a person’s name and email address and date of birth. Since business logic goes in model, we will add a computed property to calculate person’s age in the Person model object.

Let’s add some labels on our view to display persons information like so:

With model and view in place we will now work on our controller to present the data.

Modify the controller class like so:

In the controller class, we are doing the following things:

  1. We created outlet connections in order to access the view labels.
  2. In the viewDidLoad method, we first fetched the person data.
  3. fetchData method mimics an api call from which person data is fetched and returned.
  4. Next we populated the returned data.

Here is our final output:

If you want to refer the source code to advance your learning , please clone the example from https://github.com/muthineni/blogs/tree/main/DesignPatterns/MVCExample

Stay tuned for more insights in our next article.If you like the article please clap and follow me for the updated articles. thank you.

--

--