Interactive tutorial

Learn Swift

A hands-on introduction, one question at a time. Modeled on the official Swift documentation and the rhythm of CS61A and CS61B: a short idea, a small program, then a question about what it will do before you run it.

Follows CS61A (Fall 2026) and CS61B (Spring 2026) lecture by lecture, with Swift from The Swift Programming Language (Swift 6.4) and the official documentation.

How a lesson works

Each lesson reads like a CS61A lecture: short notes and small programs, then questions. “What would Swift print?” asks you to predict output (type Error if the program would fail); Parsons problems ask you to put shuffled lines back in order. Each lesson ends with a lab to write and run on your own machine, with solutions you can open when you are ready.

Unit 1

Building abstractions with functions

Expressions, names, and functions; control; functions that take and return functions; and recursion. CS61A’s first four weeks.

  1. 01Getting started

    What happens between typing code and seeing output?CS61A · Welcome

    Available
  2. 02Names and functions

    What does it mean to give a computation a name?CS61A · Functions

    Available
  3. 03Control

    How does a program choose what to do next?CS61A · Control

    Available
  4. 04Higher-order functions

    What changes when functions become values?CS61A · Higher-Order Functions

    Available
  5. 05Environments and closures

    Where does a name get its value?CS61A · Environments

    Available
  6. 06Functional abstraction

    What should a function promise, and what should it hide?CS61A · Abstraction · Function Examples

    Available
  7. 07Recursion

    How can a function trust itself before it is finished?CS61A · Recursion

    Available
  8. 08Tree recursion

    What happens when one call makes two?CS61A · Tree Recursion

    Available

Discussions, homework, and projects

  1. Discussion 0Getting startedGetting startedAvailable
  2. Discussion 1Control and environment diagramsNames and functions · ControlAvailable
  3. Homework 1Functions and controlNames and functions · ControlAvailable
  4. Discussion 2Higher-order functionsHigher-order functions · Environments and closuresAvailable
  5. Homework 2Higher-order functionsHigher-order functions · Environments and closures · Functional abstractionAvailable
  6. Project 1HogNames and functions · Control · Higher-order functions · Environments and closures · Functional abstractionAvailable
  7. Discussion 3RecursionRecursionAvailable
  8. Discussion 4Tree recursionTree recursionAvailable
  9. Homework 3RecursionRecursion · Tree recursionAvailable

Unit 2

Building abstractions with data

Sequences, containers, optionals, your own types, recursive data, mutation, classes, protocols, lazy sequences, and efficiency. CS61A weeks five to nine.

  1. 09Sequences

    What can you do with anything that comes in order?CS61A · Sequences

    Available
  2. 10Dictionaries, sets, and tuples

    Which container fits which question?CS61A · Containers

    Available
  3. 11Optionals

    How do you represent “there might be nothing here”?CS61A · Containers (Swift-specific)

    Available
  4. 12Structures and enumerations

    How do you make invalid states impossible to write?CS61A · Objects

    Available
  5. 13Linked lists and trees

    How can a type contain itself?CS61A · Linked Lists · Trees

    Available
  6. 14Mutation and value semantics

    When does changing one thing change another?CS61A · Mutation

    Available
  7. 15Classes and inheritance

    When is sharing an object what you want?CS61A · Classes · Inheritance

    Available
  8. 16Protocols and generics

    How do you write code for types that do not exist yet?CS61A · Object Examples (interfaces)

    Available
  9. 17Iterators and lazy sequences

    Can you work with a sequence that never ends?CS61A · Lazy Evaluation · Generators

    Available
  10. 18Efficiency and memoization

    Why does the same answer sometimes take so much longer?CS61A · Efficiency

    Available

