Ios SwiftUI@State vs-Binding

Ios SwiftUI@State vs-Binding,ios,swiftui,Ios,Swiftui,我正在学习使用Swift和SwiftUI进行iOS编程。我对@状态和绑定之间的区别知之甚少,也非常困惑 如果我理解正确的话,Binding只是技术上的State,但它不会更新视图。如果是这样,那么如果我可以使用状态做同样的事情,为什么我需要绑定 • @State keyword allows us to ask the SwiftUI to monitor the value of the property. Once the value will change, the View will

我正在学习使用Swift和SwiftUI进行iOS编程。我对
@状态
绑定
之间的区别知之甚少,也非常困惑

如果我理解正确的话,
Binding
只是技术上的
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 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.
链接供您参考:
状态和绑定都是属性包装

@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"
  • 它用于每次更新变量的值
  • 我们也可以说它是一种双向绑定
  • 如果更改属性状态,SwiftUI将自动重新加载视图主体
  • 它用于字符串、整数和布尔等简单属性
@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.
  • 使用此选项,可以访问另一个视图的状态属性
  • 它将为您提供变量的读写访问权限

状态
视为你观点的唯一真相来源,作为变异变量的一种手段&使视图失效以反映该状态

绑定
另一方面,是。 一种改变视图未管理的
状态
的方法(例如
切换
,它反映并控制控件本身不知道其存储或来源的布尔值)

最后,您可以使用
$
前缀操作符从任何
状态
获取
绑定

一个简单的选择指南是:

我是否需要修改对我来说是私有的值?=>陈述

是否需要修改其他视图的状态?=>装订


SwiftUI是一个面向组件的声明性框架。您必须忘记MVC,在MVC中,控制器在视图和模型之间起中介作用。SwiftUI使用扩散算法来理解更改并只更新相应的视图

@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"
  • 状态属性已连接到视图。视图正在永久读取状态属性。这意味着每当@State属性发生更改/更新时,视图都会重新呈现,并最终根据@State的数据显示内容
  • 状态仅可由特定视图访问
  • 像字符串、整数和布尔值这样的简单属性属于一个视图-标记为private
  • 所有标记为状态的字段都存储在特殊的分离内存中,只有相应的视图才能访问和更新它们
@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.
  • BindableObject协议,需要didChange属性。它可以在环境中使用它,并在视图发生更改时立即重建视图
  • didChange属性应该是一个发布者,它是一个新的苹果反应式框架Combine的一部分
  • Publisher的主要目标是在发生更改时通知所有订阅者。一旦出现新值,SwiftUI将重建视图
@EnvironmentObject

  • 它是名为环境的功能的一部分。您可以使用所有需要的服务类填充您的环境,然后从该环境中的任何视图访问它们
  • @环境对象对于环境中的每个视图都是可访问的
  • @环境在其他位置创建的对象属性,例如共享数据。应用程序丢失时会崩溃
  • 环境是使用SwiftUI进行依赖注入的正确方式
    • 状态 像字符串、整数和布尔值这样的简单属性属于一个视图标记为private

      绑定 复杂属性,如在许多视图中共享数据的自定义类型。引用类型为必需

      环境对象
      其他地方创建的属性(如共享数据应用程序)如果丢失就会崩溃。

      以下是我为自己准备的注释

      @状态:

      • 我们需要在视图结构中使用它
      • 建议将其设置为私有
      • 我们应该提供默认值
      • 可以用作装订
      • 这意味着要存储简单类型,如字符串、Int、Bool等
      @Binding:

      • 这用于在视图之间共享公共数据
      • 最好的例子是,从视图1显示一张工作表,然后从视图2启动关闭操作
      • 不需要默认值,因为它将从另一个视图进行设置
      谢谢

      我想提供一个非常简短的“真实用途”解释,这有助于我把它弄清楚。 我并没有定义状态/绑定,我只是指出了其中的巨大差异

      @State
      拥有价值,“真理之源”
      @Binding
      传递值,用作管道。
      关于@State的一件重要事情是:更改将触发重新绘制。更改@State的值将导致整个视图“重新执行”。

      简单而优雅的解释如果我的视图中有一个自定义类型,它是一个可以由用户更新的属性,那么在该属性上使用@State是否不好?