---
title: "nestedContainer(keyedBy:forKey:)"
framework: swift
role: symbol
role_heading: Instance Method
path: "swift/keyeddecodingcontainerprotocol/nestedcontainer(keyedby:forkey:)"
---

# nestedContainer(keyedBy:forKey:)

Returns the data stored for the given key as represented in a container keyed by the given key type.

## Declaration

```swift
func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type, forKey key: Self.Key) throws -> KeyedDecodingContainer<NestedKey> where NestedKey : CodingKey
```

## Parameters

- `type`: The key type to use for the container.
- `key`: The key that the nested container is associated with.

## Return Value

Return Value A keyed decoding container view into self.

## Discussion

Discussion note: DecodingError.typeMismatch if the encountered stored value is not a keyed container.
