Creates a new dictionary from the key-value pairs in the given sequence.
SDK
- Xcode 10.2+
Framework
- Swift Standard Library
Declaration
Parameters
keysAndValues
A sequence of key-value pairs to use for the new dictionary. Every key in
keys
must be unique.And Values
Return Value
A new dictionary initialized with the elements of keys
.
Discussion
You use this initializer to create a dictionary when you have a sequence of key-value tuples with unique keys. Passing a sequence with duplicate keys to this initializer results in a runtime error. If your sequence might have duplicate keys, use the Dictionary(_:
initializer instead.
The following example creates a new dictionary using an array of strings as the keys and the integers in a countable range as the values:
Precondition: The sequence must not have duplicate keys.