by Example

Dictionaries (Maps)


Declaring a dictionary is similar to creating
an array. You must provide both the keys and
the values separated by a colon (":"),






To create an empty dictionary, use the initializer syntax


Assign values to the dictionary








var groceryPrices = [
    "Milk": 3,
    "Bread": 2,
    "Butter": 4,
]


var gasPrices = Dictionary<String, Float>()


gasPrices["diesel"] = 4.1
gasPrices["gasoline"] = 3.5


print("Grocery prices: \(groceryPrices)")
print("Gas prices: \(gasPrices)")
$ swift .swift
Grocery prices: ["Bread": 2, "Butter": 4, "Milk": 3]
Gas prices: ["gasoline": 3.5, "diesel": 4.1]