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

Get Swift Quick Syntax Reference 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.