Swift Standard Library: Complete Overview for iOS/macOS

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 upon which all development for iOS, macOS, watchOS, and tvOS is built. Unlike many languages where the standard library is minimalistic, Swift offers a rich toolkit right out of the box. In this article, we will thoroughly examine the key components of the library used in everyday development.



Basic Data Types and Protocols



The foundation of the library consists of primitive types and protocols that ensure uniformity in data handling.



  • Equality and Comparison Protocols: Equatable, Comparable, Hashable — allow comparing and hashing objects.
  • Representation Protocols: CustomStringConvertible, CustomDebugStringConvertible — for string descriptions of objects.
  • Basic Types: Int, Double, Bool, String, Character — redefined for maximum performance.


// Example of using protocolsstruct Person: Equatable, CustomStringConvertible {    let name: String    let age: Int        var description: String {        return "\\(name), \\(age) years old"    }}

let alice = Person(name: "Alice", age: 30)let bob = Person(name: "Bob", age: 25)print(alice == bob) // falseprint(alice) // "Alice, 30 years old"


Collections: Arrays, Dictionaries, and Sets



Swift provides three main types of collections, optimized for different use cases.



Arrays

Ordered collections with random access. Support dynamic expansion and contraction.



var numbers: [Int] = [1, 2, 3, 4, 5]numbers.append(6)numbers.remove(at: 0)let filtered = numbers.filter { $0 > 3 }print(filtered) // [4, 5, 6]


Dictionaries

Unordered key-value collections. Keys must be Hashable.



var capitals: [String: String] = [    "Russia": "Moscow",    "France": "Paris",    "Japan": "Tokyo"]capitals["Germany"] = "Berlin"if let russiaCapital = capitals["Russia"] {    print("The capital of Russia is \\(russiaCapital)")}


Sets

Unordered collections of unique elements. Ideal for membership checks and mathematical operations.



let setA: Set = [1, 2, 3, 4]let setB: Set = [3, 4, 5, 6]let intersection = setA.intersection(setB)let union = setA.union(setB)print(intersection) // [3, 4]print(union) // [1, 2, 3, 4, 5, 6]


Working with Strings



Strings in Swift are collections of Unicode characters, making internationalization extremely simple.



  • String Indices: startIndex, endIndex, index(_:offsetBy:) — safe character navigation.
  • Interpolation: "\\(variable)" — embedding values into a string.
  • Multiline Literals: """ """ — convenient formatting.


let greeting = "Hello, world!"let firstChar = greeting[greeting.startIndex] // "H"let index = greeting.index(greeting.startIndex, offsetBy: 7)let substring = greeting[index...] // "world!"

// Interpolation and multilinelet name = "Swift"let description = """The \\(name) language is used for developmenton iOS and macOS."""print(description)


Optional Types and Working with nil



Swift offers safe handling of missing values through optional types. This is one of the key features of the language.



var optionalName: String? = "Alex"// Optional                    

Recommendations