I like to know if I could make my view function bind itself via inout instead of Binding, is it possible in right/better syntax for us? right know I am getting compile time error of:
Modifying state during view update, this will cause undefined behavior.
which is understandable for me, I thought maybe I am using-wrong syntax for this work.
PS: I completely know about Binding and it use case, this question try find answer if we could do it with inout as well.
func TextView(inPutValue: inout Bool) -> some View {
return Text("Hello")
.onTapGesture { inPutValue.toggle() }
}
use case:
struct ContentView: View {
@State private var isOn: Bool = true
var body: some View {
TextView(inPutValue: &isOn)
}
}
update:
import SwiftUI
struct ContentView: View {
@State private var value: Int = Int() { didSet { print(value.description) } }
var body: some View {
Button("update State via inout") { inoutFunction(incomingValue: &value) }
}
}
func inoutFunction(incomingValue: inout Int) { incomingValue += 1 }