← thecodex.expert · The Codex Family of Knowledge

Free · No account · Official sources only

Swift — Cradle to Mastery

A complete, structured path through Swift — from Chris Lattner's four years building it in secret to optionals, value semantics, structured concurrency, and the SwiftUI/server-side reach that took Swift well beyond just iOS. Every lesson points at reference pages already written and verified on this site. Free, no account, no sign-up.

30 lessons 6 stages ~21 hours of reading 10 projects Swift 6.x · curriculum current as of 2026
Start Lesson 1 → Browse the reference instead

How this course works

1
Follow the stages in orderEach stage assumes the one before it. Stage 3 in particular assumes Stage 2 cold.
2
Read the tab the lesson namesReference pages have Beginner, Intermediate and Expert tabs. Read only what the lesson asks for on a first pass.
3
Run every exampleKeep the playground open in a second tab. Reading code you have not run is how misunderstandings survive.
4
Build after each stageDo not save all ten projects for the end. Pick one up as soon as a stage gives you enough to attempt it.
Casual pace: ~10 weeks at 4 hours a week
Steady pace: ~5 weeks at 8 hours a week
Intensive: ~2 weeks full-time
STAGE 0

Before You Start

Chris Lattner, the creator of LLVM, spent four years building Swift in secret before Apple unveiled it at WWDC 2014. These two lessons explain why, and get a toolchain installed.

LESSON 1

What is Swift?

Chris Lattner spent 4 years building it in secret before WWDC 2014

Swift was designed by Chris Lattner, the creator of the LLVM compiler infrastructure, over four years of secret development inside Apple before its surprise unveiling at WWDC 2014 — replacing Objective-C with a language built from the start around safety (optionals, value types) without giving up the performance Apple's platforms needed.

📚 🟩 Read the Beginner tab only.
Key focus: Why Swift's design goals (safety plus performance) trace directly back to its LLVM-creator origins.
⏱ 30 min
Required Reading
→
What is Swift? — The Codex

Swift's origin at Apple under Chris Lattner, its 2014 unveiling, and where Swift 6.x sits in that history.

LESSON 2

Setup and Toolchain

Swift itself runs on Linux and Windows — Xcode is only for iOS/macOS apps

The Swift compiler and toolchain run natively on Linux and Windows with no Xcode involved at all; Xcode is required only when you're actually building and submitting an iOS or macOS app, not for learning or writing Swift itself.

📚 🟩 Read the Beginner tab only.
Key focus: Why Swift itself is cross-platform, and exactly when Xcode specifically becomes necessary.
⏱ 30 min
Required Reading
→
Setup and Toolchain — The Codex

Installing the Swift toolchain, compiling and running a first .swift file, and when Xcode is actually required.

STAGE 1

Language Fundamentals

The syntax you will use in nearly every file: constants over variables by default, an exhaustive switch, real closures, and error handling that is genuinely lighter than Java's. Five lessons before optionals and value types properly begin.

LESSON 3

Variables and Types

let makes a value truly constant — var is the exception

Swift treats <code>let</code> as the default reach for most values, with <code>var</code> reserved for the genuine exception where reassignment is actually needed — a small syntactic nudge, like Kotlin's <code>val</code>, toward writing more code that simply can't be accidentally mutated.

📚 🟩 Beginner → ⚡ Intermediate.
Key focus: Why let is the default reach, and recognizing the genuine cases that need var instead.
⏱ 30 min
Required Reading
→
Variables and Types — The Codex

let vs. var, Swift's basic types, type inference, and string interpolation.

LESSON 4

Control Flow

switch must be exhaustive AND never falls through by default

Swift's <code>switch</code> makes two real, compiler-enforced departures from C's classic version: every possible case must genuinely be covered (exhaustiveness), and execution never silently falls through into the next case unless you explicitly ask for it with <code>fallthrough</code>.

📚 🟩 Beginner → ⚡ Intermediate.
Key focus: The two compiler-enforced guarantees — exhaustiveness and no silent fallthrough — that make switch genuinely safer.
⏱ 45 min
Required Reading
→
Control Flow — The Codex

