斯威夫特山地车_斯威夫特的物业包装

斯威夫特山地车

Properties in Swift are used to hold some kind of data. Sometimes these properties have some kind of logic to be triggered when they’re modified. For Example - having a property which maintains the state of the cache, when it’s changed, the application needs to update the values to UserDefaults. Something like this -

Swift中的属性用于保存某种数据。 有时,这些属性在修改后会触发某种逻辑。 例如-具有一个属性来维护缓存的状态,当更改时,应用程序需要将值更新为UserDefaults 。 像这样-

In this example, when isCacheUpdated and isUserDetailsUpdated are modified, it needs to save the values to UserDefaults to maintain the state of the cache. If you notice, the logic for updating the userDefaults is repetitive and not generic. So if we were to maintain some more states, we have to write more number of lines of code.

在此示例中,修改isCacheUpdatedisUserDetailsUpdated时,需要将值保存到UserDefaults以维护高速缓存的状态。 如果您注意到,用于更新userDefaults的逻辑是重复的,而不是通用的。 因此,如果我们要维护更多状态,就必须编写更多行代码。

Same is the case with userData and accountData, when the data is updated, it needs to encrypt before saving it to database. How can we solve this problem where we can reuse this logic and make it more generic? And easy to use too 😃

与userData和accountData一样,在更新数据时,需要先对其进行加密,然后再将其保存到数据库中。 我们如何解决这个问题,在这里我们可以重用此逻辑并使它更通用? 而且容易使用😃

The answer is Property Wrappers in Swift 5.1. Let’s see how it works and solves our problem.

答案是Swift 5.1中的 Property Wrappers。 让我们看看它如何工作并解决我们的问题。

物业包装 (Property Wrappers)

Property wrappers are types which wraps a given value and attach associated logic to it. It can be implemented using either Struct or Class by annotating it with the @propertyWrapper attribute. The only real requirement is that each property wrapper type should contain a stored property called wrappedValue, which tells Swift which underlying value that’s being wrapped.

属性包装器是包装给定值并为其附加关联逻辑的类型。 通过使用@propertyWrapper属性对其进行注释,可以使用StructClass来实现它。 唯一真正的要求是每个属性包装器类型都应包含一个称为wrappedValue的存储属性,该属性告诉Swift正在包装哪个基础值。

Let’s take the above example and see how we can use Property wrappers for isCacheUpdated and isUserDetailsUpdated -

让我们以上面的示例为例,看看如何为isCacheUpdatedisUserDetailsUpdated使用属性包装器-

Image for post

In this example, we’ve created a property wrapper UserDefaultsBacked which wraps values which needs to be stored to UserDefaults. This struct is annotated with @propertyWrapper and wraps values of type DataType (Generic)

在此示例中,我们创建了一个属性包装器UserDefaultsBacked ,该包装器包装了需要存储到UserDefaults中的值。 该结构用@propertyWrapper注释,并包装类型为DataType(通用)的值

Property Wrappers can also have their own set of properties to hold some values. Here, we’ve a initialiser which takes key as a parameter to point to the correct data in userDefaults.

属性包装器也可以具有自己的一组属性来保存某些值。 在这里,我们有一个初始化程序,该初始化程序使用key作为参数来指向userDefaults中的正确数据。

Now see how easy it is to use it in the code-

现在看看在代码中使用它有多么容易-

Image for post

it’s that simple 😀 Just declare the variable with annotation and we can save significant amount code and make it more generic.

就是这么简单😀只需用批注声明变量,我们就可以节省大量代码并使其更通用。

See the code below to implement Property Wrappers to wrap userData and accountData -

请参阅下面的代码以实现属性包装器来包装userDataaccountData-

Image for post

Here we create a property wrapper SecureData and implement the required property wrappedValue. See how we use this -

在这里,我们创建一个属性包装器SecureData并实现所需的属性wrappedValue。 看看我们如何使用-

Image for post

Just declare userData and accountData with @SecureData annotation and everything is set up. Swift automatically understands that userData and accountData are wrapped by property wrapper SecureData and it applies associated logic whenever these values are modified.

只需使用@SecureData批注声明userDataaccountData 即可完成所有设置。 Swift自动了解userData和accountData由属性包装器SecureData包装,并且只要修改了这些值,它就会应用关联的逻辑。

We’ve seen, property wrappers are easy to use types and saves significant amount of code and provide more generic solutions. It seems exciting to me but let me know what do you think about it.

我们已经看到,属性包装器是易于使用的类型,并节省了大量代码并提供了更通用的解决方案。 对我来说似乎很令人兴奋,但请告诉我您对此有何看法。

Thanks for Reading 😀

感谢您的阅读😀

翻译自: https://medium.com/analytics-vidhya/property-wrappers-in-swift-2a1355bbf171

斯威夫特山地车

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值