Explain it simply
Dictionaries, in plain language
Dictionary is a collection that stores data as key-value pairs. It's useful for things like user profiles, settings, country-capital mappings, or product price tables. Where an array cares mostly about order, a dictionary cares about looking things up by key.
Problem → Why → Solution
The problem — why this concept matters
Force-unwrapping with capitals["Korea"]! can crash your app if the key doesn't exist.
A common mistake
Assuming one sample output proves nil, ownership, actor isolation, and every error path.
Core concept — understand how it works
Treat Dictionaries as a contract about value and reference semantics, mutability, copying, identity, optionality, and lifetime—not merely syntax. Prefer struct or enum value types and use class identity or shared mutable state only when required.
- typed value
- Swift expression or function
- safe result
Explain the type, ownership, and runtime behavior of Dictionaries
Core syntax or pattern
var capitals = [
"Myanmar": "Naypyidaw",
"Japan": "Tokyo"
]
if let myanmarCapital = capitals["Myanmar"] {
print("Capital of Myanmar is \(myanmarCapital)")
}
capitals["Thailand"] = "Bangkok"
print("Total countries: \(capitals.count)")Treat Dictionaries as a contract about value and reference semantics, mutability, copying, identity, optionality, and lifetime—not merely syntax. Prefer struct or enum value types and use class identity or shared mutable state only when required.
Read the code step by step
- typed value
var capitals = [Dictionary is a collection that stores data as key-value pairs. It's useful for things like user profiles, settings, country-capital mappings, or product price tables. Where an array cares mostly about order, a dictionary cares about looking things up by key.
- Swift expression or function
if let myanmarCapital = capitals["Myanmar"] {Treat Dictionaries as a contract about value and reference semantics, mutability, copying, identity, optionality, and lifetime—not merely syntax. Prefer struct or enum value types and use class identity or shared mutable state only when required.
- safe result
print("Total countries: \(capitals.count)")Run and test an original Dictionaries example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
- typed value
- Swift expression or function
- safe result
Success
Explain the type, ownership, and runtime behavior of Dictionaries
Failure
Force-unwrapping with capitals["Korea"]! can crash your app if the key doesn't exist.
Dictionary is a collection that stores data as key-value pairs. It's useful for things like user profiles, settings, country-capital mappings, or product price tables. Where an array cares mostly about order, a dictionary cares about looking things up by key.
var capitals = [
"Myanmar": "Naypyidaw",
"Japan": "Tokyo"
]
if let myanmarCapital = capitals["Myanmar"] {
print("Capital of Myanmar is \(myanmarCapital)")
}
capitals["Thailand"] = "Bangkok"
print("Total countries: \(capitals.count)")When you grab a value from a dictionary by key, the result is an optional — because that key might not actually exist. That's why using if let to print the value only when it exists is the safe approach.
Capital of Myanmar is Naypyidaw Total countries: 3Real-world usage
How to apply it in a real project
Run and test an original Dictionaries example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
Mini challenge
Build it yourself
Run and test an original Dictionaries example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
var capitals = [
"Myanmar": "Naypyidaw",
"Japan": "Tokyo"
]
if let myanmarCapital = capitals["Myanmar"] {
print("Capital of Myanmar is \(myanmarCapital)")
}
capitals["Thailand"] = "Bangkok"
print("Total countries: \(capitals.count)")Expected: Explain the type, ownership, and runtime behavior of Dictionaries
Show hint
Run and test an original Dictionaries example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- typed value → Swift expression or function → safe result
- Treat Dictionaries as a contract about value and reference semantics, mutability, copying, identity, optionality, and lifetime—not merely syntax. Prefer struct or enum value types and use class identity or shared mutable state only when required.
- Force-unwrapping with capitals["Korea"]! can crash your app if the key doesn't exist.
- Run and test an original Dictionaries example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
The Swift Programming Language — Swift.org