if/else and while loops, switch's exhaustiveness requirement, pattern matching in switch cases, and fallthrough.

LESSON 5

Functions

A parameter can have two different names — one for the caller, one internal

A Swift function parameter can carry an external name the caller writes at the call site and a completely different internal name the function body actually uses — letting a call read like a natural sentence while the implementation still gets a name that makes sense to it.

📚 🟩 Beginner → ⚡ Intermediate.
Key focus: How separate external and internal parameter names make call sites read naturally without compromising the implementation.
⏱ 45 min
Required Reading
→
Functions — The Codex

Function declarations, external vs. internal parameter names, default parameter values, and variadic parameters.

LESSON 6

Closures

@escaping isn't optional — required the moment a closure might outlive the call

The compiler requires the <code>@escaping</code> annotation the exact moment a closure might be stored and called after the function that received it has already returned — a real, enforced distinction from a closure that only runs synchronously during the call, not just documentation.

📚 ⚡ Intermediate.
Key focus: Why the compiler forces @escaping onto closures that can outlive the call that received them.
⏱ 45 min
Required Reading
→
Closures — The Codex

Closure syntax and capturing values, trailing closure syntax, @escaping and when it's required, and autoclosures.

LESSON 7

Error Handling

throws and try are required — genuinely lighter than Java's checked exceptions

A Swift function that can fail must be explicitly marked <code>throws</code>, and every call site must use <code>try</code> to acknowledge that — a real, compiler-enforced requirement, though genuinely lighter-weight than Java's checked-exception system since there's no exception-type hierarchy to declare.

📚 ⚡ Intermediate.
Key focus: The throws/try contract as a real requirement, and why it's lighter than Java's checked exceptions.
⏱ 30 min
Required Reading
→
Error Handling — The Codex

do/try/catch, the Error protocol, throws function declarations, and try?/try! variants.

STAGE 2

Optionals, Value Types and Enums — Swift's Defining Discipline

This is the stage that makes Swift, Swift: optionals as a real, compiler-checked answer to null, value semantics that make copying a struct genuinely safe, and enums that can carry real data. Seven lessons, the heart of the course.

LESSON 8

Optionals and Value Types

if let, guard let, ??, and why structs copy independently

Swift's <code>Optional</code> makes the possibility of absence part of a value's actual type, checked at compile time through <code>if let</code>, <code>guard let</code>, the nil-coalescing operator <code>??</code>, and optional chaining — and that same safety-first design extends to value types, where assigning a struct to a new variable produces a genuine, independent copy rather than a shared reference.

📚 🟩 Beginner → ⚡ Intermediate.
Key focus: How optionals make absence a compile-time-checked type fact, and why value-type copying is independent by design.
⏱ 1.25 hours
Required Reading
→
Optionals and Value Types — The Codex

Optional types and unwrapping (if let, guard let, ??, force-unwrap), optional chaining, and the value-vs-reference-type distinction.

LESSON 9

Structs and Classes

Assign a struct and get a copy; assign a class and share the object

Assigning a struct instance to a new variable produces a genuine, fully independent copy — change one and the other is untouched — while assigning a class instance makes both variables point at exactly the same underlying object, so a change through either one is visible through both.

📚 ⚡ Intermediate.
Key focus: The concrete, observable difference a struct-vs-class choice makes the moment you assign or pass one.
⏱ 1 hours
Required Reading
→
Structs and Classes — The Codex

Struct vs. class declarations, value semantics vs. reference semantics, initializers, and choosing between them.

LESSON 10

Enums

A case can carry its own data — no separate subclasses needed

A Swift enum case can carry genuinely different associated data of its own — unlike most languages' plain named-constant enums — which is what lets a single enum model something like a network response's several distinct shapes without a separate class hierarchy at all.

📚 ⚡ Intermediate.
Key focus: How associated values let one enum model several genuinely different data shapes without subclassing.
⏱ 45 min
Required Reading
→
Enums — The Codex

