Skip to content

Instantly share code, notes, and snippets.

@ralfebert
Last active April 22, 2021 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ralfebert/255127ae12c9995e16eaa6dea553840c to your computer and use it in GitHub Desktop.
Save ralfebert/255127ae12c9995e16eaa6dea553840c to your computer and use it in GitHub Desktop.
import SwiftUI
class DemoModel : ObservableObject {
enum State {
case value(Date)
}
@Published var counter = 0
@Published var state = State.value(Date())
}
@main
struct ObservableObjectInApp: App {
@StateObject var model = DemoModel()
var body: some Scene {
WindowGroup {
VStack {
Button("Increment \(model.counter)") {
model.counter += 1
}
Button("Update \(String(describing: model.state))") {
model.state = .value(Date())
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment