import SwiftUI
struct CounterView: View {
@State private var count = 0
var body: some View {
VStack(spacing: 20) {
Text("\(count)")
.font(.largeTitle)
.accessibilityLabel("Current count")
Button("Increase") {
count += 1
}
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(8)
.accessibilityLabel("Increase count button")
}
.padding()
}
}
struct CounterView_Previews: PreviewProvider {
static var previews: some View {
CounterView()
}
}We use the @State property wrapper to create a variable count that SwiftUI watches for changes. When count changes, the view updates automatically to show the new number.
The Text view displays the current count. The Button increases the count by 1 each time it is tapped.
This simple example shows how @State helps keep UI and data in sync easily.