Initializer

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.