[英]How @Environment property wrapper Works in SwiftUI
There is no clear explination about @Environment关于@Environment 没有明确的解释
From my understanding @Environment is specifically there to work with SwiftUI's own pre-defined keys and @Environment is great for reading out things like device is in dark mode or light mode, what size class your view is being rendered with, and more – fixed properties that come from the system.
根据我的理解,@Environment 专门用于与 SwiftUI 自己的预定义键一起使用,@Environment 非常适合读取设备处于暗模式或亮模式、您的视图正在呈现的大小类等信息 - 已修复来自系统的属性。
.environment
modifier.environment
修饰符Why we need to create custom environment values with key path system
为什么我们需要使用关键路径系统创建自定义环境值
Probably for convenience, alternatives are strings (not safe and not convenient) and types (would not allow for more than 1 instance of a type).可能为了方便起见,替代方案是字符串(不安全且不方便)和类型(不允许超过 1 个类型的实例)。
Under the hood some modifiers like alignment changes environment value.
在引擎盖下,一些修改器(如对齐)会更改环境值。 Why it is required to modify environment value instead of wrapper View
为什么需要修改环境值而不是包装视图
Because you need to somehow tell the views down the hierarchy how to behave.因为您需要以某种方式告诉层次结构中的视图如何表现。 You don't know what these views will be.
你不知道这些观点会是什么。
If we can set by modifier why we need to inject with .environment modifier
如果我们可以通过修饰符设置为什么我们需要注入 .environment 修饰符
It's the same thing, convenience.都是一样的,方便。
.environment
and some .myCustomEnvironmentValue
would be equivalent. .environment
和一些.myCustomEnvironmentValue
是等价的。
Why can't we use Singleton or different type instead of environment value
为什么我们不能使用 Singleton 或不同类型而不是环境值
Singleton is single, where environments are scoped to hierarchy. Singleton 是单一的,其中环境的范围是层次结构。 You can have different values of the same environment in different parts of the app.
您可以在应用程序的不同部分拥有相同环境的不同值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.