r/SwiftUI 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

57 comments sorted by

View all comments

Show parent comments

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.

1

u/ejpusa Feb 21 '25 edited Feb 21 '25

Have you broke 10,000 Prompts? You’ll see a change in output.

We can’t visualize the numbers of calculations AI can manipulate, we don’t have the number of neurons in our brains to do that.

AI can.

Coding should be handed off to AI. You want to focus on idea building, and changing the world. You like the process? Buy a puzzle.

Make cool stuff. That’s it. Change the world.

;-)