Enum cases with associated values, raw values, computed properties on enums, and enums with methods.

LESSON 11

Collections

Array, Set, and Dictionary are all structs with copy-on-assignment

Because <code>Array</code>, <code>Set</code>, and <code>Dictionary</code> are all themselves structs, they inherit exactly the same copy-on-assignment guarantee every other Swift struct has — implemented efficiently under the hood through copy-on-write, so the actual copy only happens the moment one side is genuinely mutated.

📚 ⚡ Intermediate.
Key focus: Why collections being structs gives them the same copy guarantee, made efficient by copy-on-write.
⏱ 45 min
Required Reading
→
Collections — The Codex

Array, Set, and Dictionary basics, their struct-based value semantics, copy-on-write, and common collection operations.

LESSON 12

Pattern Matching

if case checks one pattern without needing a full switch

<code>if case</code> lets you check a single pattern — say, one specific enum case with associated values — without writing out a full <code>switch</code> and its other, irrelevant branches, which is genuinely useful exactly when only one case actually matters to the code at hand.

📚 ⚡ Intermediate.
Key focus: When if case is the right, lighter-weight tool instead of a full switch statement.
⏱ 30 min
Required Reading
→
Pattern Matching — The Codex

if case and guard case, pattern matching with associated values, where clauses in patterns, and tuple patterns.

LESSON 13

String Handling

str[0] doesn't even compile — a deliberate Unicode-correctness choice

Indexing a Swift string with a plain integer doesn't even compile, a deliberate design choice made because a single visible character (grapheme cluster) can genuinely be composed of multiple Unicode scalar values — so Swift forces you through its <code>String.Index</code> type rather than pretending integer indexing is safe when it silently isn't.

📚 ⚡ Intermediate.
Key focus: Why Swift refuses integer string indexing outright, rather than letting it quietly misbehave on real Unicode text.
⏱ 30 min
Required Reading
→
String Handling — The Codex

String as a collection of Characters, String.Index and why it exists, Unicode correctness, and common string operations.

LESSON 14

Extensions

Add a computed property to Int — but never a stored one

A Swift extension can add a genuinely new computed property, method, or even protocol conformance to an existing type like <code>Int</code> — including ones you don't own — but it can never add a stored property, a real structural limitation rooted in how existing instances' memory layout can't retroactively grow.

📚 ⚡ Intermediate.
Key focus: Why extensions can add computed properties and methods but never stored properties, and what that limitation stems from.
⏱ 30 min
Required Reading
→
Extensions — The Codex

Extension syntax, adding computed properties and methods to existing types, protocol conformance via extensions, and the stored-property limitation.

STAGE 3

Protocols, Generics and Access Control

Protocol-oriented programming, the generics machinery that makes it type-safe, controlling what a module exposes, and the automatic JSON handling built on top of all three. Four lessons.

LESSON 15

Access Control

public lets another module use your class — open is the separate, stronger level

<code>public</code> lets code in a different module use your class, but that alone is not enough to subclass or override it there — <code>open</code> is Swift's separate, deliberately stronger access level specifically required before another module can do that.

📚 🟩 Beginner → ⚡ Intermediate.
Key focus: The real distinction between public (usable elsewhere) and open (subclassable elsewhere).
⏱ 30 min
Required Reading
→
Access Control — The Codex

private, fileprivate, internal, public, and open, and the specific public-vs-open distinction for subclassing.

LESSON 16

Generics Deep Dive

associatedtype lets a protocol require a type without naming it

A protocol's <code>associatedtype</code> lets it require that conforming types provide some type, without the protocol itself ever naming what that type actually is — each conforming type fills in its own concrete answer, inferred automatically from how it's used, which is what makes protocols like <code>Collection</code> genuinely generic.

📚 ⚡ Intermediate → 🔥 Expert.
Key focus: How associatedtype lets a protocol stay generic over a type it never has to name itself.
⏱ 45 min
Required Reading
→
Generics Deep Dive — The Codex

Generic functions and types, associatedtype in protocols, where clauses for constraining generics, and generic subscripts.

LESSON 17

