Optimizing iOS Development: Crafting an Effective MVVM Folder Structure for SwiftUI

Mayank Choudhary
5 min readJul 23, 2023

Photo by Wes Hicks on Unsplash

Hey there! So, when it comes to building an iOS app, one thing that’s super important is having a solid app architecture. It’s like creating a strong foundation for your house — it makes everything else easier and more manageable. One of the popular architectures for iOS apps is called MVVM, which stands for Model-ViewModel-View.

Okay, let’s break it down! Imagine you’re building an app, and you want to keep things neat and tidy. The first folder you’d create is called “Features”
This is the main folder where each feature of your app resides. Each feature, like Onboarding, Home, or Posts, has its own folder.

Next inside each feature (if needed), we will create a folder “Feature/Model”. Here, you’ll put all your data-related stuff — like data structures, database interactions, and business logic. It’s like the brains of your app!

Next up, let’s talk about the “Feature/View” folder. Here, you’ll find all your user interface components — the screens, buttons, and everything that users interact with. The cool thing is that the View doesn’t know anything about the data itself; it just listens to the ViewModel and displays whatever it gets. It keeps things separate and clean.

“Feature/Modifiers” folder. This one is all about customizing and enhancing your SwiftUI views. You can create your own view modifiers and reuse them across your app. It’s a great way to keep your SwiftUI code clean and organized.

Now, we have the “Feature/ViewModel” folder. This is like the middleman between the Model and the View. The ViewModel handles all the data that your app needs to display, and it does all the data manipulation. It’s like a translator between your Model and what you see on the screen.

Alright, moving on to the “Services” folder. This is where you put all your network requests, database stuff, and anything that connects to the outside world. By having all these services in one place, it’s easier to manage and debug them.

Now, let’s talk about the “Utilities” folder. Here, you’ll find helpful functions or extensions that you can use throughout your app. Think of it as a toolbox filled with useful tools you can grab anytime you need them.

The MVVM architecture MVVM (Model-View-ViewModel) is not just about organizing your code into folders; it is a software architectural pattern that encompasses a lot more than just folder structure. MVVM is designed to improve code organization, maintainability, and separation of concerns in iOS apps.

|
|--- 📁 Features
| |
| |--- 📁 Onboarding
| | |--- 📁 Model
| | | |--- UserModel.swift
| | |--- 📁 View
| | | |--- OnboardingView.swift
| | | |--- 📁 Modifiers
| | | |--- CardModifier.swift
| | |--- 📁 ViewModel
| | |--- OnboardingViewModel.swift
| |--- 📁 Home
| | |--- 📁 Model
| | |--- 📁 View
| | | |--- ContentView.swift
| | | |--- DetailView.swift
| | | |--- 📁 Modifiers
| | | |--- CardModifier.swift
| | |--- 📁 ViewModel
| | |--- HomeViewModel.swift
| |--- 📁 Posts
| |--- 📁 Model
| |--- 📁 View
| | |--- ContentView.swift
| | |--- DetailView.swift
| | |--- 📁 Modifiers
| | |--- CardModifier.swift
| |--- 📁 ViewModel
| |--- PostsViewModel.swift
|
|--- 📁 Modifiers
| |
| |--- RootModifier.swift
|
|--- 📁 Services
| |
| |--- APIService.swift
| |--- DatabaseService.swift
| |--- NetworkManager.swift
|
|--- 📁 Utilities
|
|--- Extensions.swift
|--- Helpers.swift

Why MVVM?

1. Enhanced UI Testability: MVVM promotes the use of SwiftUI or UIKit Views that are purely focused on rendering the user interface and interacting with the ViewModel. These views are decoupled from the business logic, making them more testable in isolation. You can write UI tests specifically for these View components, ensuring that the correct UI elements are displayed and responding to user interactions as expected.

2. Test Coverage and Code Maintainability: By following MVVM’s separation of concerns, developers can achieve higher test coverage. Since each component has a clear responsibility, it becomes easier to write targeted tests for specific functionalities. Additionally, MVVM encourages modular code, which results in a more maintainable codebase, making it easier to add or update features without breaking existing functionality.

3. Testing Reactive Bindings: In MVVM, data binding is often facilitated by reactive programming frameworks like Combine or RxSwift. These frameworks enable reactive bindings between the ViewModel and the View, ensuring that UI elements update automatically when underlying data changes. By testing these reactive bindings, you can verify that the UI is updating correctly based on ViewModel changes.

4. Snapshot Testing for SwiftUI Views: MVVM’s emphasis on SwiftUI Views makes it suitable for leveraging snapshot testing techniques. With snapshot testing, you can capture the appearance of SwiftUI Views under different scenarios and compare them against reference images. This helps catch unexpected UI changes early and provides visual regression testing.

5. Parallel Testing and Faster Feedback Loop: Because MVVM separates the UI from the business logic, developers can perform parallel testing. Unit tests for the ViewModel and Model can run independently of UI tests, providing faster feedback during development and continuous integration processes. This results in quicker iterations and faster delivery of new features.

6. Easier Debugging: Due to its structured and modular nature, MVVM simplifies the debugging process. When issues arise during testing, it becomes easier to pinpoint the source of the problem, whether it’s in the ViewModel, Model, or UI components. This accelerates the debugging process and reduces downtime.

7. Testing Data Validation and Error Handling: The ViewModel is responsible for handling data validation and error handling before presenting data to the View. By writing comprehensive tests for the ViewModel, you can ensure that it handles various scenarios correctly, such as invalid inputs or error responses from APIs.

In conclusion, MVVM significantly improves the testing experience in iOS app development by promoting code modularity, test-driven development, and clear separation of concerns. These testing benefits contribute to a more stable, reliable, and maintainable iOS app, ultimately resulting in a positive user experience.

Photo by the blowup on Unsplash

Sign up to discover human stories that deepen your understanding of the world.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Mayank Choudhary
Mayank Choudhary

Written by Mayank Choudhary

Self-Taught Developer 👨‍💻 • Open Source Enthusiast• Postman API fest 22 mentor• Cloud Native Hackathon Winner • Hacktoberfest 2020 • Harvard University CS50x

Responses (3)

Write a response

Great post - it's a great idea to create a "mother" folder for all the view and inside that folder create MVVM folders.
I will try this in my next app - thanks!

lots of buzzwords, promoting MVVM in SwiftUI is a crime

I'd change your title, since this is much, much more about folder structure than explaining MVVM.