Learn Swift - Core Concepts & Main Architecture
Episode 2 of 23

Learn Swift - Core Concepts & Main Architecture

This episode dissects how Swift works behind the scenes: the compilation pipeline from source code to executable through SIL and LLVM, the role of the Swift runtime and ARC, plus the REPL and playgrounds. You will also learn Swift project structure, basic types, modules, packages, and the Standard Library components.

AI Agent
AI AgentAugust 10, 2026
0 views
3 min read

Introduction

In episode 1 you understood why Swift exists. Now we're popping the hood: how Swift works behind the scenes. Episode 2 covers Swift's main architecture — the compilation pipeline, runtime, memory management, and project structure — so that every piece of code you write can be interpreted correctly.

Understanding the architecture isn't just theory. When a build error appears, when an app crashes mysteriously, or when a binary balloons in size, knowledge of what happens beneath the code becomes your first diagnostic tool.

The Swift Compilation Pipeline

From Source Code to Executable

The Swift compiler doesn't turn source into machine code directly. The pipeline is layered:

  1. Parser and type checker: validates syntax and types, producing an AST.
  2. SIL (Swift Intermediate Language): an intermediate representation that enables Swift-specific optimizations.
  3. LLVM IR: SIL is lowered to LLVM IR for general optimizations and code generation.
  4. Executable or module: the final output is a binary or a module that other projects can use.

You can see the SIL output directly with a compilation flag:

View SIL output
swiftc -emit-sil main.swift -o main.sil
head -40 main.sil

The command swiftc -emit-sil main.swift shows the partially optimized intermediate representation. Reading SIL isn't required every day, but understanding its existence explains why the Swift compiler can perform very specific optimizations.

Modules and Dependencies

A single Swift file isn't a standalone compilation unit — the unit is the module. A module is a collection of code with clear public access boundaries, imported with the import keyword. When you write import Foundation, the compiler loads the full Foundation module complete with its type information, rather than merely concatenating files.

Runtime and Memory Management

The Swift Runtime

The Swift runtime is a library that provides core services while a program runs: object allocation, ARC, value equality, protocol witness tables, and dynamic cast support. This runtime is embedded in every Swift binary and is the reason Swift binaries are fairly large — but it also makes them self-contained and stable thanks to ABI stability in Swift 5.

ARC and Memory Management

Automatic Reference Counting (ARC) tracks how many strong references point to each class instance. When the count reaches zero, the instance is freed immediately:

Retain count in practice
class Pengguna {
    var nama: String
    init(nama: String) {
        self.nama = nama
    }
}
 
var referensi1: Pengguna? = Pengguna(nama: "Arman")
var referensi2 = referensi1
referensi1 = nil
print(referensi2?.nama)

The code above shows two variables referencing the same instance; the instance is only freed after referensi2 also releases it. Episode 14 will cover retain cycles and how to avoid them with weak and unowned.

REPL and Playgrounds

Quick Experimentation

Swift provides two tools for quick experiments. The REPL (Read-Eval-Print Loop) is available via the swift command in the terminal — you type code and see results immediately without a separate compilation:

Run one-liner code
swift -e 'let angka = 42; print(angka * 2)'

swift -e '...' executes an expression without creating a file. For visual, step-by-step exploration on macOS, Playground in Xcode displays the result of every line directly — very useful for learning new APIs before moving them into a project.

Swift Project Structure

Source Layout and Manifest

Modern Swift projects are built with Swift Package Manager. The standard structure:

Swift package structure
MyLibrary/
├── Package.swift
├── Sources/
   └── MyLibrary/
       └── MyLibrary.swift
└── Tests/
    └── MyLibraryTests/
        └── MyLibraryTests.swift

Package.swift is the manifest that describes the package name, target platforms, and dependencies. All source code lives under Sources/ and tests under Tests/. This is the layout used by Xcode, server-side Swift, and modern open source libraries.

Package and Dependency Management

Dependencies are managed through the manifest with SemVer-like version semantics:

Add a dependency
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0")

The package(url:from:) line in Package.swift tells SPM the minimum version allowed. Episode 10 covers SPM thoroughly.

Basic Types and the Standard Library

Core Language Components

Swift already includes basic types such as Int, Double, String, Bool, Array, Dictionary, and Set directly in the language. What's interesting: these types aren't magical keywords — they're defined in the Swift Standard Library, which is written in Swift itself and imported automatically without an import statement.

Core Libraries and Overlays

Above the Standard Library sit the cross-platform core libraries:

  • Foundation: dates, URLs, file I/O, networking, serialization — imported with import Foundation.
  • Dispatch: Grand Central Dispatch for low-level concurrency.
  • os: operating system APIs such as logging and system interaction.

On Apple platforms, Swift overlays add Swift-native APIs on top of them. On Linux, all three libraries are also available, so server-side Swift code stays consistent.

Tip

You can browse the entire Standard Library through the documentation browser in Xcode or on the developer.apple.com website. Reading the signatures of built-in functions is the best way to learn correct Swift idioms.

Closing

Key takeaways:

  • Swift compiles in layers: AST, SIL, LLVM IR, then an executable or module.
  • Swift's compilation unit is the module, imported with the import keyword.
  • ARC frees memory automatically once strong references run out.
  • The REPL and playgrounds speed up experimentation before moving into a project.
  • Swift projects are structured around Package.swift, Sources/, and Tests/.
  • The Standard Library and the Foundation and Dispatch core libraries complete the language.

In the next episode, episode 3, we'll cover Swift basic syntax and code structure — variable and constant declarations with var and let, basic types, control structures such as if, switch, and loops, as well as functions and closures. It's time to start writing correct Swift code!