DEV Community

Gamya
Gamya

Posted on

Swift Dictionaries

We've already seen how arrays let us store lots of values in one place. But arrays have a problem β€” you have to remember positions to get your data back.

What if there was a smarter way? πŸ€”

That's where dictionaries come in.


The Problem with Arrays

Let's say you're storing a character's profile from My Hero Academia:

var hero = ["Izuku Midoriya", "One For All", "Class 1-A"]
Enter fullscreen mode Exit fullscreen mode

Now to read that data back:

print("Name: \(hero[0])")
print("Quirk: \(hero[1])")
print("Class: \(hero[2])")
Enter fullscreen mode Exit fullscreen mode

This works... but it's fragile. What if someone removes an item from the array?

print("Name: \(hero[0])")
hero.remove(at: 1)
print("Quirk: \(hero[1])")   // ❌ Now prints "Class 1-A" β€” wrong!
print("Class: \(hero[2])")   // πŸ’₯ CRASH β€” index doesn't exist anymore
Enter fullscreen mode Exit fullscreen mode

Everything falls apart. You can't trust positions. You have to remember what index holds what β€” and that's a recipe for bugs πŸ›


Enter Dictionaries

Instead of positions, dictionaries use keys β€” labels you choose yourself to describe exactly what each value means.

Here's the same hero profile, rewritten as a dictionary:

let hero = [
    "name": "Izuku Midoriya",
    "quirk": "One For All",
    "class": "Class 1-A"
]
Enter fullscreen mode Exit fullscreen mode

Now reading data is crystal clear:

print(hero["name", default: "Unknown"])
print(hero["quirk", default: "Unknown"])
print(hero["class", default: "Unknown"])
Enter fullscreen mode Exit fullscreen mode

No guessing. No positions. No crashes from removed items. Just clean, readable code 🌸


πŸ”‘ Keys and Values

Every item in a dictionary has two parts:

Part What it is Example
Key The label you use to find the data "name"
Value The actual data stored "Izuku Midoriya"

Think of it like a character profile card β€” you look up the field name (key) and get the info (value) back instantly, even if there are thousands of entries. Swift optimizes dictionaries specifically for this kind of fast lookup ⚑


⚠️ Why Dictionaries Return Optionals

Try reading a key that doesn't exist in the dictionary:

print(hero["villain"])
Enter fullscreen mode Exit fullscreen mode

Swift won't crash β€” but it will return nil, meaning nothing. That's because Swift can't guarantee that every key you ask for actually exists. This is why reading from a dictionary gives you back an optional value β€” it might be there, or it might not.

You'll see a warning like:

Expression implicitly coerced from 'String?' to 'Any'
Enter fullscreen mode Exit fullscreen mode

Swift is telling you "hey, are you sure this value exists?" πŸ€”

We'll cover optionals in depth later in this series β€” but for now, there's a simple and clean solution.


πŸ›‘οΈ Default Values β€” Your Safety Net

When reading from a dictionary, you can provide a default value that gets used if the key doesn't exist:

print(hero["name", default: "Unknown"])      // Izuku Midoriya
print(hero["villain", default: "Unknown"])   // Unknown
Enter fullscreen mode Exit fullscreen mode

No crash. No warning. Just a safe fallback value. βœ…

Here's a more real-world example β€” tracking exam scores:

let examScores = [
    "math": 95,
    "english": 88,
    "history": 72
]

print(examScores["math", default: 0])      // 95
print(examScores["science", default: 0])   // 0 β€” didn't take it
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ When should you use a default value?

  • Use default: 0 if a missing score means the student didn't take the exam
  • Skip the default and handle nil directly if missing means they haven't taken it yet

The right choice depends on what your app needs to do with that missing data.


Different Types for Keys and Values

Dictionaries aren't limited to String keys and String values. You can mix and match types.

String keys, Bool values β€” tracking which characters have awakened their powers:

let awakenedPowers = [
    "Izuku": true,
    "Bakugo": true,
    "Mineta": false
]
Enter fullscreen mode Exit fullscreen mode

Integer keys, String values β€” tracking anime seasons by year:

let seasonYears = [
    2013: "Attack on Titan S1",
    2017: "Attack on Titan S2",
    2022: "Attack on Titan Final Season"
]

print(seasonYears[2013, default: "Unknown"])
// Attack on Titan S1
Enter fullscreen mode Exit fullscreen mode

Creating Empty Dictionaries

Just like arrays, you can start with an empty dictionary and fill it in over time.

var characterStats = [String: Int]()

characterStats["strength"] = 95
characterStats["speed"] = 88
characterStats["intelligence"] = 70
Enter fullscreen mode Exit fullscreen mode

The [String: Int] syntax means:

  • Keys will be String
  • Values will be Int

