Skip to content

Dictionaries

Declaring Dictionaries

Dictionaries are an unordered collection of keys and values. Values relate to unique keys and must be of the same type.

When initializing a Dictionary the full syntax is as follows:

var books: Dictionary<Int, String> = Dictionary<Int, String>()

Although a more concise way of initializing:

var books = [Int: String]()
// or
var books: [Int: String]

Declare a dictionary with keys and values by specifying them in a comma separated list.

The types can be inferred from the types of keys and values.

var books: [Int: String] = [1: "Book 1", 2: "Book 2"]
var otherBooks = [3: "Book 3", 4: "Book 4"]

print("books: \(books)") // Output: [2: "Book 2", 1: "Book 1"]
print("otherBooks: \(otherBooks)") // Output: [3: "Book 3", 4: "Book 4"]

Accessing Values

A value in a Dictionary can be accessed using its key:

let bookName = books[1]
print(bookName) // Output: Optional(Book 1)

The values of a dictionary can be iterated through using the values property:

for book in books.values {
  print("Book Title: \(book)")
}

// Output:
// Book Title: Book 2
// Book Title: Book 1

Similarly, the keys of a dictionary can be iterated through using its keys property:

for bookNumber in books.keys {
  print("Book number: \(bookNumber)")
}

// Output:
// Book number: 1
// Book number: 2

To get all key and value pair corresponding to each other (you will not get in proper order since it is a Dictionary)

for (book, bookNumbers) in books {
  print("\(book) -> \(bookNumbers)")
}

// Output:
// 2 -> Book 2
// 1 -> Book 1

Note

Note that a Dictionary, unlike an Array, in inherently unordered-that is, there is no guarantee on the order during iteration.

If you want to access multiple levels of a Dictionary use a repeated subscript syntax.

// Create a multilevel dictionary.
var myDict: [String: [Int: String]] = [
    "Toys": [1: "Car", 2: "Truck"],
    "Interests": [1: "Science", 2: "Math"],
]

if let toys = myDict["Toys"], let truck = toys[2]{
    print(truck) // Output: Truck
}

// or with Optional chaining

if let truck = myDict["Toys"]?[2] {
    print(truck) // Output: Truck
}

Change Value of Dictionary using Key

var user = ["name": "John", "surname": "Doe"]
// Set the element with key: 'name' to 'Jane'
user["name"] = "Jane"
print(user) // Output: ["name": "Jane", "surname": "Doe"]

Get all keys in Dictionary

let user = ["name": "Kirit", "surname" : "Modi"]
let allKeys = Array(user.keys)
print(allKeys) // Output: ["name", "surname"]

Modifying Dictionaries

Add a key and value to a Dictionary

var books = [Int: String]()
// books -> [:]

books[5] = "Book 5"
// books -> [5: "Book 5"]

books.updateValue("Book 5.1", forKey: 5)
// books -> [5: "Book 5.1"]

Note

updateValue return the value that was replaced, or nil if a new key-value pair was added.

let previousValue = books.updateValue("Book 5.2", forKey: 5)

print(books) // Output: [5: "Book 5.2"]
print(previousValue) // Output: Optional("Book 5.1")

Remove value and their keys with similar syntax:

books[5] = nil
// books -> [:]

books [6] = "Deleting from Dictionaries"
// books -> [6: "Deleting from Dictionaries"]

let removedBook = books.removeValue(forKey: 6)
print(removedBook) // Output: Optional("Deleting from Dictionaries")

Merge two dictionaries

var a = ["a": 1, "b": 2]
var b = ["b": 3, "c": 4]

a.merge(b) { old, new in
    new
}

print(a) // print: ["a": 1, "b": 3, "c": 4]
print(b) // print: ["b": 3, "c": 4]

Tip

merging(_:uniquingKeysWith:) Creates a dictionary by merging the given dictionary into this dictionary, using a combining closure to determine the value for duplicate keys.

Comments