Skip to content

Instantly share code, notes, and snippets.

@mykolaharmash
Created October 27, 2020 12:42
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
ObservableObject, @ObservedObject and @published
import SwiftUI
class Counter: ObservableObject {
@Published var count: Int
init(_ count: Int) {
self.count = count
}
func increment() {
count = count + 1
}
}
struct ContentView: View {
@ObservedObject var counter = Counter(0)
var body: some View {
VStack {
Text(String(counter.count))
Button("+ 1", action: { counter.increment() })
}.font(.system(size: 26))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment