Skip to content

Instantly share code, notes, and snippets.

@mykolaharmash
Last active October 27, 2020 11:49
Show Gist options
  • Save mykolaharmash/a47b109bbdbdc613693dcc1673a6cc08 to your computer and use it in GitHub Desktop.
Save mykolaharmash/a47b109bbdbdc613693dcc1673a6cc08 to your computer and use it in GitHub Desktop.
@State with reference type (broken)
import SwiftUI
class Counter {
var count: Int
init(_ count: Int) {
self.count = count
}
func increment() {
count = count + 1
}
}
struct ContentView: View {
@State 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