0
0
Swiftprogramming~15 mins

Dictionary creation and access in Swift - Mini Project: Build & Apply

Choose your learning style9 modes available
Dictionary creation and access
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print(selectedBook) inside the if let block to show the book title.