<!--
{
  "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/KeyedDecodingContainerProtocol/decodeIfPresent(_:forKey:)-613fl",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s30KeyedDecodingContainerProtocolPsE15decodeIfPresent_6forKeys5Int16VSgAFm_0I0QztKF"
  },
  "title" : "decodeIfPresent(_:forKey:)"
}
-->

# decodeIfPresent(_:forKey:)

Decodes a value of the given type for the given key, if present.

```
func decodeIfPresent(_ type: Int16.Type, forKey key: Self.Key) throws -> Int16?
```

## Parameters

`type`

The type of value to decode.

`key`

The key that the decoded value is associated with.

## Return Value

A decoded value of the requested type, or `nil` if the
`Decoder` does not have an entry associated with the given key, or if
the value is a null value.

## Discussion

This method returns `nil` if the container does not have a value
associated with `key`, or if the value is null. The difference between
these states can be distinguished with a `contains(_:)` call.

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

---

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)
