Swift Lab Examples
Each file is a complete Swift script: statements at the top level run in order, so there is no project to configure and nothing to import beyond the standard library and Foundation. The demos follow the lesson order — Phase 1 syntax through Phase 4 memory and concurrency — and each one ends with a working example whose output you can predict before you run it.
# Run a demo directly — compilation and execution in one step
swift 01_hello.swift
# Or compile it first, then run the binary (faster on repeated runs)
swiftc -O 01_hello.swift -o demo01
./demo01
# Demo 16 uses top-level await, which needs Swift 5.7 or newer
swift --version
The References & Playgrounds page lists online playgrounds for the cases where you cannot install a toolchain, and the Study Projects page continues with larger programs.
Foundations
Constants, variables, types, operators and control flow. These five files are deliberately small:
they establish the habits — let before var, explicit conversion, exhaustive
switch — that the later demos build on.
| # | Description | Link |
|---|---|---|
| 1 | Hello, Swift: top-level code, interpolation, constants | View |
| 2 | Variables and types: inference, integer widths, optionals | View |
| 3 | Operators: arithmetic, ranges, identity versus equality | View |
| 4 | Control flow: guard, exhaustive switch, labeled loops | View |
| 5 | Functions: argument labels, inout, tuples, function values | View |
Collections, Optionals & Text
The three collections, then the two types that cause the most confusion for newcomers: optionals, which the compiler forces you to unwrap honestly, and strings, whose indices are not integers.
| # | Description | Link |
|---|---|---|
| 6 | Collections: array, dictionary, set, map/filter/reduce | View |
| 7 | Optionals: binding, chaining, map/flatMap, the enum underneath | View |
| 8 | Strings: grapheme clusters, indices, splitting, Unicode equality | View |
Objects, Protocols & Generics
The modelling half of the language: when to choose a value type over a reference type, how enums carry data per case, how protocols replace class hierarchies, and how generics specialise code without duplicating it.
| # | Description | Link |
|---|---|---|
| 9 | Structs, classes and inheritance: value versus reference semantics | View |
| 10 | Enums: raw values, associated values, methods, recursive cases | View |
| 11 | Protocols: defaults via extensions, composition, associated types | View |
| 12 | Generics: constraints, where clauses, overload resolution | View |
Closures & Errors
Two topics that meet in almost every real API: closures because every callback, comparison and completion handler is one, and errors because Swift refuses to let a failure be ignored silently.
| # | Description | Link |
|---|---|---|
| 13 | Closures: capture semantics, capture lists, escaping, higher-order use | View |
| 14 | Errors: throwing functions, do/catch, defer, Result, rethrows | View |
Advanced
The Phase 4 material as runnable code. Demo 15 prints its own deinit lines so you can
watch reference counting — and watch a cycle leak. Demo 16 turns the same ownership question into
concurrency with tasks and actors. Demo 17 closes the loop with access control, which is how a set
of files becomes a module with a deliberate public surface.
| # | Description | Link |
|---|---|---|
| 15 | Memory and ARC: retain/release, cycles, weak, unowned, copy-on-write | View |
| 16 | Concurrency: async/await, task groups, actors, cancellation | View |
| 17 | Access control: open to private, fileprivate helpers, module boundaries | View |
How to Study These Demos
Reading a demo is not studying it. The files are ordered so that each one only uses features introduced earlier, which makes this sequence work:
- Predict the output first. Read the file, write down what you expect each
printto produce, then run it. The surprise is the lesson. - Break it on purpose. Delete a
guard, force-unwrap an optional that is nil, or remove[weak self]from demo 15. Compiler errors are the best documentation Swift has. - Change one thing at a time. Swap
letforvar, change a constraint fromNumerictoComparable, replace a class with a struct. - Rewrite from memory. Close the file and reproduce the smallest version of it that still compiles.
When a file stops being enough, move to the Study Projects page: the same language, but the programs are large enough to need structure — algorithms, data structures, file and network code.