SwiftUI @State vs Binding

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

    Think of State as the single source of truth for your view, as a means of mutating a variable & invalidating the view to reflect that state.

    Binding on the other hand, is a two-way connection between a view and its underlying model. A means of mutating a State that is not managed by the view (for example a Toggle that reflects and controls a bool value that the control itself has no knowledge about its storage or origin)

    Finally, you can get a Binding from any State by using the $ prefix operator.

    A simple guide for choosing between them would be:

    Do I need to modify a value that is private to me? => State

    Do I need to modify a State of some other view? => Binding

    0 讨论(0)
  • 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.
    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2021-02-04 13:40

    SwiftUI is a declarative Component-Oriented framework. You have to forget about MVC where you have controllers mediating between view and model. SwiftUI uses diffing algorithm to understand changes and update only corresponding views.

    @State

    • A State property is connected to the view. A State property is permanently being read by the view. That means that every time the @State property gets changed/updated, the view gets re-rendered and eventually displays the content depending on the @State's data.
    • State is accessible only to a particular view.
    • Simple properties like strings, integers and booleans belongs to a single view - mark as private.
    • All the fields marked as State are stored in special separated memory, where only corresponded view can access and update them.

    @Binding

    • BindableObject protocol, which requires a didChange property. It makes possible to use it inside Environment and rebuild view as soon as it changes.
    • The didChange property should be a Publisher, which is a part of a new Apple’s Reactive framework called Combine.
    • The main goal of Publisher is to notify all subscribers when something changes. As soon as new values appear, SwiftUI will rebuild Views.

    @EnvironmentObject

    • It is a part of feature called Environment. You can populate your Environment with all needed service classes and then access them from any view inside that Environment.
    • @EnvironmentObject is accessible for every view inside the Environment.
    • @EnvironmentObject Properties created elsewhere such as shared data. App crashes if it is missing.
    • The Environment is the right way of Dependency Injection with SwiftUI.
    0 讨论(0)
  • 2021-02-04 13:42

    State

    •   @State keyword allows us to ask the SwiftUI to monitor the value of the property. Once the value will change, the View will be invalidated and rendered again in efficient manner.
    •   A persistent value of a given type, through which a view reads and monitors the value.
    •   It is just another @propertyWrapper that outlines a source of truth.
    •   When you use state the framework allocate persistence storage for variable and tracks it as a dependency ... you alway has to specify an initial constant value"
    

    Binding

    •   @Binding and $ prefix allows passing State property into the nested child.
    •   A manager for a value that provides a way to mutate it.
    •   @Binding yet another @propertyWrapper that depends explicitly on state.
    •   By using the Binding property wrapper you define an explicit dependency to a source of truth without owning it, additionally you don't need to specify an initial value because binding can be derived from state.
    

    Link for your reference: https://medium.com/stepstone-tech/swiftui-101-how-to-use-state-and-binding-in-your-first-custom-ui-control-64d395947492

    0 讨论(0)
  • 2021-02-04 13:48

    State simple properties like string, integers, and Booleans Belong to a single view - mark as private

    Binding complex properties like custom type Sharing data in many views. Required for reference types

    EnvironmentObject properties created elsewhere such as shared data App crashes if it is missing.

    0 讨论(0)
提交回复
热议问题