Discussions, homework, and projects

  1. Homework 4Sequences and containersSequences · Dictionaries, sets, and tuples · OptionalsAvailable
  2. Project 2CatsSequences · Dictionaries, sets, and tuples · Optionals · Higher-order functions · Tree recursion · Efficiency and memoizationAvailable
  3. Discussion 5TreesLinked lists and treesAvailable
  4. Homework 5Recursive data and mutationStructures and enumerations · Linked lists and trees · Mutation and value semanticsAvailable
  5. Discussion 6Objects and classesStructures and enumerations · Mutation and value semantics · Classes and inheritanceAvailable
  6. Homework 6Classes and interfacesClasses and inheritance · Protocols and genericsAvailable
  7. Project 3Ants vs. SomeBeesStructures and enumerations · Classes and inheritance · Protocols and genericsAvailable
  8. Discussion 7Iterators and efficiencyIterators and lazy sequences · Efficiency and memoizationAvailable
  9. Homework 7Lazy sequences and efficiencyIterators and lazy sequences · Efficiency and memoizationAvailable

Unit 3

Programs that meet the world

Algebraic data types, interpreters, errors, concurrency, testing, and packages. CS61A’s last weeks, with Swift in place of Gleam and SQL.

  1. 19Algebraic data types and pattern matching

    What if the compiler checked every case for you?CS61A · Functional Programming · Algebraic Data Types · Immutable Data

    Available
  2. 20Writing an interpreter

    What does it take to run a language?CS61A · Interpreters

    Available
  3. 21Error handling

    What should a function do when it cannot do its job?CS61A · Debugging

    Available
  4. 22Concurrency with async and await

    How do you wait without stopping everything?CS61A · Browsers · Applications

    Available
  5. 23Testing

    How do you know your program is right?CS61A · Software Testing · Software Tracing

    Available
  6. 24Building a Swift package

    How does code become something other people can use?CS61A · Applications · Conclusion

    Available

Discussions, homework, and projects

  1. Discussion 8Algebraic data typesAlgebraic data types and pattern matchingAvailable
  2. Homework 8Pattern matchingAlgebraic data types and pattern matchingAvailable
  3. Discussion 9InterpretersWriting an interpreterAvailable
  4. Homework 9Interpreters and errorsWriting an interpreter · Error handlingAvailable
  5. Project 4SchemeEnvironments and closures · Linked lists and trees · Algebraic data types and pattern matching · Writing an interpreter · Error handlingAvailable
  6. Discussion 10ConcurrencyConcurrency with async and awaitAvailable
  7. Discussion 11TestingTestingAvailable
  8. Homework 10Concurrency and testingConcurrency with async and await · TestingAvailable

Unit 4

Building data structures

References and memory, linked lists and resizing arrays built from scratch, inheritance and interfaces, iteration and equality, and SwiftUI for drawing. CS61B’s first five weeks.

  1. 25References, identity, and memory

    When two names refer to one thing, who owns it?CS61B · Intro to Java · Defining and Using Classes · Lists, Arrays, Maps, References

    Available
  2. 26Drawing with SwiftUI

    How does a program draw a world that keeps changing?CS61B · For Project 0: Particle Simulator

    Available
  3. 27Testing data structures

    How do you test code that holds a million things?CS61B · Testing · Packages

    Available
  4. 28IntLists and SLLists

    Why hide a list’s nodes from the people who use it?CS61B · Recursion, IntLists · SLLists, Nested Classes, Sentinel Nodes

    Available
  5. 29Doubly linked lists and deques

    How do you walk a list backward without a cycle of ownership?CS61B · DLLists

    Available
  6. 30Arrays from scratch

    How can adding to an array be fast on average?CS61B · Basic ALists · Resizing and Circular Arrays

    Available
  7. 31Interface and implementation inheritance

    Which method runs when two types both define it?CS61B · Interface and Implementation Inheritance · Subtype Polymorphism

    Available
  8. 32Iteration, equality, and ordering

    What does your type need to work with for-in, ==, and sorted()?CS61B · Iterators, Iterables, and Equals · Comparables and Comparators

    Available
  9. 33Copy-on-write collections

    How can a copy be free until someone changes it?CS61B · Swift addition: value semantics for collections

    Available

