swift - Swiftui 中 init() 中的变量前的下划线是什么意思?

标签 swift xcode swiftui

momentDate 之前的下划线是什么意思?为什么需要它?
enter image description here

最佳答案

下划线变量名指的是 Binding 的底层存储结构。这是名为 Property Wrappers 的语言功能的一部分。 .
给定一个变量声明,@Binding var momentDate: Date ,您可以访问三个变量:

  • self._momentDateBinding<Date>结构本身。
  • self.momentDate ,相当于 self._momentDate.wrappedValue , 是 Date .在 View 正文中呈现日期时,您将使用它。
  • self.$momentDate ,相当于 self._momentDate.projectedValue ,也是Binding<Date> .如果他们需要能够更改日期,您可以将其传递给 subview 。

  • 对于 Binding ,“预计值”( $ )只是 self , 和 _ 的区别和 $仅在 access level .但是,其他属性包装器可能会转换不同类型的值(请参阅 language guide 中的 @SmallNumber 示例)。

    关于swift - Swiftui 中 init() 中的变量前的下划线是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65209314/

    相关文章:

    ios - Swift 3 中的 CSStickyHeaderFlowLayout

    ios - 点击其他节标题时重新加载节标题

    ios - 列表区域无用

    macos - macOS 菜单项未调用 SwiftUI Toggle onChange

    swift - 在 Swift 中自动处理不安全分配的内存

    ios - 将 Swift 添加到项目中会显着增加大小。如何以及何时可以避免

    xcode - PackageMaker 错误无法将资源复制到 en.lproj

    ios - 如何下载并保存音频文件,然后快速播放?

    xcode - swift - 通过prepareforsegue在2个 View Controller 之间添加值

    JSON 解码器 无法读取数据,因为它的格式不正确