Collection of key-value pairs where you can select the type of key, Int, String, Any, etc.

  • A diccionary don't have a specified order.
  • A dictionary Key type must conform to the Hashable protocol, like a set's value type.

Creating empty dictionary

var currencies: [String: String] = [:]

Adding pairs

currencies["USD"] = "United States dollar"
currencies["BOB"] = "Boliviano"
currencies["MXN"] = "Mexican peso"

Creating a dictionary with pairs

var currencies: [String: String] = ["USD": "United States dollar",
                                    "BOB": "Boliviano",
                                    "MXN": "Mexican peso"]

Updating values

The advantage of using updateValue() is that you can get the old value on the same line.

If the pair doesn't exist, this creates it.

if let oldValue = currencies.updateValue("Euro", forKey: "EUR") {
    print("Old value: \(oldValue)")

Removing pairs

The advantage of using removeValue() is that you can get the removed value on the same line.

if let removedValue = currencies.removeValue(forKey: "EUR") {
    print("The removed currency is \(removedValue).")

Iterating over a dictionary

for (code, name) in currencies {
    print("\(code) = \(name)")


for code in currencies.keys {
    print("Code: \(code)")


for name in currencies.values {
    print("Name: \(name)")

Creating a array with keys or values

let codes = [String](currencies.keys)
let names = [String](currencies.values)