Codable and JSON

Conform a struct to Codable and the compiler writes the JSON logic automatically

Conforming a struct or class to <code>Codable</code> makes the compiler automatically synthesize its entire JSON encoding and decoding logic, matching property names to JSON keys — zero manual key-by-key mapping code required for the common case.

📚 🟩 Beginner → ⚡ Intermediate.
Key focus: How Codable's automatic synthesis eliminates manual JSON mapping code for the common case.
⏱ 30 min
Required Reading
→
Codable and JSON — The Codex

Codable, Encodable and Decodable, CodingKeys for custom key mapping, and JSONEncoder/JSONDecoder.

LESSON 18

Protocols and Generics, Tied Together

Conformance, default implementations, some/any, where clauses and Codable together

This lesson is the synthesis for the stage: protocol conformance, default implementations via protocol extensions, the <code>some</code> and <code>any</code> keywords for working with protocol types, generics with <code>where</code> clauses from the previous lesson, and <code>Codable</code> &mdash; Swift's protocol-oriented style seen as one coherent whole rather than separate features.

📚 ⚡ Intermediate.
Key focus: Seeing protocol conformance, default implementations, some/any, generics, and Codable work together as protocol-oriented Swift.
⏱ 1.25 hours
Required Reading
→
Protocols and Generics, Tied Together — The Codex

Protocol conformance and default implementations via extensions, the some vs. any keywords, generics with where clauses, and Codable, together.

STAGE 4

Concurrency and Memory

Swift's structured, actor-based concurrency model and the automatic reference counting underneath every class instance — including the one real, common way it can still leak memory. Four lessons.

LESSON 19

Async/Await

An async function can't just be called from sync code — Task{} is the real bridge

An <code>async</code> function cannot simply be called from ordinary synchronous code the way a regular function can; <code>Task { }</code> is the actual bridge that starts a new piece of asynchronous work running from synchronous context, which is worth understanding before async/await otherwise seems to mysteriously not compile.

📚 ⚡ Intermediate.
Key focus: Why Task{} — not a direct call — is required to start async work from synchronous code.
⏱ 45 min
Required Reading
→
Async/Await — The Codex

async function declarations, await, Task{} as the sync-to-async bridge, and structured concurrency basics.

LESSON 20

The Concurrency Model

An actor automatically serializes every access to its own state

A Swift <code>actor</code> automatically serializes every access to the state it owns, so two pieces of code can never race on the same actor's data — and that safety guarantee is exactly why reading even a simple property from outside the actor requires <code>await</code>, not a stylistic quirk.

📚 ⚡ Intermediate → 🔥 Expert.
Key focus: Why an actor's automatic serialization is what forces await even on a simple external property read.
⏱ 45 min
Required Reading
→
The Concurrency Model — The Codex

Actors and automatic state serialization, @MainActor, Sendable and safely crossing concurrency domains, and structured concurrency guarantees.

LESSON 21

Memory Management

Two classes with strong references to each other never get deallocated

Two class instances each holding a strong reference to the other create a retain cycle that ARC can never break on its own — not a bug in automatic reference counting, but genuinely its single most common real-world failure mode, and exactly what <code>weak</code> and <code>unowned</code> references exist to prevent.

📚 ⚡ Intermediate.
Key focus: Recognizing a retain cycle as ARC's normal behavior working correctly, and using weak/unowned to prevent it.
⏱ 45 min
Required Reading
→
Memory Management — The Codex

Automatic Reference Counting (ARC) basics, strong reference cycles, weak vs. unowned references, and closures capturing self.

LESSON 22

Concurrency and ARC, Tied Together

async/await, actors, Sendable, and ARC's retain cycles together

This lesson is the synthesis for the stage: async/await and <code>Task</code> from two lessons ago, actors and <code>@MainActor</code> for safe state access, <code>Sendable</code> for safely crossing concurrency domains, structured concurrency's guarantees, and ARC's memory model including the retain cycles the previous lesson covered &mdash; concurrency and memory management seen as the one connected system they actually are in real Swift code.

