<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/KeyedDecodingContainer/decode(_:forKey:)-1u4zx",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s22KeyedDecodingContainerV6decode_6forKeyqd__qd__m_xtKSeRd__lF"
  },
  "title" : "decode(_:forKey:)"
}
-->

# decode(_:forKey:)

Decodes a value of the given type for the given key.

```
func decode<T>(_ type: T.Type, forKey key: KeyedDecodingContainer<K>.Key) throws -> T where T : Decodable
```

## Parameters

`type`

The type of value to decode.

`key`

The key that the decoded value is associated with.

## Return Value

A value of the requested type, if present for the given key
and convertible to the requested type.

## Discussion> throws: `DecodingError.typeMismatch` if the encountered encoded value
> is not convertible to the requested type.

> throws: `DecodingError.keyNotFound` if `self` does not have an entry
> for the given key.

> throws: `DecodingError.valueNotFound` if `self` has a null entry for
> the given key.

---

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)