📖 Scenario: You are organizing a small library's book collection. Each book has a unique code and a title.
🎯 Goal: Create a dictionary to store book codes and titles, then access and print a specific book title using its code.
📋 What You'll Learn
Create a dictionary with exact book codes and titles
Create a variable to hold a specific book code
Access the dictionary using the book code variable
Print the accessed book title
💡 Why This Matters
🌍 Real World
Dictionaries are used to store and look up data quickly, like a phone book or a product catalog.
💼 Career
Knowing how to create and access dictionaries is essential for many programming tasks, including data handling and app development.
Progress0 / 4 steps
1
Create the book dictionary
Create a dictionary called books with these exact entries: "B001": "The Swift Programming Language", "B002": "Introduction to Algorithms", "B003": "Clean Code"
Swift
Hint
Use square brackets to create a dictionary with key-value pairs separated by colons.
2
Set the book code variable
Create a variable called selectedCode and set it to the string "B002"
Swift
Hint
Use let to create a constant string variable.
3
Access the book title
Create a variable called selectedBook and set it to the value from books using the key selectedCode. Use optional binding to safely unwrap the value.
Swift
Hint
Use if let to safely get the value from the dictionary.
4
Print the selected book title
Inside the if let block, write print(selectedBook) to display the book title.
Swift
Hint
Use print(selectedBook) inside the if let block to show the book title.
Practice
(1/5)
1. What is a dictionary in Swift used for?
easy
A. To store data in key-value pairs for quick lookup
B. To store a list of values without keys
C. To perform mathematical calculations
D. To create user interface elements
Solution
Step 1: Understand dictionary purpose
Dictionaries store data as pairs of keys and values, allowing fast access by key.
Step 2: Compare options
Only To store data in key-value pairs for quick lookup correctly describes this key-value storage purpose.
Final Answer:
To store data in key-value pairs for quick lookup -> Option A
Quick Check:
Dictionaries = key-value storage [OK]
Hint: Remember: dictionaries pair keys with values for fast access [OK]
Common Mistakes:
Confusing dictionaries with arrays (which have no keys)
Thinking dictionaries perform calculations
Assuming dictionaries create UI elements
2. Which of the following is the correct way to create a dictionary in Swift?
easy
A. let dict = ("apple", 1), ("banana", 2)
B. let dict = ["apple": 1, "banana": 2]
C. let dict = {"apple": 1, "banana": 2}
D. let dict = ["apple", 1, "banana", 2]
Solution
Step 1: Identify Swift dictionary syntax
Swift dictionaries use square brackets with key-value pairs separated by colons.
Step 2: Check each option
let dict = ["apple": 1, "banana": 2] uses correct syntax: ["key": value, ...]. Others use tuples, braces, or arrays incorrectly.
Final Answer:
let dict = ["apple": 1, "banana": 2] -> Option B
Quick Check:
Dictionary syntax = [key: value] [OK]
Hint: Use square brackets with colons for key-value pairs [OK]
Common Mistakes:
Using parentheses instead of square brackets
Using curly braces which define closures
Listing keys and values without colons
3. What will be the output of this Swift code?
let fruits = ["apple": 3, "banana": 5]
print(fruits["banana"] ?? 0)
medium
A. 5
B. nil
C. banana
D. 0
Solution
Step 1: Access dictionary value by key
fruits["banana"] looks up the value for key "banana", which is 5.
Step 2: Use nil-coalescing operator
The ?? 0 means if the key is missing (nil), print 0; here key exists, so print 5.
Final Answer:
5 -> Option A
Quick Check:
fruits["banana"] = 5 [OK]
Hint: Use ?? to provide default if key missing [OK]
Hint: Remember dictionary access returns optional, unwrap before use [OK]
Common Mistakes:
Thinking keys must be integers
Assuming dictionary is immutable by default
Ignoring optional nature of dictionary values
5. Given a dictionary let items = ["pen": 3, "notebook": 0, "eraser": 2], which Swift code correctly creates a new dictionary containing only items with quantity greater than zero?
hard
A. let filtered = items.compactMap { $0.value > 0 }
B. let filtered = items.filter { $0.key > 0 }
C. let filtered = items.filter { $0.value > 0 }
D. let filtered = items.map { $0.value > 0 }
Solution
Step 1: Understand filtering dictionary by value
We want to keep pairs where value > 0, so filter with condition on $0.value.
Step 2: Analyze each option
let filtered = items.filter { $0.value > 0 } uses filter with correct condition. let filtered = items.filter { $0.key > 0 } incorrectly compares key (a String) to 0. Options A and C use map/compactMap which transform values, not filter pairs.
Final Answer:
let filtered = items.filter { $0.value > 0 } -> Option C
Quick Check:
Filter dictionary by value > 0 = let filtered = items.filter { $0.value > 0 } [OK]
Hint: Use filter with $0.value condition to keep matching pairs [OK]
Common Mistakes:
Filtering by key instead of value
Using map instead of filter for selection
Using compactMap which changes type, not filters
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.