📚 ⚡ Intermediate → 🔥 Expert.
Key focus: Seeing async/await, actors, Sendable, and ARC's memory model work together as one connected system.
⏱ 1.25 hours
Required Reading
→
Concurrency and ARC, Tied Together — The Codex

async/await, Task, actors, @MainActor, Sendable, structured concurrency, ARC, weak/unowned references, and retain cycles, together.

STAGE 5

Apple Platforms, Tooling and Beyond

Property wrappers, the SwiftUI and Combine frameworks built on top of everything so far, reaching outside Swift entirely to Objective-C and the server, and the tooling that ties a real project together. Eight lessons to close out the course.

LESSON 23

Property Wrappers

@propertyWrapper is real and writable — @State isn't special compiler magic

<code>@propertyWrapper</code> is a genuine, ordinary language mechanism you can write yourself — SwiftUI's <code>@State</code> is not special, hidden compiler magic at all, just an ordinary struct that happens to use this exact same mechanism, which is worth knowing before SwiftUI's property wrappers otherwise feel like unexplainable magic.

📚 ⚡ Intermediate.
Key focus: Why @State is an ordinary use of a real language feature, not special-cased compiler behavior.
⏱ 45 min
Required Reading
→
Property Wrappers — The Codex

@propertyWrapper syntax, wrappedValue and projectedValue, writing a custom property wrapper, and how SwiftUI's wrappers use the same mechanism.

LESSON 24

SwiftUI Basics

A View's body describes the UI — it's redrawn automatically when state changes

A SwiftUI <code>View</code>'s <code>body</code> is not a UI-building function that runs once — it is a declarative description of what the UI should look like for the current state, and SwiftUI automatically redraws it whenever any state the body reads actually changes.

📚 ⚡ Intermediate.
Key focus: The describe-don't-build mental model, and how SwiftUI knows exactly when to redraw a view.
⏱ 45 min
Required Reading
→
SwiftUI Basics — The Codex

View protocol and body, @State and @Binding, basic layout views (VStack, HStack), and how state changes trigger automatic redraws.

LESSON 25

Combine

For a one-shot async result, async/await is now usually simpler

Combine models values that arrive over time as a genuine reactive stream — but for the common case of a single, one-shot asynchronous result, Swift's own <code>async</code>/<code>await</code> is now usually the simpler, more direct choice, which is worth knowing before reaching for Combine out of habit.

📚 ⚡ Intermediate → 🔥 Expert.
Key focus: When Combine's stream-over-time model genuinely earns its place over simpler async/await.
⏱ 45 min
Required Reading
→
Combine — The Codex

Publishers and Subscribers, common operators (map, filter, combineLatest), and when Combine fits better than plain async/await.

LESSON 26

The Standard Library

Result<Success, Failure> is just an enum — a typed alternative to throws

<code>Result&lt;Success, Failure&gt;</code> is genuinely just an enum with two cases, no hidden compiler magic involved, offering a real typed alternative to Swift's own <code>throws</code> mechanism for situations where you want to store, pass around, or defer handling of a fallible outcome.

📚 ⚡ Intermediate.
Key focus: Choosing Result's typed, storable outcome over throws' immediate-propagation model.
⏱ 30 min
Required Reading
→
The Standard Library — The Codex

Result<Success, Failure>, converting between Result and throws, and other standard library essentials (Comparable, Hashable, Equatable).

LESSON 27

Objective-C Interoperability

A struct or an enum with associated values can't be used from Objective-C at all

<code>@objc</code> cannot bridge everything: a Swift struct, or an enum carrying associated values, genuinely cannot be exposed to and used from Objective-C code at all, which matters directly for any codebase still mixing the two languages during a gradual migration.

📚 ⚡ Intermediate.
Key focus: The real, structural limits of @objc bridging, and which Swift types can never cross into Objective-C.
⏱ 30 min
Required Reading
→
Objective-C Interoperability — The Codex

@objc and @objcMembers, which Swift features can and can't bridge to Objective-C, and mixed-language project setup.

LESSON 28

