SwiftUI @State vs Binding

前端 未结 6 1612
野的像风
野的像风 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:39

    Here is the notes I have prepared for myself,

    @State:

    • We need to use this inside a view struct
    • It’s recommended to make as private
    • We should provide default value
    • Can be used as binding
    • This is meant to store simple types like String, Int, Bool, etc...

    @Binding:

    • This is used to share common data between the views
    • Best example is, presenting an sheet from View 1 and initiating dismiss action from View 2
    • No need for default value as will set it from another view

    Thanks!

提交回复
热议问题