Skip to content

Instantly share code, notes, and snippets.

@mykolaharmash
Created October 27, 2020 14:21
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 mykolaharmash/915f9252d655b860f040a36983a3d8d1 to your computer and use it in GitHub Desktop.
Save mykolaharmash/915f9252d655b860f040a36983a3d8d1 to your computer and use it in GitHub Desktop.
import SwiftUI
class Counter: ObservableObject {
@Published var count: Int
init(_ count: Int) {
self.count = count
}
func increment() {
count = count + 1
}
}
struct CounterView: View {
@ObservedObject var counter = Counter(0)
var body: some View {
Text(String(counter.count))
Button("+ 1", action: { counter.increment() })
}
}
struct ContentView: View {
@State var random: Int = 0
var body: some View {
VStack {
CounterView()
Spacer()
Text(String(random))
Button("Random", action: { random = Int.random(in: 1...10) })
}.font(.system(size: 26))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment