How can I initialize or update value of a variable, like with UIKit, in SwiftUI?
For example in the code below I can update my value with action of a Button or with .onAppear() of Text, and there is some other ways. But I am interested if there is a way or tech for coding like this:
struct ContentView: View {
@State private var valueOfA: String?
var body: some View {
valueOfA = "Magic!" // ← Here(I know it is impossible but if there is a way of this coding?)
Button("change") { valueOfA = "omid" }
Button("nil") { valueOfA = nil }
Text(valueOfA ?? "")
.onAppear {
valueOfA = "Some Value"
}
}
}

@Statevariable in thebody. This will cause an infinite loop of the view trying to update itself. What do you want instead?GeometryReaderso we can see the big picture here.