Populating a Dictionary

To get started, create a new playground called Dictionary.playground. Declare a dictionary called movieRatings and use type inference to initialize it with some data.

Listing 10.1  Creating a dictionary

import Cocoa

var movieRatings = ["Donnie Darko": 4, "Chungking Express": 5, "Dark City": 4]

You created a mutable dictionary using the Dictionary literal syntax. Your dictionary will hold movie ratings. Its keys are instances of the String type and represent individual movies. These keys map onto values that are instances of the Int type that represent individual ratings of the movies.

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.