The difference between @State private var and @State var?

What's the difference between @State private var and @State var? And when to use the former and when to use the latter?

Answered by simonfromhelix in 790416022

It's good practice to always write

@State private var

This is because you should not declare a view and set the state within it's initialiser, it can cause the value to change undesirably when the view is redrawn.

Use @Binding if you want to bind a variable to a view and make changes to it inside and outside of that view.

Accepted Answer

It's good practice to always write

@State private var

This is because you should not declare a view and set the state within it's initialiser, it can cause the value to change undesirably when the view is redrawn.

Use @Binding if you want to bind a variable to a view and make changes to it inside and outside of that view.

The difference between @State private var and @State var?
 
 
Q