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

Populating a Dictionary

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

Listing 10.1 Creating a dictionary

import Cocoa

var str = "Hello, playground"

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.

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