Swift Lab Examples

Scope: seventeen self-contained practice programs, one file each. Every demo is fully commented so you can read why and not only what: open it in the code viewer, download it, run it, break it, and change it.

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.

#DescriptionLink
1Hello, Swift: top-level code, interpolation, constantsView
2Variables and types: inference, integer widths, optionalsView
3Operators: arithmetic, ranges, identity versus equalityView
4Control flow: guard, exhaustive switch, labeled loopsView
5Functions: argument labels, inout, tuples, function valuesView

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.

#DescriptionLink
6Collections: array, dictionary, set, map/filter/reduceView
7Optionals: binding, chaining, map/flatMap, the enum underneathView
8Strings: grapheme clusters, indices, splitting, Unicode equalityView

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.

#DescriptionLink
9Structs, classes and inheritance: value versus reference semanticsView
10Enums: raw values, associated values, methods, recursive casesView
11Protocols: defaults via extensions, composition, associated typesView
12Generics: constraints, where clauses, overload resolutionView

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.

#DescriptionLink
13Closures: capture semantics, capture lists, escaping, higher-order useView
14Errors: throwing functions, do/catch, defer, Result, rethrowsView

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.

#DescriptionLink
15Memory and ARC: retain/release, cycles, weak, unowned, copy-on-writeView
16Concurrency: async/await, task groups, actors, cancellationView
17Access control: open to private, fileprivate helpers, module boundariesView

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:

  1. Predict the output first. Read the file, write down what you expect each print to produce, then run it. The surprise is the lesson.
  2. 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.
  3. Change one thing at a time. Swap let for var, change a constraint from Numeric to Comparable, replace a class with a struct.
  4. 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.