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.

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 compiler doesn't turn source into machine code directly. The pipeline is layered:
You can see the SIL output directly with a compilation flag:
swiftc -emit-sil main.swift -o main.sil
head -40 main.silThe 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.
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.
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.
Automatic Reference Counting (ARC) tracks how many strong references point to each class instance. When the count reaches zero, the instance is freed immediately:
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.
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:
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.
Modern Swift projects are built with Swift Package Manager. The standard structure:
MyLibrary/
├── Package.swift
├── Sources/
│ └── MyLibrary/
│ └── MyLibrary.swift
└── Tests/
└── MyLibraryTests/
└── MyLibraryTests.swiftPackage.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.
Dependencies are managed through the manifest with SemVer-like version semantics:
.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.
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.
Above the Standard Library sit the cross-platform core libraries:
import Foundation.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.
Key takeaways:
import keyword.Package.swift, Sources/, and Tests/.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!