@State
@State is a property wrapper that stores a value locally in a view and updates the UI when the value changes.
Example
Demo.swift
ContentView.swift
App.swift
import SwiftUI
struct CounterView: View {
@State private var count = 0
var body: some View {
VStack(spacing: 12) {
Text("Count: \(count)")
Button("Increment") { count += 1 }
}
.padding()
}
}
Leave a Reply