-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Swift 4 Programming Cookbook

The last collection type we will look at is the Dictionary, which is a familiar construct in programming languages, where it is sometimes referred to as a hash table. A dictionary holds a collection of pairings between a key and a value. The key can be any element that conforms to the Hashable
protocol (just like elements in a set), and the value
can be any type. The contents of a Dictionary are not stored in order, unlike an array; instead, the key is used both when storing a value and as a lookup when retrieving a value.
In this recipe, we will use a Dictionary to store details of people at a place of work. We need to store and retrieve a person's information based on their role in the organization, like a company directory. To hold our person's information, we will use a modified version of our Person
class from Chapter 1, Swift Building Blocks.
Enter the following code into a new playground:
struct PersonName { let givenName: String...