You can do the same for any type combination β€” [String: String], [Int: Bool], and so on.


πŸ”„ Updating Values β€” No Duplicates Allowed

Dictionaries do not allow duplicate keys. If you assign a value to a key that already exists, Swift simply overwrites the old value:

var rivalry = [String: String]()

rivalry["Naruto"] = "Sasuke"
print(rivalry["Naruto", default: "Unknown"])  // Sasuke

rivalry["Naruto"] = "Pain"
print(rivalry["Naruto", default: "Unknown"])  // Pain
Enter fullscreen mode Exit fullscreen mode

The old value is gone β€” replaced by the new one. So every key in a dictionary is always unique. πŸ”’


πŸ› οΈ Useful Dictionary Operations

πŸ“ Count β€” How many entries?

let heroTeam = ["leader": "Levi", "scout": "Eren", "medic": "Hange"]
print(heroTeam.count) // 3
Enter fullscreen mode Exit fullscreen mode

πŸ—‘οΈ Remove Everything

var tempData = ["key1": "value1", "key2": "value2"]
tempData.removeAll()
print(tempData.count) // 0
Enter fullscreen mode Exit fullscreen mode

🧩 Putting It All Together

Here's a mini character profile system using everything we covered:

// Create a character profile
var profile = [String: String]()

profile["name"] = "Levi Ackerman"
profile["rank"] = "Captain"
profile["squad"] = "Survey Corps"
profile["weapon"] = "Omni-Directional Mobility Gear"

// Read profile safely
print(profile["name", default: "Unknown"])     // Levi Ackerman
print(profile["rank", default: "Unknown"])     // Captain
print(profile["hometown", default: "Unknown"]) // Unknown

// Update a value
profile["rank"] = "Humanity's Strongest"
print(profile["rank", default: "Unknown"])     // Humanity's Strongest

// Check size
print(profile.count) // 4

// Clear everything
profile.removeAll()
print(profile.count) // 0
Enter fullscreen mode Exit fullscreen mode

πŸ†š Arrays vs Dictionaries β€” Which to Use?

Arrays Dictionaries
Access by Position (index) Label (key)
Order βœ… Preserved ❌ Not guaranteed
Duplicate items βœ… Allowed ❌ Keys must be unique
Safe access ❌ Wrong index = crash βœ… Missing key = nil
Best for Ordered lists Labelled data / lookups

🌟 Wrap Up

Dictionaries solve a real problem that arrays can't β€” they let you store and retrieve data using meaningful labels instead of fragile position numbers.

  • Use arrays when order matters and items are accessed by position
  • Use dictionaries when you want to look things up by name

In real apps you'll reach for dictionaries constantly β€” user profiles, settings, API responses, game stats, and so much more. Getting comfortable with them now will pay off big time down the road πŸ’ͺ

Next up, we'll look at sets β€” another way to store data, but with some very interesting superpowers. See you there! πŸ‘‹

Top comments (4)

Collapse
 
junhao profile image
Jun Hao

Hi MY DEAR GIRL

Such a beautifully written explanation. 🌸 The way complex concepts like arrays, dictionaries, keys, values, and optionals are broken down into simple, relatable examples makes Swift much less intimidating for beginners.
I especially enjoyed how the post focuses not just on syntax, but on why dictionaries are often the safer and more reliable choice. The examples are clear, engaging, and easy to follow, making learning feel enjoyable rather than overwhelming.
Thank you for taking the time to create educational content that is both informative and approachable. Posts like this help many aspiring developers build confidence and understand programming concepts more deeply. Looking forward to reading more of your Swift tutorials and learning from your experience. βœ¨πŸ‘
From Your Best Friend

Collapse
 
gamya_m profile image
Gamya

Thank you so much; this genuinely made my day! 🌸 Comments like yours are exactly what keep me motivated to keep writing and learning in public. I'm really glad the examples felt relatable and that the "why" behind dictionaries came throughβ€”that's always what I try to focus on, because understanding the reason matters just as much as knowing the syntax. We're all on this learning journey together, and knowing that these articles are helping even one person feel more confident with Swift means everything to me. More articles are on the way, so stay tuned β€” and thank you for taking the time to leave such a kind and thoughtful comment! βœ¨πŸ‘

Collapse
 
junhao profile image
Jun Hao

Hello, MY DEAR GIRL
I saw your comment.
Thank you. I am always interested in your articles.
Please post many more good articles in the future.
I would like to have a conversation with you and hear about your experiences.
From your friend

Thread Thread
 
gamya_m profile image
Gamya

Thank you so much for the kind words, it really means a lot! 🌸 There's plenty more Swift content coming, so stay tuned! Feel free to share your thoughts in the comments on any article β€” see you in the next one! 😊✨