Server-Side Swift

Vapor runs on the exact same toolchain that builds iOS apps

Vapor, a genuine server-side Swift framework, runs on the identical Swift toolchain used to build iOS apps — the same <code>async</code>/<code>await</code> and structured concurrency work exactly the same way on a Linux server as they do in an iOS app, with no separate server-flavored dialect of the language to learn.

📚 ⚡ Intermediate.
Key focus: Why Vapor needs no separate language dialect — the exact same Swift concurrency model applies on Linux.
⏱ 30 min
Required Reading
→
Server-Side Swift — The Codex

Vapor basics, routing and request handling, and how the same async/await model applies identically on the server.

LESSON 29

Swift Package Manager

Package.swift is written in actual, compiled Swift code

Unlike almost every other language's dependency manifest — typically JSON, YAML, or XML — <code>Package.swift</code> is itself real, compiled Swift code, which means it can use conditionals, variables, and any other language feature to describe a package's dependencies and targets.

📚 🟩 Beginner → ⚡ Intermediate.
Key focus: Why a Package.swift being real compiled Swift, not a data format, is a genuinely unusual manifest design.
⏱ 30 min
Required Reading
→
Swift Package Manager — The Codex

Package.swift structure, declaring dependencies and targets, and the executable-Swift-as-manifest design.

LESSON 30

Testing

@Test alone marks a function as a real test — no class or inheritance needed

Swift Testing needs no test class, no inheritance from a base test case, and no method name that starts with <code>test</code> — the <code>@Test</code> attribute alone is enough to mark any ordinary function as a real, runnable test, a genuinely lighter-weight model than XCTest's class-based approach.

📚 ⚡ Intermediate.
Key focus: How @Test's attribute-based model removes the class-and-naming-convention ceremony older frameworks required.
⏱ 30 min
Required Reading
→
Testing — The Codex

@Test and #expect in Swift Testing, comparison with XCTest's class-based model, and parameterized tests.

STAGE 6

Concepts That Cross Languages

These pages explain ideas that are not specific to Swift — they apply to every language you will ever learn. Read them once and the next language costs you far less effort. They pair well with the lessons above rather than replacing them.

STAGE 7

Build Real Things — 10 Projects

Reading is not enough. These 10 projects — five basic, five medium — are chosen because each one exercises something specific you learned above, and because they are genuinely idiomatic Swift rather than generic exercises. Each link below opens the project brief; the Swift implementation walkthrough is in progress.

BASIC — 5 PROJECTS
→
Number Guessing Game

readLine() input parsing, a guess loop, and Int.random(in:) for the secret number.

→
Temperature Converter

A switch over an enum for conversion direction, and string interpolation for output.

→
Word Counter

Reading a file with String(contentsOfFile:), and a [String: Int] dictionary for a frequency table.

→
To-Do List (CLI)

A Codable struct for the task shape, and JSONEncoder/JSONDecoder for persistence.

→
Password Generator

An OptionSet for character-set selection, and a review of Int.random(in:)'s suitability for real security.

MEDIUM — 5 PROJECTS
→
Web Scraper

URLSession's async/await APIs for real requests, and regular expressions for link extraction.

→
REST API

Vapor's routing for a real HTTP server, Codable for typed JSON responses, and an in-memory actor-backed store.

→
CLI Task Manager

An enum with associated values for subcommands, and a switch the compiler checks for exhaustiveness.

→
URL Shortener

An actor-backed store for thread-safe access, and Vapor serving the HTTP layer with structured concurrency.

→
Log Analyser

Parsing lines with Swift's regex literals, and a Dictionary-based counter for top-N aggregation.

STAGE 8

Practice & Experimentation

Ongoing, not a final step. Use these throughout the course — try every snippet you read, and run anything you are unsure about rather than assuming.

Resources
→
Swift Playground

Run Swift code without installing anything, via the official Swift Playground.

→
Swift Snippets

Copy-ready idiomatic patterns to keep beside you while you build.

→
Swift Reference Hub

All 30 topic pages in one index, for looking things up later.