14.10 Sequence-based Dictionary Initialization
Dictionaries may also be initialized using sequences to represent the keys and values. This is achieved using the Swift zip() function, passing through the keys and corresponding values. In the following example, a dictionary is created using two arrays:
let keys = ["100-432112", "200-532874", "202-546549", "104-109834"]
let values = ["Wind in the Willows", "Tale of Two Cities",
"Sense and Sensibility", "Shutter Island"]
let bookDict = Dictionary(uniqueKeysWithValues: zip(keys, values))
This approach allows keys and values to be generated programmatically. In the following example, a number range starting at 1 is being specified for the keys instead of using an array of predefined keys:
let values = ["Wind in the Willows", ...