Post

Replies

Boosts

Views

Activity

How do you let SwiftUI View know that ViewModel's computed property has changed?
Imagine you have ViewModel which computed property may change from time to time: import Foundation class ViewModel: ObservableObject { private var val = 42; var compProp: Int { return val } func maybeChange() { if (Int.random(in: 0..<2) == 1) { val += 1 ??? //heyViewThePropertyIsChanged(nameof(compProp)) } } } How could you force the View: import SwiftUI struct ContentView: View { @StateObject var viewModel: ViewModel var body: some View { VStack { Text("\(viewModel.compProp)").font(.title) } .frame(minWidth: 320) .toolbar { Button(action: viewModel.maybeChange) { Label("Maybe", systemImage: "gear.badge.questionmark") } }.padding() } } to reflect such a change?
3
0
202
Sep ’24