<!--
{
  "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/EnvironmentValues/subscript(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Subscript",
  "symbol" : {
    "kind" : "Instance Subscript",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI17EnvironmentValuesVy5ValueQzxmcAA0C3KeyRzluip::OverloadGroup"
  },
  "title" : "subscript(_:)"
}
-->

# subscript(_:)

Accesses the environment value associated with a custom key.

```
subscript<K>(key: K.Type) -> K.Value where K : EnvironmentKey { get set }
```

## Overview

Create a custom environment value by declaring a new property
in an extension to the environment values structure and applying
the [`Entry()`](/documentation/SwiftUI/Entry()) macro to the variable declaration:

```
extension EnvironmentValues {
    @Entry var myCustomValue: String = "Default value"
}
```

You use custom environment values the same way you use system-provided
values, setting a value with the [`environment(_:_:)`](/documentation/SwiftUI/View/environment(_:_:)) view
modifier, and reading values with the [`Environment`](/documentation/SwiftUI/Environment) property wrapper.
You can also provide a dedicated view modifier as a convenience for
setting the value:

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

---

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)