---
title: "init(objects:forKeys:)"
framework: foundation
role: symbol
role_heading: Initializer
path: "foundation/nsdictionary/init(objects:forkeys:)"
---

# init(objects:forKeys:)

Initializes a newly allocated dictionary with key-value pairs constructed from the provided arrays of keys and objects.

## Declaration

```swift
convenience init(objects: [Any], forKeys keys: [any NSCopying])
```

## Parameters

- `objects`: An array containing the values for the new dictionary.
- `keys`: An array containing the keys for the new dictionary. Each key is copied (using doc://com.apple.foundation/documentation/Foundation/NSCopying/copy(with:); keys must conform to the NSCopying protocol), and the copy is added to the new dictionary.

## Discussion

Discussion This method steps through the objects and keys arrays, creating entries in the new dictionary as it goes. An NSInvalidArgumentException is raised if the objects and keys arrays do not have the same number of elements.

## See Also

### Creating a Dictionary from Objects and Keys

- [init(objects:forKeys:count:)](foundation/nsdictionary/init(objects:forkeys:count:).md)
- [init(object:forKey:)](foundation/nsdictionary/init(object:forkey:).md)
