swiftui是什么_SwiftUI的@EnvironmentObject是什么?

swiftui是什么

You may think of @EnvironmentObject as shared data that multiple views could use to get the data. This should be used if your data will be shared across many parts of the app.

您可能@EnvironmentObject视为共享数据,多个视图可以使用该共享数据来获取数据。 如果您的数据将在应用程序的许多部分之间共享,则应使用此选项。

“A property wrapper type for an observable object supplied by a parent or ancestor view.” — Apple Documentation

“由父视图或祖先视图提供的可观察对象的属性包装器类型。” — Apple文档

In this tutorial, you’ll learn what @EnvironmentObject is in SwiftUI. You’ll learn:

在本教程中,您将学习@EnvironmentObject中的@EnvironmentObject。 您将学到:

  • How to use @EnvironmentObject.

    如何使用@EnvironmentObject

  • When to use @EnvironmentObject.

    何时使用@EnvironmentObject

先决条件 (Prerequisites)

To follow along in this tutorial, you’ll need a basic familiarity with Swift and some basic knowledge in at least Xcode 11.

要继续学习本教程,您需要对Swift有基本的了解,并且至少在Xcode 11中需要一些基本知识。

何时使用@EnvironmentObject (When to Use @EnvironmentObject)

Given a scenario where view A creates an object and places it inside the environment, any views inside should be able to gain access to that object whenever they need it. This will save a lot of trouble passing from screen to screen.

在场景A中创建一个对象并将其放置在环境中的情况下,其中的任何视图都应能够在需要时访问该对象。 这将节省从屏幕传递到屏幕的许多麻烦。

Image for post

However, there is a pitfall if this is not properly configured. When you declare @EnvironmentObject in your code, SwiftUI will then automatically look for the object, and when it is not found, your app will crash immediately.

但是,如果配置不正确,则会存在陷阱。 当您在代码中声明@EnvironmentObject时,SwiftUI随后将自动查找该对象,当找不到该对象时,您的应用程序将立即崩溃。

Do take note that when you use @EnvironmentObject, you are essentially saying that an object exists, and you should always make sure that it does.

请注意,使用@EnvironmentObject ,实际上是在说一个对象存在,并且应始终确保它存在。

如何使用@EnvironmentObject (How to Use @EnvironmentObject)

With @EnvironmentObject, SwiftUI allows you to share across all the views in your app. It sounds a lot like a singleton to me, but it isn't.

使用@EnvironmentObject ,SwiftUI允许您在应用程序中的所有视图之间共享。 在我看来,这听起来很单身,但事实并非如此。

A more practical use of @EnvironmentObject is where a user moves from View A to View B, then to View C, and then View D, with information passed from screen to screen. Instead of passing from screen to screen, View A could just pass the data to the environment, and then you could then retrieve the data from the environment in View D. Your code will look even tidier this way.

@EnvironmentObject更实际用法是用户从视图A移至视图B,然后移至视图C,然后移至视图D,并在屏幕之间传递信息。 视图A可以将数据传递到环境中,而不是从一个屏幕传递到另一个屏幕,然后可以在视图D中从环境中检索数据。这样,您的代码看起来将更加整洁。

You are building an app where you would like to know if the dark mode is toggled or not. You first create an ObservableObject:

您正在构建一个想知道是否切换了暗模式的应用程序。 您首先创建一个ObservableObject

class DeviceSetting: ObservableObject {
@Published var isDarkMode = false
}

Next, inside SceneDelegate, initialise DeviceSetting:

接下来,在SceneDelegate内部,初始化DeviceSetting

var deviceSetting = DeviceSetting()

And connect it with your ContentView as follows:

并将其与ContentView连接,如下所示:

let contentView = ContentView().environmentObject(deviceSetting)

The first screen that you will be working on is ContentView. This is kind of like your home page.

您将要使用的第一个屏幕是ContentView 。 这有点像您的主页。

Here you will be able to toggle the dark mode to your desire.

在这里,您将能够根据需要切换暗模式。

Image for post

The next screen is the detail screen, where you will be able to set the state of the dark mode.

下一个屏幕是详细信息屏幕,您可以在其中设置暗模式的状态。

@EnvironmentObject var deviceSetting: DeviceSettingvar body: some View {
HStack {
Text("Dark Mode is")
self.deviceSetting.isDarkMode ? Text("Enabled") : Text("Disabled")
}.navigationTitle("Setting Screen")
}

See how this is being done:

看看这是怎么做的:

Image for post

翻译自: https://medium.com/better-programming/what-is-swiftuis-environmentobject-4eb4aee8fc38

swiftui是什么

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值