O'Reilly logo

Swift Programming: The Big Nerd Ranch Guide by John Gallagher, Matthew Mathias

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

import Cocoa var movieRatings = ["Donnie Darko": 4, "Chungking Express": 5, "Dark City": 4] print("I have rated \(movieRatings.count) movies.") let darkoRating = movieRatings["Donnie Darko"] movieRatings["Dark City"] = 5 movieRatings let oldRating: Int? = movieRatings.updateValue(5, forKey: "Donnie Darko") if let lastRating = oldRating, let currentRating ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required