How to Create a Dictionary in Swift

Use dictionaries in Swift when you need to store values by key for fast lookups. Dictionaries are perfect for settings, user records, caches, API payloads, and grouped analytics data.

What you’ll build or solve

You’ll learn how to create dictionaries in Swift with literal syntax, explicit types, empty dictionaries, and model-based lookups. You’ll also know when arrays are a better fit.

When this approach works best

This approach is the right choice when lookup speed by key matters more than order.

Common real-world scenarios include:

  • User IDs → names
  • App settings
  • Product lookups
  • API query params
  • Cached results

This is a bad idea when the data is naturally ordered and should be looped in sequence.

Prerequisites

You only need:

  • Basic Swift collections
  • Familiarity with key-value data
  • Understanding of type inference

Step-by-step instructions

Step 1: Create a dictionary with literal syntax

The most common pattern uses square brackets with key-value pairs.

let scores = [
    "Alex": 95,
    "Sam": 88
]

Swift infers this as [String: Int].

This is the cleanest default syntax.

Step 2: Add an explicit dictionary type

Explicit types help with empty or public dictionaries.

let settings: [String: Bool] = [
    "darkMode": true,
    "notifications": false
]

This improves clarity.

Step 3: Create an empty dictionary

Empty dictionaries need a type hint.

var cache: [String: String] = [:]

This is common for dynamic lookup tables.

Step 4: Store model values

Dictionaries scale well for custom structs.

struct User {
    let name: String
}

let usersById: [Int: User] = [
    1: User(name: "Alex"),
    2: User(name: "Sam")
]

This is excellent for fast model access.

What to look for:

  • Dictionaries optimize key lookups
  • Key types should be stable
  • Empty dictionaries need explicit types
  • Great for caches and settings
  • Arrays are better for ordered lists

Examples you can copy

Settings

let flags = ["beta": true]

Empty cache

var cache: [String: Data] = [:]

Model lookup

let users: [Int: User]

Common mistakes and how to fix them

Mistake 1: Using empty dictionaries without types

What the reader might do:

let data = [:]

Why it breaks: Swift cannot infer key and value types.

Corrected approach:

Use [Key: Value].

Mistake 2: Expecting stable order

What the reader might do:

Use a dictionary for ordered UI rows.

Why it breaks: dictionaries are not the clearest structure for sequence-based UI.

Corrected approach:

Use arrays.

Mistake 3: Using mutable updates on let

What the reader might do:

let cache = ["a": "1"]
cache["b"] = "2"

Why it breaks: immutable dictionaries cannot change.

Corrected approach:

Use var.

Troubleshooting

If type inference fails, declare [Key: Value].

If updates error, switch from let to var.

If the UI needs ordering, convert to an array.

If keys feel unstable, choose a stronger identifier type.

Quick recap

  • Use dictionaries for fast key lookups
  • Empty dictionaries need explicit types
  • Great for settings and caches
  • Use var for mutable updates
  • Prefer arrays for ordered UI data