Skip to content
Justice Tech School

Dictionaries

A dictionary maps keys to values. Lookups return optionals.

Example
var d = ["a": 1, "b": 2]
d["c"] = 3
print(d["a"] ?? 0, d["z"] ?? 0)
for (k, v) in d.sorted(by: { $0.key < $1.key }) { print(k, v) }
d["a"] = nil
print(d.count)

Exercise

Default for missing.

d["x"]  0

Quiz Test

What does d["z"] return if missing?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.