Workshop 20: Dictionary

In this workshop you will learn the basics of declaring, initializing, and accessing entries of a swift dictionary. You will also learn how to manipulating entries of the dictionary.

Before you begin, I assume you have done the following:

workshop16-fig00

A Swift dictionary is a collection of unordered list of keys and values pairs or entries. You can think of a Swift dictionary variable or constant as a glossary of useful terms and their definitions. Like an array, all of the key-pairs types must be the same. Like an array you cannot add, modify, and delete entries from a dictionary constant; you can only query its entries. You can add, modify, and delete entries from a dictionary variable.

Create a Dictionary Variable and Constant

Here are two ways to create a dictionary variable.

// Create and initialize a dictionary variable
var friendsNamesDictionary = ["Mol":"Molly Lee Ford", "Will":"William Abraham Ford", "Alex":"Alexander Mathew Ford"]

// Create a dictionary constant
let daysDictionary = [01:"Sunday", 02:"Monday", 03:"Tuesday", 04:"Wednesday", 05:"Thursday", 06:"Friday", 07:"Saturday"]

// Output in a playground file
["Will": "William Abraham Ford", "Mol": "Molly Lee Ford", "Alex": "Alexander Mathew Ford"]
[1: "Sunday", 2: "Monday", 3: "Tuesday", 4: "Wednesday", 5: "Thursday", 6: "Friday", 7: "Saturday"]

Create an Empty Dictionary Variable

// Two ways to create an empty dictionary variable
var fruitNames = Dictionary<Int,String>()
var fruits: Dictionary = [:]

// Output in a playground file
0 key/value pairs
0 key/value pairs

Accessing an Entry in The Dictionary Variable

A dictionary entry is accessed using it’s key. If the key doesn’t exists in the dictionary the optional value nil is returned.

friendsNamesDictionary["Will"]

// Output in a playground file
{Some "William Abraham Ford"}

Modify an Entry in a Dictionary Variable

Code below shows how to use an entry’s key to modify an entry in the friendsNamesDictionary variable.

friendsNamesDictionary["Alex"] = "Jessica Parker"
friendsNamesDictionary

// Output in the playground file
{Some "Jessica Parker"}
["Will": "William Abraham Ford", "Mol": "Molly Lee Ford", "Alex": "Jessica Parker"]

Update an Entry in The Dictionary Variable

The updateValue(newValue, forKey:key) property is used to update an entry in a dictionary variable.

friendsNamesDictionary.updateValue("Zelda Caban",forKey:"Alex")
friendsNamesDictionary

// Output in the playground file
{Some "Alexander Mathew Ford"}
["Will": "William Abraham Ford", "Mol": "Molly Lee Ford", "Alex": "Zelda Caban"]

Remove an Entry from a Dictionary Variable

To remove an entry from a dictionary; you use the entry’s key; or the the removeValueForKey() property. Code below shows how to use both methods.

// Method 1
friendsNamesDictionary["Alex"] = nil

// Method 2
friendsNamesDictionary.removeValueForKey("Mol")

After executing above code, the friendsNamesDictionary will only have one key-value pair; which is this: [“Will”: “William Abraham Ford”]

Remove All Entries from a Dictionary Variable

To remove all entries (key-value pairs) from a dictionary variable, you have to use the removeAll() property.

friendsNamesDictionary.removeAll()
friendsNamesDictionary.count  // Output 0

Query The Dictionary Variable

You can use these properties to query either a dictionary constant or a dictionary variable.

friendsNamesDictionary.count
friendsNamesDictionary.isEmpty

// Output in a playground file
3
false