<!--
{
  "availability" : [
    "iOS: 17.0.0 -",
    "iPadOS: 17.0.0 -",
    "macCatalyst: 17.0.0 -",
    "macOS: 14.0.0 -",
    "tvOS: 17.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 10.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/Transaction/subscript(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Subscript",
  "symbol" : {
    "kind" : "Instance Subscript",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI11TransactionVy5ValueQzxmcAA0C3KeyRzluip"
  },
  "title" : "subscript(_:)"
}
-->

# subscript(_:)

Accesses the transaction value associated with a custom key.

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

## Overview

Create custom transaction values by defining a key that conforms to the
[`TransactionKey`](/documentation/SwiftUI/TransactionKey) protocol, and then using that key with the subscript
operator of the [`Transaction`](/documentation/SwiftUI/Transaction) structure to get and set a value for
that key:

```
private struct MyTransactionKey: TransactionKey {
    static let defaultValue = false
}

extension Transaction {
    var myCustomValue: Bool {
        get { self[MyTransactionKey.self] }
        set { self[MyTransactionKey.self] = newValue }
    }
}
```

---

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)