Swift Standard Library (iOS/macOS): Complete Overview

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Swift Standard Library (iOS/macOS): Complete Overview



The Swift Standard Library is a fundamental set of types, protocols, and functions built directly into the language. It provides the basic functionality needed for development on iOS, macOS, watchOS, and tvOS. Unlike Foundation or UIKit frameworks, which are part of the SDK, the Swift Standard Library is available immediately without additional imports. In this article, we will examine in detail its key components, data structures, protocols, and memory management mechanisms.



Basic Data Types and Collections



Swift provides powerful and type-safe collections optimized for performance.



Arrays (Array)

An ordered collection of elements of the same type. Supports random access and dynamic resizing.

var numbers: [Int] = [1, 2, 3]numbers.append(4)let first = numbers[0] // 1numbers.sort()


Sets (Set)

An unordered collection of unique elements. Ideal for membership testing and mathematical operations.

let setA: Set<Int> = [1, 2, 3, 4]let setB: Set<Int> = [3, 4, 5, 6]let intersection = setA.intersection(setB) // {3, 4}let union = setA.union(setB) // {1, 2, 3, 4, 5, 6}


Dictionaries (Dictionary)

An unordered collection of key-value pairs. Keys must be hashable (Hashable).

var capitals: [String: String] = ["Russia": "Moscow", "USA": "Washington"]capitals["Germany"] = "Berlin"if let russiaCapital = capitals["Russia"] {    print(russiaCapital) // Moscow}


Protocols: The Foundation of Swift's Versatility



The Swift Standard Library is built on protocols that define a common interface for various types.



  • Equatable — allows comparing instances using ==.
  • Hashable — required for using a type as a dictionary key or set element.
  • Comparable — enables ordering objects (<, >, <=, >=).
  • Codable — combines Encodable and Decodable for serialization to JSON, Property List, and other formats.
  • CaseIterable — automatically provides a collection of all enumeration values.


Example of Using Codable

struct User: Codable {    let id: Int    let name: String    let isActive: Bool}

let json = """{ "id": 42, "name": "Ivan", "isActive": true}""".data(using: .utf8)!

let decoder = JSONDecoder()if let user = try? decoder.decode(User.self, from: json) { print(user.name) // Ivan}


Memory Management: ARC and Copy-on-Write



Swift uses Automatic Reference Counting (ARC) for memory management of reference types (classes). For structures and enumerations, value copy semantics apply, but with Copy-on-Write (COW) optimization for collections.



var array1 = [1, 2, 3]var array2 = array1 // Data is shared until modifiedarray2.append(4)    // Actual copying occurs here (COW)print(array1.count) // 3 — original array unchanged


Generics



Generics allow writing flexible and reusable functions and types without sacrificing performance.



func swapTwoValues<T>(_ a: inout T, _ b: inout T) {    let temporaryA = a    a = b    b = temporaryA}

var x = 10var y = 20swapTwoValues(&x, &y) // x = 20, y = 10


Optional Types (Optionals)



Safe handling of missing values is one of Swift's key features. Optionals prevent null-pointer exceptions.



var optionalString: String? = "Hello"// Optional bindingif let unwrapped = optional                    

Recommendations