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

# decodeIfPresent(_:)

Decodes a value of the given type, if present.

```
mutating func decodeIfPresent<T>(_ type: T.Type) throws -> T? where T : Decodable
```

## Parameters

`type`

The type of value to decode.

## Return Value

A decoded value of the requested type, or `nil` if the value
is a null value, or if there are no more elements to decode.

## Discussion

This method returns `nil` if the container has no elements left to
decode, or if the value is null. The difference between these states can
be distinguished by checking `isAtEnd`.

> 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)