Sample code of an MVVM-based architecture targetting projects required to support iOS 12+.
Image source: https://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c38de52
This section outlines the key aspects of each component in the proposed architecture.
- Only layer allowed to
import UIKit
. - Main objects are usually
UIView
s andUIViewController
s. UIView
s should be created and configured separately from theUIViewController
:- View code is encouraged, but XIBs and Storyboards (without segues) are accepted.
UIViewController
s has two main responsibilities: managing view lifecycle and setting up bindings between its view and view model:- Traditionally, callback closures or delegates have been used for communication between view controllers and view models. This proposal brings RxSwift to the mix.
- Should not
import UIKit
. Examples:- If you need a
UIColor
, use an enum and convert it to the actualUIColor
in the view layer. - If you need a
UIImage
, use aString
and load the actualUIImage
in the view layer.
- If you need a
- Act as a data source for the views:
- Fetch data from and/or update the model.
- Format data to be presented on the views.
- Notify the view of changes in the model.
- Should not expose the model to the view.
- Dependencies should be provided through dependency injection. Usually contain:
- A
weak
reference to the coordinator. - References to repositories (data providers).
- A
- Responsible for navigation.
- Only layer allowed to call
present
,pushViewController
, etc. - Can be arranged in a tree-like structure for handling complex navigation setups.
- Specially useful if you need to support multiple platforms (eg. iPhone and iPad).
- TBD
- TBD
- TBD
UIViewController
s hold strong references to views and view models.- View models hold strong references to the model and weak references to coordinators.
- Coordinators hold strong references to child coordinators.
- SnapKit: for elegantly setting constraints in code.
- RxSwift: for implementing composable and reactive two-way data bindings.
Files should be grouped first by feature (eg. Cart
, Settings
) and then by function (eg. Models
, View Controllers
).