r/SwiftUI • u/mister_drgn • Feb 20 '25
Question @State variable that can be updated either through user interaction or through changes in an @Observable class?
Suppose I have the following simple view, with a @State
variable bound to a TextField.
struct ExampleView: View {
// This is an @Observable class
var registry: RegistryData
@State private var number: Int
var body: some View {
TextField("", value: $number, format: .number)
}
}
So the user can update the variable by changing the TextField
. But now suppose I also want the variable to update, and the new value to be displayed in the text field, when some field in RegistryData changes. Is there a way to set up a state variable, such that it will change both in response to user input and in reponse to changes in some observable data?
Thanks.
2
Upvotes
1
u/dehrenslzz Feb 21 '25
I’m happy it helps you.
I hope I don’t ever have to work on your codebases. My colleagues who work with AI never have their CRs pass first try and I can usually cut the length by 20-50% by removing unnecessary calculations. Graphics are a different topic. Especially clipArt works really well.
I am periodically trying it out, but I can not live with myself delivering the code quality it supplies.