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

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<

你可能感兴趣的:(python,java)