A property wrapper type that can read and write a value managed by SwiftUI. https://developer.apple.com/documentation/swiftui/state
In a View's struct, you can't change a value of properties directly, whereas a wrapped property using @State
attribute can be changed its value by SwiftUI. State properties change, then SwiftUI re-renders the View that refers to these.
import SwiftUI
struct ContentView: View {
@State private var isPowerOn = false
var body: some View {
Button(action: {
isPowerOn.toggle()
}) {
Image(systemName: "power")
.foregroundColor(isPowerOn ? .blue : .gray)
}
}
}