简体   繁体   English

@State 属性包装器的 SwiftUI 文档

[英]SwiftUI Documentation for @State property wrapper

I recently ran into an issue where I had to init an @State variable in my init method.我最近遇到了一个问题,我必须在我的 init 方法中初始化一个@State变量。 This post helped me figure that out. 这篇文章帮助我弄清楚了这一点。

What I realized was that this is the way to do it:我意识到这是这样做的方法:

@State var fullText: String // No default value of ""

init(letter: String) {
    _fullText = State(initialValue: list[letter]!)
}

I understand that @State is a property wrapper, and I read the documentation on property wrappers.我知道@State是一个属性包装器,我阅读了有关属性包装器的文档。 And from other reading I discovered under the hood this code:从其他阅读中我发现了这段代码:

@State private var flag = false

is translated into this code:被翻译成这个代码:

private var _flag: State<Bool> = State(initialValue: false)
private var $flag: Binding<Bool> { return _flag.projectedValue }
private var flag: Bool {
    get { return _flag.wrappedValue }
    nonmutating set { _flag.wrappedValue = newValue }
}

My question is where is it documented that _variableName is created from the wrapped property method and I can redefine it?我的问题是它在哪里记录了_variableName是从包装的属性方法创建的,我可以重新定义它? How is a SwiftUI developer supposed to know that from Apple's docs? SwiftUI 开发人员应该如何从 Apple 的文档中知道这一点? I'm trying to find what I'm assuming is Documentation I'm missing.我试图找到我所假设的我丢失的文档。

As property wrappers were a Swift Evolution proposal and were developed as part of Swift's open source philosophy, we can find the best documentation directly in the Proposal SE-0258 document .由于属性包装器是Swift Evolution提案,并且是作为 Swift 开源理念的一部分开发的,因此我们可以直接在提案 SE-0258 文档中找到最好的文档 This describes the rationale behind the @propertyWrapper design, and the exact specification to how they are implemented (including the definitions of wrapped values and projected values ).这描述了@propertyWrapper设计背后的基本原理,以及它们如何实现的确切规范(包括包装值投影值的定义)。

Sections that may help in your understanding:可能有助于您理解的部分:

§ A property wrapper type provides the storage for a property that uses it as a wrapper. §属性包装器类型为将其用作包装器的属性提供存储。 The wrappedValue property of the wrapper type provides the actual implementation of the wrapper, while the (optional) init(wrappedValue:) enables initialization of the storage from a value of the property's type.包装器类型的wrappedValue属性提供了包装器的实际实现,而(可选) init(wrappedValue:)允许从属性类型的值初始化存储。

The use of the prefix _ for the synthesized storage property name is deliberate: it provides a predictable name for the synthesized storage property that fits established conventions for private stored properties.为合成存储属性名称使用前缀_是有意的:它为合成存储属性提供了一个可预测的名称,该名称符合私有存储属性的既定约定。

[...] [...]

§ A property wrapper type can choose to provide a projection property (eg, $foo ) to expose more API for each wrapped property by defining a projectedValue property. §属性包装器类型可以选择提供一个投影属性(例如, $foo ),通过定义一个projectedValue属性来为每个包装的属性公开更多的API。 As with the wrappedValue property and init(wrappedValue:) , the projectedValue property must have the same access level as its property wrapper type.wrappedValue属性和init(wrappedValue:)projectedValue属性必须与其属性包装器类型具有相同的访问级别。

As property wrappers are new to Swift as a whole, documentation is still lacking on Apple's side in my opinion, especially when it comes to SwiftUI.由于属性包装器对 Swift 整体而言是新的,在我看来,Apple 方面仍然缺乏文档,尤其是在 SwiftUI 方面。 However, there are only a handful of property wrappers we really have to know about ( @State , @Binding , @EnvironmentObject , @ObservedObject , @Published ) to fully utilise SwiftUI;不过,也有唯一的财产包装了一把,我们真的了解( @State@Binding@EnvironmentObject@ObservedObject@Published )充分利用SwiftUI; all other aspects of SwiftUI (such as View types) are pretty well documented in Apple's Developer docs. SwiftUI 的所有其他方面(例如View类型)在 Apple 的开发人员文档中都有很好的记录。

Additionally, articles shared in the Swift community ( example ) can help you to get a grasp on where you might want to implement property wrappers yourself!此外,Swift 社区中共享的文章( 示例)可以帮助您了解您可能希望自己实现属性包装器的位置!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM