SwiftUI @State vs Binding

前端 未结 6 1595
野的像风
野的像风 2021-02-04 12:43

I am learning iOS Programming with Swift and SwiftUI. I know very little and I am very confuse about the difference between a @State and a Binding<*>

6条回答
  •  死守一世寂寞
    2021-02-04 13:38

    Both @State and @Binding are property wrappers.

    @State

    • It is used to update the value of a variable every time.
    • We can also say it's a two way binding.
    • If we change the property state then SwiftUI will automatically reload the body of the view.
    • It is used for simple properties like strings, integers and booleans.

    @Binding

    • Using this, you can access the state property of another view.
    • It will give you the read and write access for the variable.

提交回复
热议问题