swift · tutorial · architecture

Arrays and Dictionaries in Real App Data Flows

Published November 9, 2026 · 3 min read · beginner

Goal

By the end of this tutorial, you will have a simple feature model that uses arrays and dictionaries for real app data instead of abstract examples.

Requirements

  • Basic Swift knowledge
  • Xcode 15+
  • A feature that needs a small collection of records

The use case

Arrays and dictionaries show up in lists, lookup tables, and feature configuration. The goal is to keep them understandable when they are part of a real screen.

Step 1 - Start with a list

struct Contact {
    let id: UUID
    let name: String
}

let contacts = [
    Contact(id: UUID(), name: "Ana"),
    Contact(id: UUID(), name: "Ben")
]

What changed:

  • the data is explicit
  • the collection is easy to inspect

Verification:

  • you can read the records directly
  • the feature owns a small, clear list

Step 2 - Add a lookup table

let labelsByID: [UUID: String] = [
    contacts[0].id: "Primary",
    contacts[1].id: "Secondary"
]

What changed:

  • the dictionary adds a fast lookup
  • the mapping stays close to the feature

Verification:

  • each ID points to a readable label
  • the shape matches the feature’s needs

Step 3 - Use both together in the UI

struct ContactList: View {
    let contacts: [Contact]
    let labelsByID: [UUID: String]

    var body: some View {
        List(contacts, id: \.id) { contact in
            Text(labelsByID[contact.id] ?? contact.name)
        }
    }
}

What changed:

  • the array drives the rows
  • the dictionary adds detail without changing ownership

Verification:

  • the list still uses one source of truth
  • the fallback path is obvious

Result

You now have a small data flow that shows when arrays and dictionaries help instead of getting in the way. That is the right level for most feature code.

Common Swift pitfalls

  • Mistake: turning collection types into a giant generic model
    Why it happens: the examples in docs are often too abstract
    How to fix it: keep the data shaped like the screen
  • Mistake: using a dictionary when the key is not meaningful
    Why it happens: lookup feels convenient
    How to fix it: prefer the simplest collection that matches the behavior

When NOT to use this

Do not add a dictionary if a plain array or property is enough. Use the smallest structure that keeps the feature readable.

Takeaway

If you remember one thing: Use arrays and dictionaries to model feature flow, not just to store data.

Next steps