Discussions, homework, and projects

  1. Discussion 12Swift reviewReferences, identity, and memoryAvailable
  2. Discussion 13References, classes, and arraysReferences, identity, and memoryAvailable
  3. Homework 11Swift warm-upReferences, identity, and memoryAvailable
  4. Project 5Particle SimulatorReferences, identity, and memory · Drawing with SwiftUIAvailable
  5. Discussion 14IntLists and SLListsIntLists and SLListsAvailable
  6. Homework 12IntListsTesting data structures · IntLists and SLListsAvailable
  7. Project 6LinkedListDequeTesting data structures · IntLists and SLLists · Doubly linked lists and dequesAvailable
  8. Discussion 15DLLists, default implementations, and array listsDoubly linked lists and deques · Arrays from scratch · Interface and implementation inheritanceAvailable
  9. Project 7ArrayDequeArrays from scratch · Interface and implementation inheritance · Iteration, equality, and ordering · Copy-on-write collectionsAvailable
  10. Discussion 16Comparables and iteratorsIteration, equality, and orderingAvailable
  11. Homework 13Review IReferences, identity, and memory · IntLists and SLLists · Doubly linked lists and deques · Arrays from scratch · Interface and implementation inheritance · Iteration, equality, and orderingAvailable

Unit 5

Analysis and search trees

Measuring algorithms with asymptotic analysis, disjoint sets, and the trees behind sets and maps: binary search trees, B-trees, red-black trees, and heaps. CS61B’s weeks 5 to 8.

  1. 34Asymptotic analysis

    How do you compare algorithms without a stopwatch?CS61B · Asymptotics I

    Available
  2. 35Disjoint sets

    Are these two things connected?CS61B · Disjoint Sets

    Available
  3. 36Analyzing recursion

    How much work does a function that calls itself do?CS61B · Asymptotics II

    Available
  4. 37Amortized analysis and the limits of big O

    When is an occasional slow step fine?CS61B · Asymptotics III

    Available
  5. 38Sets, maps, and binary search trees

    How do you find a key without looking at every one?CS61B · Sets, Extends, and BSTs

    Available
  6. 39B-trees

    How can a tree stay balanced whatever order things arrive in?CS61B · B-Trees

    Available
  7. 40Left-leaning red-black trees

    Can a binary tree pretend to be a B-tree?CS61B · Red-Black Trees

    Available
  8. 41Heaps and priority queues

    What is the fastest way to always know the smallest item?CS61B · Heaps, Priority Queues, and Tree Representations

    Available

Discussions, homework, and projects

  1. Discussion 17Asymptotics and disjoint setsAsymptotic analysis · Disjoint setsAvailable
  2. Project 8PercolationTesting data structures · Drawing with SwiftUI · Disjoint setsAvailable
  3. Discussion 18Asymptotics IIAnalyzing recursion · Amortized analysis and the limits of big OAvailable
  4. Homework 14Asymptotic analysisAsymptotic analysis · Analyzing recursion · Amortized analysis and the limits of big OAvailable
  5. Discussion 19BSTs, B-trees, and LLRBsSets, maps, and binary search trees · B-trees · Left-leaning red-black treesAvailable
  6. Homework 15BSTMapIteration, equality, and ordering · Sets, maps, and binary search treesAvailable

Unit 6

Graphs

