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

import Cocoa

var movieRatings = ["Donnie Darko": 4, "Chungking Express": 5, "Dark City": 4]
print("I have rated \(movieRatings.count) movies.")

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, but for now properties are variables on a type that store or compute some data about the type in which you are interested. In this case, you ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.