介绍SwiftUI的新@ AppStorage,@ StateObject和@SceneStorage属性包装器

SwiftUI在WWDC 2020上得到了增强,新增了属性包装器,包括@AppStorage、@StateObject和@SceneStorage,这些在SwiftUI数据管理中扮演重要角色。iOS 14提供了这三种数据持久性属性包装器,文章将通过Xcode 12 beta版进行深入探讨。
摘要由CSDN通过智能技术生成

SwiftUI got some great new enhancements during WWDC 2020. Among the things that stood out were additions to the SwiftUI property wrapper arsenal. Property wrappers are used heavily in SwiftUI applications for updating and observing views and are a crucial part of SwiftUI data management.

SwiftUI在WWDC 2020期间进行了一些重大的新增强。其中最引人注目的是增加了SwiftUI属性包装器库。 属性包装器在SwiftUI应用程序中大量用于更新和观察视图,并且是SwiftUI数据管理的关键部分。

iOS 14 offers us three new property wrappers for data persistency:

iOS 14为我们提供了三个用于数据持久性的新属性包装器:

  • @AppStorage

    @AppStorage

  • @StateObject

    @StateObject

  • @SceneStorage

    @SceneStorage

In the next few sections, we’ll look at each of them in the new Xcode 12 (in beta at the time of writing).

在接下来的几节中,我们将在新的Xcode 12中(在撰写本文时为beta版)对其进行研究。

SwiftUI @AppStorage (SwiftUI @AppStorage)

An AppStorage property wrapper is used to read and write values to the UserDefaults. Every time the value of the AppStorage property wrapper changes, the SwiftUI view is invalidated and redrawn.

AppStorage属性包装器用于读取和写入值到UserDefaults 。 每次AppStorage属性包装器的值更改时,SwiftUI视图都会无效并重新绘制。

It behaves the same way as @State property wrappers, except that it’s used to communicate between a UserDefaults key and the SwiftUI view in a convenient way. The following code shows how we used UserDefaults

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值