Skip to Content
Course content

35: Arrays, Sets, and Dictionaries

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I want to show you a snippet of code I reviewed recently. A junior dev was building a simple "Guest List" manager for an event app. They were using an Array to keep track of email addresses and a Dictionary to store meal preferences. It looked fine at first glance, but it had two major flaws that would have caused a production crash and a lot of data duplication.

var guestEmails = ["alice@example.com", "bob@example.com", "alice@example.com"]
var mealPreferences = ["alice@example.com": "Vegan", "bob@example.com": "Gluten-Free"]

// Trying to get a preference for someone not on the list
let guestEmail = "charlie@example.com"
let preference = mealPreferences[guestEmail]! 
print("Guest preference: \(preference)")

If you run this, it crashes instantly. Why? Because mealPreferences[guestEmail] returns an optional. Since Charlie isn't in the dictionary, it returns nil, and the force-unwrap operator (!) tells Swift, "I swear this isn't nil," which is a lie. Beyond the crash, notice the guestEmails array. Alice is in there twice. In a real app, that means she gets two confirmation emails and takes up two seats.

Switching to Sets for Unique Collections

The first thing we need to fix is the duplicate email problem. When I care about uniqueness and don't particularly care about the order of the items, I stop using Arrays and reach for a Set. A Set is essentially an unordered collection of unique elements. If you try to insert "alice@example.com" twice, the Set just ignores the second attempt.

// Change this from [String] to Set<String>
var guestEmails: Set = ["alice@example.com", "bob@example.com", "alice@example.com"]
print(guestEmails.count) // Output: 2

The trade-off here is that you lose index-based access. You can't ask a Set for "the element at index 0" because there is no guaranteed order. But the win is massive: checking if a Set contains a specific item is incredibly fast, regardless of whether you have ten guests or ten million.

Handling Dictionary Optionals Safely

Now, let's kill that crash. In Swift, accessing a value in a dictionary always returns an optional because the compiler has no way of knowing if the key you're asking for actually exists. I've seen too many developers use ! because they "know" the key is there, but user input is unpredictable. Never trust your keys.

The cleanest way to handle this is using nil-coalescing (??) to provide a sensible default, or optional binding (if let) if you need to run specific logic only when a value exists.

let guestEmail = "charlie@example.com"

// Option A: Provide a default value
let preference = mealPreferences[guestEmail] ?? "No preference specified"
print("Guest preference: \(preference)")

// Option B: Only execute if the guest exists
if let specificPreference = mealPreferences[guestEmail] {
    print("Preparing a \(specificPreference) meal.")
} else {
    print("This guest isn't on the list!")
}

Choosing the Right Tool for the Job

I usually tell my mentees to think about their data requirements before typing a single bracket. If you're debating between these three, ask yourself these questions:

  • Do I need to maintain a specific order? Use an Array. It's the workhorse for lists, queues, and stacks.
  • Do I need to ensure every item is unique? Use a Set. It's perfect for things like IDs, tags, or a list of "liked" posts.
  • Do I need to associate a value with a unique key? Use a Dictionary. This is your go-to for caches, configuration settings, or any "lookup table" scenario.

One last tip: remember that for a type to be used as a key in a Dictionary or an element in a Set, it must conform to the Hashable protocol. Standard types like String, Int, and Double already do this, but if you create your own struct to use as a key, you'll need to add that conformance.




📋 Practical Task

Build a Digital Library Inventory

You are building a system to track a small library's inventory. You need to manage a list of unique Book ISBNs and map those ISBNs to the actual titles of the books.

Requirements:

  • Create a Set of strings called catalogedISBNs. Add three different ISBNs to it, including one duplicate (which should be ignored).
  • Create a Dictionary called bookTitles where the Key is the ISBN (String) and the Value is the Title (String).
  • Write a function findBook(isbn: String) that:
    • Checks if the ISBN exists in the catalogedISBNs set.
    • If it exists, retrieves the title from the bookTitles dictionary using safe optional unwrapping.
    • If it doesn't exist or the title is missing, returns a message saying "Book not found in inventory."
  • Test your function with one ISBN that exists and one that does not.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.