<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/EnvironmentKey",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI14EnvironmentKeyP"
  },
  "title" : "EnvironmentKey"
}
-->

# EnvironmentKey

A key for accessing values in the environment.

```
protocol EnvironmentKey
```

## Overview

You can create custom environment values by extending the
[`EnvironmentValues`](/documentation/SwiftUI/EnvironmentValues) structure with new properties.
First declare a new environment key type and specify a value for the
required [`defaultValue`](/documentation/SwiftUI/EnvironmentKey/defaultValue) property:

```
private struct MyEnvironmentKey: EnvironmentKey {
    static let defaultValue: String = "Default value"
}
```

The Swift compiler automatically infers the associated [`Value`](/documentation/SwiftUI/EnvironmentKey/Value) type as the
type you specify for the default value. Then use the key to define a new
environment value property:

```
extension EnvironmentValues {
    var myCustomValue: String {
        get { self[MyEnvironmentKey.self] }
        set { self[MyEnvironmentKey.self] = newValue }
    }
}
```

Clients of your environment value never use the key directly.
Instead, they use the key path of your custom environment value property.
To set the environment value for a view and all its subviews, add the
[`environment(_:_:)`](/documentation/SwiftUI/View/environment(_:_:)) view modifier to that view:

```
MyView()
    .environment(\.myCustomValue, "Another string")
```

As a convenience, you can also define a dedicated view modifier to
apply this environment value:

```
extension View {
    func myCustomValue(_ myCustomValue: String) -> some View {
        environment(\.myCustomValue, myCustomValue)
    }
}
```

This improves clarity at the call site:

```
MyView()
    .myCustomValue("Another string")
```

To read the value from inside `MyView` or one of its descendants, use the
[`Environment`](/documentation/SwiftUI/Environment) property wrapper:

```
struct MyView: View {
    @Environment(\.myCustomValue) var customValue: String

    var body: some View {
        Text(customValue) // Displays "Another string".
    }
}
```

## Topics

### Getting the default value

[`defaultValue`](/documentation/SwiftUI/EnvironmentKey/defaultValue)

The default value for the environment key.

[`Value`](/documentation/SwiftUI/EnvironmentKey/Value)

The associated type representing the type of the environment key’s
value.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)