Dictionaries

Overview

Strings for keys, Integers for values.

Dictinaries do not allow dublicate keys to exist.

Sample Codes

Standart Array

var employee = ["Taylor Swift", "Singer", "Nashville"]

Dictionary type

Standart use

let employee2 = [
    "name": "Taylor Swift",
    "job": "Singer", 
    "location": "Nashville"
]

If error

let olympics = [
    2012: "London",
    2016: "Rio de Janeiro",
    2021: "Tokyo"
]

print(olympics[2012, default: "Unknown"])
print(olympics[2013, default: "Unknown"]) // in error the result is "Unknown" #learn

Empty dictionery

var heights = [String: Int]()
heights["Yao Ming"] = 229
heights["Shaquille O'Neal"] = 216
heights["LeBron James"] = 206

Sources

Videos

Articles / Documents

Last updated