December 2018
Intermediate to advanced
414 pages
10h 19m
English
The preceding example made it clear that we should not load views in the constructor. Now, consider the following generic code that helps us to write controllers with an asynchronous state:
enum State<T> { case unknown, loading case loaded(T) case error(Error)}class StateController<T>: UIViewController { var state: State<T> = .unknown { didSet { updateState() } } private func updateState() { switch state { case .loading: view.alpha = 0.0 // make it transparent while loading case .loaded(_): view.alpha = 1.0 // show it when loaded default: break; } }}// In another view controllerlet controller = StateController<String>()controller.state = .loadingpresent(controller, animated: true, completion: nil)
This example ...
Read now
Unlock full access