Translating a Dictionary to an Array

Sometimes it is helpful to pull information out of a dictionary and put it into an array. Suppose, for example, that you want to list all of the movies that have been rated (without their ratings).

In this case, it makes sense to create an instance of the Array type with the keys from your dictionary.

Listing 10.12  Sending keys to an array

i​m​p​o​r​t​ ​C​o​c​o​a​ v​a​r​ ​m​o​v​i​e​R​a​t​i​n​g​s​ ​=​ ​[​"​D​o​n​n​i​e​ ​D​a​r​k​o​"​:​ ​4​,​ ​"​C​h​u​n​g​k​i​n​g​ ​E​x​p​r​e​s​s​"​:​ ​5​,​ ​"​D​a​r​k​ ​C​i​t​y​"​:​ ​4​]​ p​r​i​n​t​(​"​I​ ​h​a​v​e​ ​r​a​t​e​d​ ​\​(​m​o​v​i​e​R​a​t​i​n​g​s​.​c​o​u​n​t​)​ ​m​o​v​i​e​s​.​"​)​ l​e​t​ ​d​a​r​k​o​R​a​t​i​n​g​ ​=​ ​m​o​v​i​e​R​a​t​i​n​g​s​[​"​D​o​n​n​i​e​ ...

Get Swift Programming: The Big Nerd Ranch Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.