Mastering MVVM With Swift
Mastering MVVM With Swift
About the Book
Massive View Controller Syndrome
Firing up Xcode and starting a new project is a great feeling. The first few days or weeks don’t feel like work. Everything goes according to plan.
But as the code base of your project grows, it becomes harder and harder to manage the project. The view controllers of your project start to put on weight … a lot of weight. They become more than view controllers. They take on responsibilities they didn’t sign up for. Don’t they?
Yet you’ve done everything right. You’ve stuck to the rules of the Model-View-Controller pattern. Why have you coded yourself in a corner? And why is it frustrating or downright scary to add features or refactor existing functionality?
It’s Time to Cure MVC
If your project is suffering from Massive View Controller syndrome, then the Model-View-ViewModel pattern is the cure to your problem. The Model-View-ViewModel pattern has been around for many, many years, but it only recently gained traction in the Cocoa community.
MVVM extends MVC by resolving common issues. The result is a robust application architecture with lean view controllers, improved testability, and a better separation of concerns. How does that sound?
Take Control of Your Project With MVVM
In Mastering MVVM With Swift, we refactor an existing application built with MVC to use MVVM instead. The results are dramatic and the MVVM pattern is surprisingly easy to adopt in your own projects.
You learn the differences between Model-View-Controller and Model-View-ViewModel, highlighting the benefits Model-View-ViewModel has over Model-View-Controller.
After a short introduction, we take an application built with Model-View-Controller and refactor it to use Model-View-ViewModel instead. Along the way, you learn about the anatomy of view models, how to create them, and how to test them.
Last but not least, we add protocols and protocol-oriented programming to the mix to further simplify the view controllers in the project.
At the end of this course, you have the knowledge and, more importantly, the hands-on experience to apply Model-View-ViewModel in your own projects.
Ready for Xcode 9 and Swift 4
In Mastering MVVM With Swift, you learn everything you need to know to integrate MVVM in a new or an existing Swift project. We focus on the key aspects of the pattern and refactor an application that takes advantage of the core features of MVVM. We use the latest and greatest to build an application, which means we use Xcode 9 and Swift 4.
Battling Massive View Controller Syndrome
Are the view controllers of your projects suffering from Massive View Controller syndrome? You’ve carefully crafted the architecture of your application using the Model-View-Controller pattern and, yet, the view controllers of your project are ready to burst and they’re impossible to test.
Adding a feature forces you to wade through hundreds of lines of code. You hope you don’t break anything while you carefully add a few lines of code. Don’t get me wrong. Your code isn’t bad. But there’s just so much of it. You’ve applied the Model-View-Controller pattern like you were told to and you still end up with an architecture you aren’t quite happy with. Does this sound familiar?
It’s Not You. It’s MVC.
Because Apple’s frameworks are impregnated with the Model-View-Controller pattern we think it’s the right or only tool for the job. Don’t make the same mistake. There’s nothing wrong with the Model-View-Controller pattern, but there are better alternatives, especially if you’re using Swift.
Put Your View Controllers On a Diet
It’s time to put the view controllers of your project on a diet. I’m sure you’ve heard about the Model-View-ViewModel pattern. It’s the new cool kid in town. But the thing is that it isn’t a kid. MVVM has been around for many, many years and has earned its stripes. It’s a proven strategy for robust, scalable applications.
Where Should This Code Go?
I bet you’ve asked yourself many times “Where should I put this piece of code?” Every developer asks himself/herself this question, multiple times a day. If you’re still using MVC, then chances are that the answer is “the view controller”. Stop. Give MVVM a shot. You can thank me later.
MVVM is a perfect fit for Cocoa applications powered by Swift. The view model unburdens the view controller from the ungrateful task of data manipulation and transformation. The result is a focused, testable view model and a skinny, lightweight view controller.
Bundles that include this book
Reader Testimonials

Damian Esteban
Chief Technology Officer, betterPT
Even though I was already familiar with MVVM, Bart’s course showed me a different perspective on MVVM by refactoring a complete application built with MVC. He illustrates how MVVM can improve a project’s architecture and testability, and how reactive programming fits in.

