Swift Dictionary

What is a Swift Dictionary and how to create?

Tested on: Xcode 12.2, Swift 5.3

Unlike Array which is an ordered collection, the dictionary is another collection but is it unordered. Dictionary contains a key and value. The key cannot appear more than once but the different key can have the same value. These keys help you to identify which value it is related to. As shown in the image below:

The image below shown a key which is the name of the person and the key linked to it is the values which are their age. For example, from here, we know that Tylor is 28 years old and Wes is 24.


Dictionary's keys and values in Swift

When should you use dictionary?

Dictionary is useful when you want to get the value by its identifier or name. The main difference between array and dictionary is that array fetch a value by its index which is sequential while dictionary fetch by its keys and the keys doesn't have any order in it.

To futher your understanding, you will learn

  1. How to create a Dictionary?
  2. How to access the values?
  3. How to modify an array? (Adding, Removing and Updating)
  4. Array methods and properties





1. How to create a Dictionary?

To create a dictionary in Swift, you use square brackets and inside the square brackets, it contains key-value pairs and is separated by commas. As you know Swift will infer the data type and the example below is [String: Int].

Swift Code :

let ages = ["Wes": 24, "Evan": 20, "Jes": 5, "Tylor": 28]

To create an empty dictionary, you can be required to state the data type explicitly.

Swift Code :

let ages: [String:Int] = [:]



2. How to access the values?

Accessing the value in the dictionary is similar to Array using subscript or square brackets. However, you don't access the value by its index but rather you call its keys. For example, you want to get Wes' age and you can type:

Swift Code :

let ages = ["Wes": 24, "Evan": 20, "Jes": 5, "Tylor": 28]
print(ages["Wes"])

Output :

24




3. How to modify an array? (Adding, Removing and Updating)

3.1 Updating and Adding Values

You can change the value by using subscribing or .updateValue(Value, forKey: Key). If the key doesn't exist, this method will add a new pair of value.

For example, Wes's age is entered incorrectly and you can change his age by typing this (below) if the keys exist the value will change. (The example below uses subscribing as an example). This method can also be used to create a new key and value pair.

Swift Code :

var ages = ["Wes": 24, "Evan": 20, "Jes": 5, "Tylor": 28]
ages["Wes"] = 20
print(ages)

Output :

["Wes": 20, "Evan": 20, "Jes": 5, "Tylor": 28]


For example, Chua is 22 years old and you can add him by typing this (below) if the key does not exist, a new key will be created. (The example below uses .updateValue as an example). This method can also be used to update a value.

As you can see, Chua has added anywhere in the dictionary and not the last one. This is because Dictionary is an unordered collection.

Swift Code :

var ages = ["Wes": 24, "Evan": 20, "Jes": 5, "Tylor": 28]
ages.updateValue(22, forKey: "chua")
print(ages)

Output :

["Evan": 20, "chua": 22, "Wes": 24, "Tylor": 28, "Jes": 5]


3.2 Removing a Key and Value Pair

You can remove a value by using .subscripting and assign a value of .nil to it. Moreover, you can use .removeValue(forKey: Key) to remove a pair.

Swift Code :

var ages = ["Wes": 24, "Evan": 20, "Jes": 5, "Tylor": 28]
ages["Evan"] = nil
print(ages)

Output :

["Wes": 24, "Jes": 5, "Tylor": 28]



Swift Code :

var ages = ["Wes": 24, "Evan": 20, "Jes": 5, "Tylor": 28]
ages.removeValue(forKey: "Evan")
print(ages)

Output :

["Wes": 24, "Jes": 5, "Tylor": 28]




4. Array methods and properties

.count

.count is a dictionary's property and it is used to count the number of key-value pairs in the dictionary.

Swift Code :

var ages = ["Wes": 24, "Jes": 5, "Tylor": 28]
print(ages.count)

Output :

3


.isEmpty

.isEmpty is a dictionary's property and it is used to check whether is the dictionary is empty.

Swift Code :

var ages = ["Wes": 24, "Jes": 5, "Tylor": 28]
print(ages.isEmpty)

Output :

false


.reserveCapacity(Int)

.reserveCapacity can be used to increase the performance if you know how much data, the dictionary will be storing. .reserveCapacity(Int) take in a parameter which is an Int and it indicated the size of the dictionary.

The reason to add this is because newly allocated storage might be greater than the requested capacity if Swift determines the size by itself. By stating it, you tell Swift the size of the minimum capacity and Swift will not allocate more space for it. Therefore, avoiding over allocation.

Swift Code :

var ages = ["Wes": 24, "Jes": 5, "Tylor": 28]
ages.reserveCapacity(5)

Output :

false