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

# nestedContainer(keyedBy:forKey:)

Stores a keyed encoding container for the given key and returns it.

## Declaration

```swift
mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: KeyedEncodingContainer<K>.Key) -> KeyedEncodingContainer<NestedKey> where NestedKey : CodingKey
```

## Parameters

- `keyType`: The key type to use for the container.
- `key`: The key to encode the container for.

## Return Value

Return Value A new keyed encoding container.
