Created
October 27, 2020 11:50
-
-
Save mykolaharmash/a9929ed3e9704db66c18b76d4945f14f to your computer and use it in GitHub Desktop.
@State with reference type (fixed)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = Counter(counter.count + 1) }) | |
}.font(.system(size: 26)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment