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

Looping

You can use for-in to loop through a dictionary. Swift’s Dictionary type provides a convenient mechanism to loop through the key-value pairs for each entry. This mechanism breaks each entry into its constituent parts by providing temporary constants representing the key and the value. These constants are placed within a tuple that the for-in loop can access inside of its body.

Listing 10.9 Looping through your dictionary

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

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