swift属性观察器_了解Swift的属性包装器

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:)

术语 (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 ”的声明无效)

Image for post
Click for Gist 单击要点

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的许多属性。 这给出了以下代码:

Image for post
Click for Gist 单击要点

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.

实际上,我们从structclass实例中抽象了@propertyWrapperWrapper提供了透明性,因为当我们使用泛型使用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提供显示数据和时间的标准化方法确实很常见。 由于可以在许多structclass实例中重用它,因此,这无疑是Swift的Property Wrappers的绝佳用法。

Image for post
Click for Gist 单击要点

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属性观察器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值