Swift standard library basics: numbers, strings, collections and algorithms

The Swift standard library is the set of types and functions every Swift program gets without an import: numbers, strings, optionals, collections, and the algorithms that work on them. It is separate from Foundation, which adds things like Date, URL and JSONDecoder and has to be imported. This post covers the parts you use every day.
Basic types
Section titled “Basic types”- Integers:
IntandUIntare the platform word size (64 bits on Macs, iPhones and other 64-bit targets; 32 bits on Apple Watch’sarm64_32). Fixed-width types areInt8,Int16,Int32,Int64and the matchingUInttypes. UseIntunless you need a specific size. Overflow traps at runtime instead of wrapping; use&+,&-and&*when you want wrapping arithmetic. - Floating point:
Floatis 32-bit andDoubleis 64-bit. A literal like3.14is inferred asDouble. Bool:trueorfalse. Swift does not convert integers toBool, soif 1 { }doesn’t compile.Character: one extended grapheme cluster, which is what a user sees as a single character."é"and"🇨🇦"are each oneCharacter, even though they are made of more than one Unicode scalar.String: a Unicode-correct sequence ofCharactervalues. Because characters have variable width, you can’t index a string with anInt; you useString.Index.
let flag = "🇨🇦"print(flag.count) // 1 (one Character)print(flag.unicodeScalars.count) // 2 (two regional indicator scalars)
let greeting = "Hello, Swift"let comma = greeting.firstIndex(of: ",")!let hello = greeting[..<comma] // Substring "Hello"print(String(hello))Slicing a String gives you a Substring, which shares storage with the original. Convert it with String(...) if you keep it around, so the original string’s memory can be released.
Collections
Section titled “Collections”Array, Set and Dictionary are all value types with copy-on-write storage: assigning one to another variable is cheap, and the buffer is only copied when one of the copies is mutated. Whether a collection can be changed depends on whether you declare it with let or var.
An ordered, zero-indexed collection. Indexing out of bounds is a runtime trap, not an optional.
var numbers = [1, 2, 3, 4, 5]numbers.append(6) // [1, 2, 3, 4, 5, 6]numbers.insert(0, at: 0) // [0, 1, 2, 3, 4, 5, 6]print(numbers.first ?? -1) // 0; first and last return optionalsAn unordered collection of unique Hashable elements, with fast membership checks and set algebra.
var colors: Set<String> = ["red", "green", "blue"]colors.insert("yellow")let warm: Set = ["red", "orange", "yellow"]print(colors.intersection(warm)) // ["red", "yellow"] in some orderDictionary
Section titled “Dictionary”An unordered collection of key-value pairs with unique Hashable keys. Subscripting returns an optional, because the key might not be there.
var ages: [String: Int] = ["Alice": 30, "Bob": 25, "Charlie": 22]ages["David"] = 28print(ages["Eve"] ?? 0) // 0, key not presentprint(ages["Zoe", default: 0]) // 0, using the default subscriptages["Bob"] = nil // removes "Bob"Algorithms on sequences
Section titled “Algorithms on sequences”Most algorithms are defined on the Sequence and Collection protocols, so they work on arrays, sets, dictionaries, ranges and strings alike.
let numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5]
let ascending = numbers.sorted() // [1, 1, 2, 3, 4, 5, 5, 6, 9]let descending = numbers.sorted(by: >) // [9, 6, 5, 5, 4, 3, 2, 1, 1]let evens = numbers.filter { $0 % 2 == 0 } // [4, 2, 6]let squares = numbers.map { $0 * $0 } // [9, 1, 16, 1, 25, 81, 4, 36, 25]let total = numbers.reduce(0, +) // 36let firstBig = numbers.first { $0 > 4 } // Optional(5)sorted() returns a new array; sort() sorts a var array in place. compactMap is map that drops nil results, which is handy for parsing:
let raw = ["1", "two", "3"]let parsed = raw.compactMap { Int($0) } // [1, 3]Gotchas
Section titled “Gotchas”map,filterand friends on anArrayare eager and allocate a new array each time. For long chains over large data, use.lazyto avoid the intermediate arrays.DictionaryandSetiteration order is not stable between runs, so don’t rely on it in tests or output.String.countis O(n) because it has to walk grapheme clusters. UseisEmptyinstead ofcount == 0.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)