What is a “dictionary” in Swift and what is it used for?

A dictionary in Swift is a collection type that stores key-value pairs. Each value in the dictionary is associated with a unique key, which is used to access and retrieve the value.

How to declare a dictionary

Dictionaries in Swift are declared using square brackets and key-value pairs, like so:

var myDict = ["key1": "value1", "key2": "value2", "key3": "value3"]
  • This creates a dictionary called myDict with three key-value pairs.
  • In this case, the keys are strings, and the values are also strings.
  • However, dictionaries in Swift can hold values of any type, as long as the keys and values are of the same type.

How to access values in a dictionary?

To access a value in a dictionary, you use its associated key, like so:

var myValue = myDict["key1"]

// Output: "value1"
  • This retrieves the value associated with the key “key1” and assigns it a variable called “myValue”.

How to add a value to a dictionary?

To add a value to a dictionary, you simply use subscript syntax and assign a new value to the key, like so:

myDict["key4"] = "value4"

// Output: ["key1": "value1", "key2": "value2", "key3": "value3", "key4": "value4"]
  • This adds the key-value pair “key4”: “value4” to the dictionary.

If you want to learn how to update an element in a dictionary, feel free to go to this artice.

Empty dictionaries

An empty dictionary has no key-value pairs. To create one, you can define the key-value types using a dictionary literal without providing any key-value pairs. Let’s show a phonebook for example:

var phonebook = [String: String]()
  • This creates an empty dictionary called “phonebook” that has keys of type “String” (for the name) and values of type “String” (for the phone number).

Later you can then add key-value pairs to the dictionary like this:

phonebook["John"] = "555-1234"
phonebook["Jane"] = "555-5678"
  • This adds two key-value pairs to the “phonebook” dictionary, one for John and one for Jane.

Difference in syntax while creating an empty dictionary

In Swift, there are two ways to create an empty dictionary using different syntax:

  1. Literal Syntax: It is defined with an empty pair of square brackets. It can also be used to define non-empty dictionaries with initial key-value pairs.
  2. Initializer Syntax: It calls the dictionary type’s initializer to create an instance of the dictionary.
var phonebook: [String: String] = [:] // Literal syntax
var phonebook = [String: String]() // Initializer syntax

The main difference between the two is that…

  • the dictionary literal syntax is shorter and more concise
  • while the initializer syntax calls the dictionary type’s initializer and is more explicit.

Both ways are valid and it is up to developer’s preference which to use.

Quick notes

  • Dictionaries store key-value pairs, whereas arrays store values in a specific order and sets store unique values in no particular order.
  • Dictionaries allow fast lookup of values based on their keys.
  • Dictionaries have a different syntax for declaration and can hold various types of data as both key and value.
  • Dictionaries offer a variety of methods to add, remove and manipulate values such as updateValue, removeValue, and subscripts.
  • Dictionarys must not have any data stored while created. That’s we use empty ductionarys for.

Leave a Reply

Your email address will not be published. Required fields are marked *