SwiftUI for data-driven apps, tree and graph traversals, shortest paths, minimum spanning trees, and reductions, with a project that charts the history of words and explores WordNet. CS61B’s weeks 8 to 10.

  1. 42Data-driven apps with SwiftUI

    How does a view keep up with data that changes?CS61B · For Project 4: NGordNet

    Available
  2. 43Tree and graph traversals

    In what order should you visit everything?CS61B · Tree and Graph Traversals

    Available
  3. 44Graph implementations

    How should a program store a web of connections?CS61B · Graph Traversals and Implementations

    Available
  4. 45Shortest paths: Dijkstra and A*

    What is the cheapest way from here to there?CS61B · Shortest Paths

    Available
  5. 46Minimum spanning trees

    How do you connect everything as cheaply as possible?CS61B · Minimum Spanning Trees

    Available
  6. 47DAGs, topological sort, and reductions

    How can one problem be solved by turning it into another?CS61B · Directed Acyclic Graphs

    Available

Discussions, homework, and projects

  1. Discussion 20Tree traversals and graphsTree and graph traversals · Graph implementationsAvailable
  2. Project 9NGordNetSets, maps, and binary search trees · Data-driven apps with SwiftUI · Tree and graph traversals · Graph implementationsAvailable
  3. Discussion 21Shortest paths and MSTsShortest paths: Dijkstra and A* · Minimum spanning trees · DAGs, topological sort, and reductionsAvailable

Unit 7

Hashing, sorting, and software engineering

Hash tables and tries, designing large programs, the major sorting algorithms and the limits of sorting, compression, and the responsibilities that come with computing, finishing with a game in SwiftUI. CS61B’s last five weeks.

  1. 48Hash tables

    How can a lookup take the same time for ten items or ten million?CS61B · Hashing I

    Available
  2. 49Hash functions

    What makes a hash function good?CS61B · Hashing II

    Available
  3. 50Tries and prefix operations

    How do you find every word that starts with “ca”?CS61B · Tries

    Available
  4. 51Software engineering: managing complexity

    Why do large programs become hard to change?CS61B · Software Engineering I

    Available
  5. 52Games with SwiftUI

    How does a game listen, update, and remember?CS61B · For Project 5: Build Your Own World

    Available
  6. 53Designing a world

    How do you plan a program too big to hold in your head?CS61B · Software Engineering II · BYOW Warmup

    Available
  7. 54Basic sorts

    How many ways are there to put things in order?CS61B · Basic Sorts · Insertion Sort

    Available
  8. 55Quicksort and selection

    Why is the sort with the worst worst case so often the fastest?CS61B · Quicksort

    Available
  9. 56The limits of sorting

    Can any comparison sort beat n log n?CS61B · Sorting and Algorithmic Bounds

    Available
  10. 57Radix sorts

    How can you sort without comparing?CS61B · Radix Sorts

    Available
  11. 58Compression and complexity

    How small can a file get?CS61B · Compression

    Available
  12. 59Computing, society, and what comes next

    What do we owe the people who use our software?CS61B · Social Implications of Computing · AI in Software Engineering · Summary

    Available

Discussions, homework, and projects

  1. Discussion 22Hashing and triesHash tables · Hash functions · Tries and prefix operationsAvailable
  2. Homework 16Review IISets, maps, and binary search trees · Heaps and priority queues · Tree and graph traversals · Shortest paths: Dijkstra and A* · Hash tables · Tries and prefix operationsAvailable
  3. Discussion 23Hashing and tries, revisitedHash tables · Hash functions · Tries and prefix operationsAvailable
  4. Project 10Build Your Own WorldDisjoint sets · Tree and graph traversals · Software engineering: managing complexity · Games with SwiftUI · Designing a worldAvailable
  5. Discussion 24SortingBasic sorts · Quicksort and selectionAvailable
  6. Homework 17HashMapHash tables · Hash functionsAvailable
  7. Discussion 25Sorting IIThe limits of sorting · Radix sortsAvailable
  8. Homework 18LLRB setLeft-leaning red-black treesAvailable
  9. Homework 19Final reviewHeaps and priority queues · Shortest paths: Dijkstra and A* · Minimum spanning trees · Hash tables · Quicksort and selection · The limits of sorting · Radix sorts · Compression and complexityAvailable