Accessing and Modifying a Dictionary

Now that you have a mutable dictionary, how do you work with it? You will want to read from and modify the dictionary. Begin by using count to get some useful information about your dictionary.

Listing 10.2  Using count

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​.​"​)

count is a read-only property on the Dictionary type that keeps track of how many instances are held within the dictionary instance itself. We will discuss properties in detail in Chapter 16 ...

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.