0
0
Swiftprogramming~10 mins

Dictionary creation and access in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Dictionary creation and access
Start
Create Dictionary
Add Key-Value Pairs
Access Value by Key
Check if Key Exists?
NoReturn nil or default
Yes
Use Value
End
This flow shows how to create a dictionary, add key-value pairs, then access values safely by checking if the key exists.
Execution Sample
Swift
var fruits = ["apple": "red", "banana": "yellow"]
let appleColor = fruits["apple"]
let orangeColor = fruits["orange"]
Create a dictionary of fruits and colors, then access colors by keys.
Execution Table
StepActionDictionary StateAccess KeyValue Retrieved
1Create dictionary with apple and banana{"apple": "red", "banana": "yellow"}--
2Access value for key "apple"{"apple": "red", "banana": "yellow"}apple"red"
3Access value for key "orange" (not present){"apple": "red", "banana": "yellow"}orangenil
💡 Finished accessing keys; orange key not found returns nil
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
fruitsempty{"apple": "red", "banana": "yellow"}{"apple": "red", "banana": "yellow"}{"apple": "red", "banana": "yellow"}
appleColornilnilOptional("red")Optional("red")
orangeColornilnilnilnil
Key Moments - 2 Insights
Why does accessing fruits["orange"] return nil instead of an error?
Because the dictionary does not have the key "orange", Swift returns nil safely instead of crashing, as shown in execution_table row 3.
Can we add new key-value pairs after dictionary creation?
Yes, since 'fruits' is declared with 'var', it is mutable and you can add or change pairs after creation.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what value is retrieved when accessing the key "apple"?
Anil
B"yellow"
C"red"
D"orange"
💡 Hint
Check execution_table row 2 under 'Value Retrieved'
At which step does the dictionary get created with initial values?
AStep 1
BStep 2
CStep 3
DNo step creates it
💡 Hint
Look at execution_table row 1 under 'Action' and 'Dictionary State'
If we add fruits["orange"] = "orange" after step 3, what will fruits["orange"] return?
Anil
B"orange"
C"red"
DError
💡 Hint
Recall that 'fruits' is mutable (var) and adding a key-value pair updates the dictionary
Concept Snapshot
Dictionary creation: var dict = ["key": "value", ...]
Access value: dict["key"] returns optional value
If key missing, returns nil safely
Use var for mutable dictionary
Use let for constant dictionary
Full Transcript
This example shows how to create a dictionary in Swift with keys and values. We start by making a dictionary named 'fruits' with two pairs: apple-red and banana-yellow. Then we access the value for the key 'apple' which returns 'red'. Next, we try to access 'orange' which is not in the dictionary, so Swift returns nil safely without error. The dictionary is mutable because it is declared with var, so we can add or change pairs later. This teaches how to create and access dictionaries safely in Swift.