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 the foundation on which all development for iOS, macOS, watchOS, and tvOS is built. It provides developers with a set of basic data types, protocols, structures, and functions necessary for everyday work. Unlike third-party frameworks (e.g., UIKit or SwiftUI), the standard library is part of the Swift language itself and is available in any project without additional imports. In this article, we will break down the key components of the library in detail.



Basic Data Types



The Swift Standard Library includes all the primitive types you use daily:



  • Int, Float, Double — numeric types with varying precision.
  • Bool — Boolean type (true/false).
  • String — text handling.
  • Character — a single Unicode character.


An example of working with strings, demonstrating the power of built-in methods:



import Foundation

let greeting = "Hello, Swift!"let uppercased = greeting.uppercased()let containsWord = greeting.contains("Swift")

print(uppercased) // "HELLO, SWIFT!"print(containsWord) // true

// Multiline stringslet multiline = """This is the first line.This is the second line."""print(multiline)


Collections: Array, Set, Dictionary



The library provides three main collection types, optimized for different tasks:



  • Array — an ordered list of elements with index-based access.
  • Set — an unordered collection of unique values.
  • Dictionary — a collection of key-value pairs.


An example of using all three types:



// Arrayvar numbers: [Int] = [1, 2, 3, 4, 5]numbers.append(6)numbers.sort(by: >)

// Setvar uniqueNames: Set<String> = ["Alice", "Bob", "Charlie"]uniqueNames.insert("Alice") // Won't be added, as it already exists

// Dictionaryvar capitals: [String: String] = [ "Russia": "Moscow", "USA": "Washington"]capitals["France"] = "Paris"

print(numbers) // [6, 5, 4, 3, 2, 1]print(uniqueNames) // ["Charlie", "Alice", "Bob"]print(capitals) // ["Russia": "Moscow", "USA": "Washington", "France": "Paris"]


Optional Types (Optionals)



One of the key features of Swift is safe handling of missing values through Optional. This helps avoid many runtime errors common in other languages.



Main ways to work with optionals:



  • Optional Binding (if let, guard let)
  • Force Unwrapping (only if you are sure the value exists)
  • Nil-Coalescing Operator (??)
  • Optional Chaining


var username: String? = "JohnDoe"

// Optional bindingif let name = username { print("Hello, \\(name)!")} else { print("Name not set")}

// Nil-coalescinglet displayName = username ?? "Guest"print(displayName)

// Optional chaininglet count = username?.count ?? 0print("Name length: \\(count)")


Protocols and Extensions



The Swift Standard Library is built on protocols. The main protocols you will use:



  • Equatable — for comparing objects
  • Hashable — for use in Set and Dictionary
  • Comparable — for sorting
  • Codable — for serialization/deserialization (JSON, Property List)
  • Collection — the base protocol for all collections


Example of implementing a prot

Recommendations