- July 26, 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!
NavigationKit is a lightweight library which makes SwiftUI navigation super easy to use. 💻 Installation 📦 Swift Package Manager Using Swift Package Manager, add ...
An alternative SwiftUI NavigationView implementing classic stack-based navigation giving also some more control on animations and programmatic navigation. NavigationStack Installation ...
With SwiftUI Router you can power your SwiftUI app with path-based routing. By utilizing a path-based system, navigation in your app becomes ...
This package takes SwiftUI's familiar and powerful NavigationStack API and gives it superpowers, allowing you to use the same API not just ...