MVVM

up:: SwiftUI architecture models ⚐

  • Model
  • View
  • ViewModel

Model

That’s the data, eg. the framework model

View

That’s the display. There should be no logic here. They should be as dump as possible. For example, ternary operators.

ViewModel

This is where the logic sits. Only required if it’s dynamic. If there is a change in view logic.

ObservableObject means this class can “broadcast” data

  • In ObservableObject, you need to use @Published to broadcast the data
  • In the View, you need to use @StateObject to get those changes
    • There you also need to create a new one (with ())
  • Passing one in: observeObject

In the ViewModel:

import SwiftUI
 
final class FrameworkGridViewModel: ObservableObject {
    @Published var selectedFramework: Framework?
}

In the View:

@StateObject var viewModel = FrameworkGridViewModel()