Swift Collections: Array, Set, Dictionary
Swift's three collections are different access patterns, not just different APIs. Arrays keep order, Sets enforce uniqueness, and Dictionaries map keys to values. Picking an Array for uniqueness checks turns membership from O(1) into O(n) scans.
WHY IT EXISTS: Before Swift codified its collection architecture, developers shoehorned every problem into arrays. This created linear searches, duplicates, and fragile parallel-array logic. Swift formalized three distinct types so your data structure matches your access pattern, making performance obvious at the declaration site.
THE MENTAL MODEL: Think of the three collections as different filing systems. An Array is a numbered stack where position matters and duplicates are allowed. A Set is a velvet bag where you only care if an item is inside, not how many times or in what order. A Dictionary is a labeled cabinet where every document has a unique tag and you retrieve it by that tag. The choice is about which question you are asking: what is at index five, is this item present, or what belongs to this key?
HOW IT WORKS: Arrays store elements in contiguous memory with integer indices, giving O(1) random access but O(n) membership checks. Sets are backed by hash tables, so insertion, deletion, and containment are O(1) on average, but elements must be Hashable and order is undefined. Dictionaries also use hash tables, mapping unique Hashable keys to values for O(1) average lookup. All three are value types, so copies are cheap via copy-on-write, yet mutations trigger unique references before modification.
WHEN TO USE IT: Use an Array to preserve sequence, allow duplicates, or access elements by integer position, such as table view rows. Use a Set to enforce uniqueness, test membership, or perform operations like union and intersection, such as tracking selected tags. Use a Dictionary for associative lookup, such as caching objects by ID or grouping items by category.
WHEN NOT TO USE IT: Do not use an Array when you only care about uniqueness, because contains becomes a linear search. Do not use a Set when order matters or elements lack reliable Hashable conformance. Do not use a Dictionary when keys are unstable or you need positional sequencing, since dictionaries do not guarantee order. Avoid nesting optionals inside dictionaries unless you must distinguish a missing key from a nil value.
ONE CANONICAL EXAMPLE: Imagine fetching user permissions from a server as an array of strings. Storing them in an Array means scanning every element to check for admin access. Store them in a Set instead, making permission checks O(1) while silently deduplicating malformed payloads. If you later need to map each permission to an expiration timestamp, upgrade to a Dictionary keyed by permission name. This mirrors the natural shift from ordered lists to uniqueness checks to associative metadata.
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.