swift属性观察器
Difficulty: Beginner | Easy | Normal | ChallengingThis article has been developed using Xcode 11.4.1, and Swift 5.2.2
难度:初学者| 容易| 普通 | 具有挑战性本文是使用Xcode 11.4.1和Swift 5.2.2开发的
先决条件: (Prerequisites:)
You will be expected to be aware how to make a Single View Application in Swift, or a Hello World equivalent
您应该知道如何在Swift或等效的Hello World中制作Single View应用程序
It would be helpful to understand something about lazy variables would be useful
了解有关懒惰变量的信息将很有帮助
Property Wrappers rely on Generics in Swift
属性包装器依赖于Swift中的泛型
术语 (Terminology)
lazy variables: A technique in that a property is only created when it is needed
惰性变量:一种仅在需要时才创建属性的技术
Property Wrappers: A property wrapper adds a layer of separation between code that manages how a property is stored and the code that defines a property
属性包装器:属性包装器在管理属性存储方式的代码与定义属性的代码之间增加了一层隔离
为什么要使用属性包装器? (Why use property wrappers?)
Using Lazy in Swift is useful as it defers initialization of a value until its first access. This is equivalent functionality to wrapping a value in a computed get — lazy makes your code more elegant, easy to read and of course maintainable.
在Swift中使用Lazy很有用,因为它会将值的初始化推迟到其首次访问之前。 这等效于将值包装在计算出的get中的功能-惰性使您的代码更加优雅,易于阅读并且当然可以维护。
Here is an example of the lay instantiation of a struct (if you follow along with the Playground, make sure you comment out one of the People
instances at a time or the compiler will tell you that you have an invalid declaration of 'People
')
这是一个struct的实例化示例(如果您跟随Playground,请确保一次注释掉一个People
实例,否则编译器会告诉您“ People
”的声明无效)
Effectively for each property on the struct
you'd need to have one of these sets of getters and setters. What if you could use a repeatable peice of code that yould allow you to be able to reuse code.
实际上,对于struct
上的每个属性,您都需要具有以下一组getter和setter方法之一。 如果您可以使用可重复的代码,但又希望您能够重用代码,该怎么办。
使用属性包装器 (Using property wrappers)
基本例子 (Basic example)
The idea here is that we can reuse the Wrapper
for as many properties as we would like on the People
struct
. This gives the following code:
这里的想法是,我们可以根据需要在People
struct
上重复使用Wrapper
的许多属性。 这给出了以下代码:
Effectively we are abstracting the @propertyWrapper
away from our struct
or class
instances. The Wrapper
provides transparency since when we are using the Wrapper
using generics we are simply using an Array
of String
.
实际上,我们从struct
或class
实例中抽象了@propertyWrapper
。 Wrapper
提供了透明性,因为当我们使用泛型使用Wrapper
时,我们只是使用String
Array
。
ISO8601DateFormatter (ISO8601DateFormatter)
It is really common to use ISO8601DateFormatter
to provide a standardized way of presenting Data and Time. Since this can be reused in many struct
and class
instances, this is certainly is great use of Swift's Property Wrappers.
使用ISO8601DateFormatter
提供显示数据和时间的标准化方法确实很常见。 由于可以在许多struct
和class
实例中重用它,因此,这无疑是Swift的Property Wrappers的绝佳用法。
projectedValue
预计价值
Let us look here: user.createdAt
user.$createdA
让我们看一下: user.createdAt
user.$createdA
user.createdAt
Each of the two examples above gives us two different outputs — since the second of the two is a projectedValue
.
上面的两个示例都为我们提供了两个不同的输出-因为两个示例中的第二个都是projectedValue
。
So the $
indicates that we want to access the property wrapper itself rather than the property that it is wrapping.
因此, $
表示我们要访问属性包装器本身,而不是其包装的属性。
type(of: user.createdAt) // Foundation.Date.Type type(of: user.$createdAt) // String.Type
type(of: user.createdAt) // Foundation.Date.Type type(of: user.$createdAt) // String.Type
Now of course to use projectedValue
we need to have a var projectedValue
property in our propertyWrapper
.
现在,当然要使用projectedValue
我们需要在propertyWrapper中具有var projectedValue
propertyWrapper
。
结论 (Conclusion)
Property wrappers are a Swift feature that can help you write easy to read and mainable code in Swift. They have been avaliable since Swift 5.1, and have particular power when coupled with SwiftUI — so this is certainly something you should look at as you enhance your understanding of Swift and iOS/iPadOS.
属性包装器是一种Swift功能,可以帮助您在Swift中编写易于阅读且可维护的代码。 自Swift 5.1起,它们就已经可用,并且与SwiftUI结合使用时具有特定的功能-因此,在增强对Swift和iOS / iPadOS的理解时,当然应该注意这一点。
When creating code you should always think about reusability, what a great use of abstraction in the constructs of the language!
在创建代码时,您应该始终考虑可重用性,即在语言构造中对抽象的极大使用!
The code from this is included in the attached Repo.
此代码包含在随附的Repo中 。
If you’ve any questions, comments or suggestions please hit me up on Twitter
如果您有任何疑问,意见或建议,请在Twitter上打我
翻译自: https://medium.com/@stevenpcurtis.sc/understanding-swifts-property-wrappers-805340a8ced6
swift属性观察器