- August 28, 2025
- Mins Read
In Swift (on iOS or anywhere else), Array, Set, and Dictionary are all value-type collections (structs) with different guarantees:
Array<Element>O(1)).let nums = [1, 2, 3]var a = [1, 2, 3]
a.append(4)
let first = a[0]
a.remove(at: 1)
Set<Element> (where Element: Hashable)O(1) average).let primes: Set<Int> = [2, 3, 5, 7]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
Dictionary<Key: Hashable, Value>O(1) lookup/insert/remove by key.let ages = ["Alice": 30, "Bob": 27]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
| Need… | Pick |
|---|---|
| Ordered list, duplicates, index access | Array |
| Fast “is this here?” checks, no duplicates | Set |
| Map keys to values | Dictionary |
Element, Key, Value.let makes the whole collection immutable; use var to mutate.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!
This package provides you with an easy way to show tooltips over any SwiftUI view, since Apple does not provide ...
SimpleToast is a simple, lightweight, flexible and easy to use library to show toasts / popup notifications inside iOS or ...
Create Toast Views with Minimal Effort in SwiftUI Using SSToastMessage. SSToastMessage enables you to effortlessly add toast notifications, alerts, and ...