Dictionaries

A dictionary is an unordered collection of values with each one accessed through a unique key. Let's look at the following diagram:

Dictionaries

In our diagram, we have a dictionary of pizzas (Keys) with their prices (Values). In order to find something inside of a dictionary, we must look it up by its key. Let's look at a dictionary syntax:

Dictionary<Key, Value> 

Creating a dictionary

The traditional way of creating a dictionary is to first declare it as a dictionary, and then, inside angle brackets, declare a type for the key and value. Let's create our first dictionary inside Playgrounds:

let dictFirstExample = Dictionary<String, String>()

The immutable ...

Get iOS 10 Programming for Beginners 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.