November 2016
Intermediate to advanced
480 pages
14h 42m
English
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") ...Read now
Unlock full access