init(_:)
Creates an environment property to read the specified key path.
Declaration
init(_ keyPath: KeyPath<EnvironmentValues, Value>)Parameters
- keyPath:
A key path to a specific resulting value.
Discussion
Don’t call this initializer directly. Instead, declare a property with the Environment property wrapper, and provide the key path of the environment value that the property should reflect:
struct MyView: View {
@Environment(\.colorScheme) var colorScheme: ColorScheme
// ...
}SwiftUI automatically updates any parts of MyView that depend on the property when the associated environment value changes. You can’t modify the environment value using a property like this. Instead, use the environment(_:_:) view modifier on a view to set a value for a view hierarchy.