O'Reilly logo

Swift Quick Syntax Reference by Matthew Campbell

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

Chapter    21

Dictionaries

Use a dictionary when you want to store items that you need to reference with unique identifier keys. Arrays keep items in the order in which you put the items into the array, while dictionaries don’t guarantee any order at all. However, you can access each item based on the key that you provided when the item was originally added to the dictionary.

To create a new dictionary, you will need to specify both the data type for the key and the data type for the value (see Listing 21-1).

Listing 21-1. Declaring Dictionaries

var d1:Dictionary<String, Int>

In Listing 21-1, d1 is declared as a dictionary that requires String keys and Int values. Before you add any items, you will need to initialize d1 (see Listing 21-2). ...

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