Skip to content

Swift Collections

Developer UtilitiesUtilities/CollectionsSwift

What it is

Apple's package of production-grade data structures missing from the standard library: OrderedDictionary, OrderedSet, Deque and Heap.

Each type addresses a specific gap: ordering for dictionaries and sets, efficient insertion at both ends, and priority-ordered access.

Installation

.package(url: "https://github.com/apple/swift-collections.git", from: "1.1.0")

Getting started

The smallest useful thing you can do with it, and what each part means.

The four core types
import Collections

// A Dictionary with a stable, insertion-defined order.
var settings: OrderedDictionary<String, String> = [:]
settings["theme"] = "dark"
settings["lang"] = "en"

// A Set that preserves order and supports indexing.
var recent: OrderedSet<Int> = []
recent.append(3); recent.append(1); recent.append(3)   // dedupes

// O(1) at both ends — Array is O(n) at the front.
var queue: Deque<Task> = []
queue.append(task)
let next = queue.popFirst()

// Min and max in O(log n).
var heap: Heap<Int> = [5, 1, 9]
heap.insert(3)
heap.popMin()   // 1
Deque most often fixes a real performance problem: using an Array as a queue and calling removeFirst() is O(n) per operation, which quietly becomes quadratic.

Advanced usage

Where the library earns its place over a simpler alternative.

An LRU cache from OrderedDictionary
struct LRUCache<Key: Hashable, Value> {
    private var storage: OrderedDictionary<Key, Value> = [:]
    private let capacity: Int

    init(capacity: Int) { self.capacity = capacity }

    mutating func get(_ key: Key) -> Value? {
        guard let value = storage[key] else { return nil }
        storage.removeValue(forKey: key)   // move to the end
        storage[key] = value
        return value
    }

    mutating func put(_ key: Key, _ value: Value) {
        storage[key] = value
        if storage.count > capacity {
            storage.removeFirst()          // evict least recently used
        }
    }
}
The eviction is one line because OrderedDictionary keeps insertion order and supports removeFirst — with a plain Dictionary this needs a parallel linked list.

Errors and fixes

The failures you are most likely to hit, and what actually resolves them.

Dictionary iteration order changes between runs
That is by design for Dictionary. Use OrderedDictionary when order matters.
Removing from the front of a large Array is slow
Array shifts every element. Use Deque, which is O(1) at both ends.

Best practices

  • Use Deque wherever you would treat an Array as a queue.
  • Use OrderedDictionary when iteration order matters — Dictionary order genuinely varies per run.
  • Use Heap for priority queues rather than re-sorting an array.
  • Import only the submodules you need to keep build times down.

Background

Why it exists, and what it was reacting to.

The standard library deliberately ships a small set of collections. Swift Collections adds the ones most frequently needed, implemented and maintained to the same standard.