Language cheatsheet
Swift 6 Cheatsheet
Swift tooling and syntax: optionals, structs and enums, protocols, closures, error handling, async/await, actors and SwiftUI basics.
Tooling
- Check the Swift version — Print the installed Swift toolchain version.
- Create a package — Create a new command-line project with Swift Package Manager.
- Build and run — Build the package, run it, and run its tests.
- Run a script file — Run a Swift source file without building a project.
- Add a dependency — Declare a package in Package.swift.
Basics and optionals
- Constants and variables — let is constant and var can change. Types are inferred.
- Handle optionals — Unwrap an optional safely with if let or guard let. ?? supplies a default.
- Arrays and dictionaries — Collections are value types and use generic syntax.
- Loops and switch — switch must be exhaustive and supports patterns and ranges.
- Define functions — Argument labels make calls read like sentences. _ removes a label.
- Closures — Pass behavior around. Trailing closure syntax reads cleanly.
Types and protocols
- Structs — Structs are value types with memberwise initializers. Mark mutating methods with mutating.
- Classes — Classes are reference types and support inheritance.
- Enums with associated values — Each case can carry its own data, and switch extracts it.
- Protocols and extensions — A protocol describes requirements, and an extension adds default behavior.
- Generics — Write a function once for any type that meets a constraint.
- Encode and decode JSON — Conform to Codable and use JSONEncoder and JSONDecoder.
Errors and concurrency
- Throw and catch errors — Mark functions that can fail with throws, call them with try, and handle errors in do/catch.
- Async functions — async functions can suspend. Call them with await.
- Run work in parallel — async let starts child tasks that run concurrently.
- Start a task — Task starts asynchronous work from synchronous code.
- Protect shared state with an actor — An actor processes one call at a time, which prevents data races. Call its methods with await.
- Update the UI on the main actor — Mark UI code @MainActor so it always runs on the main thread.
SwiftUI basics
- A simple view — A SwiftUI view returns its content from body. State changes re-render it.
- Show a list — List and ForEach build rows from identifiable data.
- Navigate between screens — NavigationStack with NavigationLink pushes detail screens.
- Bind a control to state — A $ prefix passes a two-way binding to a control.
- Load data when a view appears — The task modifier runs async work and cancels it when the view goes away.
Related cheatsheets
- C++ 23 cheatsheet — Compile and debug C++ with g++ and CMake, and use modern C++ (up to C++23): containers, algorithms, smart pointers, templates and threads.
- C# 14 cheatsheet — Modern C# 14 syntax for strings, nullability, collections, LINQ, records, pattern matching, async code and generics.
- CSS 3 cheatsheet — Layout with flexbox and grid, responsive design, custom properties, animations and modern selectors.
- Go 1 cheatsheet — The go command for modules, building, testing and cross-compiling, plus the code patterns you write every day.
- HTML 5 cheatsheet — HTML5 page structure, text, links, images, forms, tables and metadata, with accessible patterns.
- Java, Maven and Gradle 25 cheatsheet — JDK commands to compile, run and diagnose Java programs, plus the Maven and Gradle commands for builds and tests.