Array, Set, and Dictionary
  • July 23, 2025

In Swift (on iOS or anywhere else), Array, Set, and Dictionary are all value-type collections (structs) with different guarantees:


1. Array<Element>

  • Ordered, indexed, allows duplicates.
  • Random access by integer index (O(1)).
  • Literal syntax: let nums = [1, 2, 3]
  • Common ops:
    var a = [1, 2, 3]
    a.append(4)
    let first = a[0]
    a.remove(at: 1)
    
  • Use when order matters or you need positions (e.g., table view data).

2. Set<Element> (where Element: Hashable)

  • Unordered, unique elements (no duplicates).
  • Fast membership tests/inserts/removals (O(1) average).
  • Literal via initializer: let primes: Set<Int> = [2, 3, 5, 7]
  • Useful set algebra:
    let a: Set = [1, 2, 3]
    let b: Set = [3, 4, 5]
    a.union(b)            // {1,2,3,4,5}
    a.intersection(b)     // {3}
    a.subtracting(b)      // {1,2}
    a.isSuperset(of: [1]) // true
    
  • Use when you only care whether something is present, not its position or duplicates.

3. Dictionary<Key: Hashable, Value>

  • Key–value lookup table. Keys are unique and hashable.
  • Average O(1) lookup/insert/remove by key.
  • Literal syntax: let ages = ["Alice": 30, "Bob": 27]
  • Common ops:
    var dict = ["score": 10]
    dict["score"] = 20           // update
    dict["level"] = 3            // insert
    let s = dict["score"]        // optional Int?
    dict.removeValue(forKey: "score")
    dict.updateValue(99, forKey: "bonus") // returns old value if any
    
  • Use when you need to associate values with identifiers (IDs, names, enums, etc.).

Quick chooser

Need… Pick
Ordered list, duplicates, index access Array
Fast “is this here?” checks, no duplicates Set
Map keys to values Dictionary

Shared traits & tips

  • Value semantics: Assigning or passing them copies the value. Swift uses copy-on-write, so it’s cheap until you mutate.
  • Generics & type inference: Compiler usually infers Element, Key, Value.
  • Mutability: let makes the whole collection immutable; use var to mutate.
  • Iteration order: Arrays keep order. Sets are conceptually unordered. Dictionaries maintain insertion order in current Swift, but don’t rely on it for logic.
  • Transforming:
    let arr = [1, 2, 3]
    let set = Set(arr)
    let dict = Dictionary(uniqueKeysWithValues: arr.map { ($0, "\($0)") })
    

That’s the core difference. Want examples for custom types, performance, or bridging with Objective-C? Just ask!

YOU MIGHT ALSO LIKE...
MijickPopups Hero

  Popups Alerts Resizable Sheets Banners

SwiftUI Tooltip

This package provides you with an easy way to show tooltips over any SwiftUI view, since Apple does not provide ...

SimpleToast for SwiftUI

SimpleToast is a simple, lightweight, flexible and easy to use library to show toasts / popup notifications inside iOS or ...

SSToastMessage

Create Toast Views with Minimal Effort in SwiftUI Using SSToastMessage. SSToastMessage enables you to effortlessly add toast notifications, alerts, and ...

ToastUI

A simple way to show toast in SwiftUI   Getting Started • Documentation • Change Log