The Model-View-ViewModel Architectural Pattern

The Model-View-ViewModel (MVVM) architectural pattern used by iOS/Swift applications. MVVM is an expansion of MVC where we formally couple the view and the controller, but move all the presentation logic out of the controller into a new object called the view model.

Within an iOS/Swift context, the role of the view is fulfilled by the view+view controller combination (see Figure ).

Figure Model-View View-Model pattern on iOS
 
The roles of the three components are summarized below:

  • Model: Responsible for storing data, maintains two-way communication with the view model.
  • View/View controller: Represents the view part of the pattern, and handles user interaction events. Maintains two-way communication with the view model.
  • View model: Handles presentation logic, maintains two- communication with both the model and the view/view controller. Handles communication with other controllers that may provide specific functionality like complex business logic, and network requests.