Jake Young
Chief Information Officer, ACP Decisions
A big thank you. This has been the single most effective course I've taken about iOS. I put it into practice right away and our App Store app, and its testing, has benefited greatly.
Table of Contents
-
Welcome
- Xcode 9 and Swift 4
- What You’ll Learn
- How to Use This Book
-
1 Is MVC Dead
- What Is It?
- Advantages
- Problems
- An Example
- How Can We Solve This?
-
2 How Does MVVM Work
- Advantages of MVVM
- Basic Rules
- It’s Time to Refactor
-
3 Meet Cloudy
- Application Architecture
-
4 What Is Wrong With Cloudy
- Day View Controller
- Week View Controller
- Locations View Controller
- Settings View Controller
- What’s Next
-
5 A Quick Recap
- MVVM Architecture
- Naming the View Model
-
6 Time to Create a View Model
- Creating the View Model
- Creating the Public Interface
-
7 Put the View Model to Work
- Adding the View Model to the Day View Controller
- Creating the View Model in the Root View Controller
- Updating the Weather Data Container
- Build and Run
-
8 Rinse and Repeat
- Creating the Week View View Model
- Creating the View Model in the Root View Controller
- Updating the Table View
-
9 Using MVVM In the Settings View
- Creating the Settings View Time View Model
- Importing UIKit
- Refactoring the Settings View Controller
- Your Turn
-
10 Adding Protocols to the Mix
- Creating the Protocol
- Conforming to the Protocol
- Refactoring the Settings View Controller
-
11 Making Table View Cells Autoconfigurable
- Updating the Settings Table View Cell
- Updating the Settings View Controller
- Protocol-Oriented Programming
- Conclusion
-
12 Supercharging MVVM With Protocols
- Creating a New View Model
- Refactoring the Week View View Model
- Creating Another Protocol
- Updating the Weather Day Table View Cell
- Updating the Week View Controller
-
13 Ready, Set, Test
- Adding a Unit Test Target
- Organizing the Unit Test Target
-
14 Testing Your First View Model
- Creating a Test Case
- Importing the Cloudy Module
- Writing a Unit Test
- Writing More Unit Tests
- Resetting State
- Unit Testing the Other View Models
-
15 Using Stubs for Better Unit Tests
- Adding Stub Data
- Loading Stub Data
- Unit Testing the Day View View Model
-
16 A Few More Unit Tests
- Unit Testing the Week View View Model
- Unit Testing the Weather Day View View Model
- 17 Taking MVVM to the Next Level
- 18 What Are the Options
-
19 DIY Bindings
- Creating the View Model
- Implementing the View Model
- Performing Geocoding Requests
- Notifying the View Controller
- Refactoring the Add Location View Controller
- Updating the User Interface
- More Refactoring
-
20 Why RxSwift
- It’s a Library
- Testability
- Powerful
-
21 Integrating RxSwift and RxCocoa
- Defining Dependencies
- Installing Dependencies
-
22 Refactoring the View Model
- Refactoring the View Model
- Reducing State
-
23 Refactoring the View Controller
- What Have We Accomplished
-
24 Protocol Oriented Programming and Dependency Injection
- A Plan of Action
- Defining the Protocol
- Adopting the Protocol
- Refactoring the Add Location View View Model
- Refactoring the Add Location View Controller
-
25 Testing and Mocking
- Setting Up the Environment
- Mocking the Location Service
- Writing Unit Tests
-
26 Where to Go From Here
- Putting the View Controllers On a Diet
- Introducing View Models
- Improved Testability
- More Flexibility
- Where to Start
The Leanpub 60-day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms
Do Well. Do Good.
Authors have earned$11,718,542writing, publishing and selling on Leanpub, earning 80% royalties while saving up to 25 million pounds of CO2 and up to 46,000 trees.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers), EPUB (for phones and tablets) and MOBI (for Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them
Top Books
Recipes for Decoupling
Matthias NobackWrite software that survives
CQRS by Example
Carlos Buenosvinos, Christian Soronellas, and Keyvan Akbary- Leverage your Software Architecture skills by learning everything about CQRS in detail with lots of examples
- Develop faster applications by applying CQRS and fostering Read Models and Projections
- Learn how to apply CQRS into a brownfield project from a pragmatic approach
OpenIntro Statistics
David Diez, Christopher Barr, Mine Cetinkaya-Rundel, and OpenIntroA complete foundation for Statistics, also serving as a foundation for Data Science.
Leanpub revenue supports OpenIntro (US-based nonprofit) so we can provide free desk copies to teachers interested in using OpenIntro Statistics in the classroom and expand the project to support free textbooks in other subjects.
More resources: openintro.org.
Maîtriser Apache JMeter
Philippe Mouawad, Bruno Demion (Milamber), and Antonio Gomes RodriguesToute la puissance d'Apache JMeter expliquée par ses commiteurs et utilisateurs experts. De l'intégration continue en passant par le Cloud, vous découvrirez comment intégrer JMeter à vos processus "Agile" et Devops.
If you're looking for the newer english version of this book, go to Master JMeter : From load testing to DevOps
C++20 - The Complete Guide
Nicolai M. JosuttisAll new language and library features of C++20 (for those who know previous C++ versions).
The book presents all new language and library features of C++20. Learn how this impacts day-to-day programming, to benefit in practice, to combine new features, and to avoid all new traps.
Buy early, pay less, free updates.
Other books:
Ansible for DevOps
Jeff GeerlingAnsible is a simple, but powerful, server and configuration management tool. Learn to use Ansible effectively, whether you manage one server—or thousands.
Jetpack Compose internals
Jorge CastilloJetpack Compose is the future of Android UI. Master how it works internally and become a more efficient developer with it. You'll also find it valuable if you are not an Android dev. This book provides all the details to understand how the Compose compiler & runtime work, and how to create a client library using them.
The PowerShell Scripting and Toolmaking Book
Don Jones and Jeff HicksLearn the patterns, practices, and details of PowerShell Scripting and Toolmaking from the industry's two most recognized experts on the subject.
Mastering STM32 - Second Edition
Carmine NovielloWith more than 1200 microcontrollers, STM32 is probably the most complete ARM Cortex-M platform on the market. This book aims to be the most complete guide around introducing the reader to this exciting MCU portfolio from ST Microelectronics and its official CubeHAL and STM32CubeIDE development environment.
Introduction to Data Science
Rafael A IrizarryThe demand for skilled data science practitioners in industry, academia, and government is rapidly growing. This book introduces concepts from probability, statistical inference, linear regression and machine learning and R programming skills. Throughout the book we demonstrate how these can help you tackle real-world data analysis challenges.
Top Bundles
- #1
Software Architecture
2 Books
"Software Architecture for Developers" is a practical and pragmatic guide to modern, lightweight software architecture, specifically aimed at developers. You'll learn:The essence of software architecture.Why the software architecture role should include coding, coaching and collaboration.The things that you really need to think about before... - #3
CCIE Service Provider Ultimate Study Bundle
2 Books
Piotr Jablonski, Lukasz Bromirski, and Nick Russo have joined forces to deliver the only CCIE Service Provider training resource you'll ever need. This bundle contains a detailed and challenging collection of workbook labs, plus an extensively detailed technical reference guide. All of us have earned the CCIE Service Provider certification... - #5
Pattern-Oriented Memory Forensics and Malware Detection
2 Books
This training bundle for security engineers and researchers, malware and memory forensics analysts includes two accelerated training courses for Windows memory dump analysis using WinDbg. It is also useful for technical support and escalation engineers who analyze memory dumps from complex software environments and need to check for possible... - #10
All the Books of The Medical Futurist
6 Books
We put together the most popular books from The Medical Futurist to provide a clear picture about the major trends shaping the future of medicine and healthcare. Digital health technologies, artificial intelligence, the future of 20 medical specialties, big pharma, data privacy, digital health investments and how technology giants such as Amazon...