Glossary
106 terms from Swift Beyond the Basics, in plain words, each with the chapter that teaches it.
#
- #expect
- Checks in a test. `#expect` records a failure and carries on, while `try #require` stops the test, and also unwraps an optional.
- @concurrent
- A Swift 6.2 attribute put before an `async` function to run it off the main actor, which matters in targets that put all code on the main actor by default. Its arguments and result must be `Sendable`.
- @Observable
- An attached macro that makes a class's stored properties trackable. Code can then be told when a property it read changes.
- @ObservationIgnored
- A marker on a property of an `@Observable` class that stops it being tracked, so changes to it never trigger `onChange`.
- @testable import
- An import used in tests that also lets them reach a module's `internal` code, not only its `public` parts.
A
- access control
- Rules that decide which code can see a name. `public` is visible to any importing module, `internal` (the default) only inside the module, `fileprivate` only in the file, and `private` only in the type and its same-file extensions. In OrderKit, `quote` must be `public` so the `kc` tool can call it.
- actor
- A reference type that lets only one task use its state at a time. Outside code must `await` its methods. Kettle & Crane's `Outbox` and `FakeServer` are actors, so many tasks can use them without data races.
- ARC (automatic reference counting)
- Swift's way of freeing class instances. Each object counts the strong references to it and is freed when the count reaches zero.
- argument (command-line)
- The three kinds of command-line input in the `kc` tool. An argument is a plain value like `12`, an option is a name with a value like `--cafe "Bloom Café"`, and a flag is a name alone that turns something on, like `--single-origin`.
- associated type
- A placeholder type inside a protocol that each conforming type chooses. In `Store`, `Item` is `String` for `BeanStore` and `Mug` for a mug store.
- associated value
- Extra data attached to an enum case. `SyncError.rejected(id:)` carries the id of the rejected order.
- async let
- A Swift form that starts a call running alongside the code around it, so a fixed, known number of calls can run side by side. When the number of jobs isn't known in advance, a task group is the better choice.
- AsyncSequence
- A sequence whose items arrive over time, so a `for await` loop waits for each one. A task group is an AsyncSequence of its children's results.
- attached macro
- A macro written with `@` in front of a declaration, which adds code to that declaration. `@Test`, `@Suite` and `@Observable` are attached macros.
- await
- The keyword that marks a place where code may pause to wait for async work or for an actor. Outside code needs it to call an actor's methods or read its properties.
C
- cancellation
- Asking a task to stop. It is cooperative: `cancel()` only sets a flag, and the task must check it (for example with `Task.isCancelled`) or sleep, which throws `CancellationError`.
- capture
- A closure keeping a reference to the variables it uses, not a copy. A capture list in square brackets, like `[count]` or `[weak self]`, changes how: `[count]` copies the value at that moment.
- child task
- A task started inside a task group or by `async let`. Its parent waits for it to finish, and its errors arrive in the parent.
- class
- A type whose instances are shared: assigning one gives a second name for the same object. Choose it for one shared object with identity, such as a café's `Tab`.
- Codable
- Two protocols in one, `Encodable & Decodable`. A type that conforms can be turned into data such as JSON and built back from it.
- Comparable
- A standard protocol for types that can be ordered. You write the `<` operator and Swift builds `>`, `<=`, `sorted()`, `min()` and `max()` from it.
- compiler plug-in
- A separate program the compiler runs while building your code to expand a macro. It is built on swift-syntax.
- completion order
- The order in which tasks finish, which is the order a task group hands back results. The espresso, the quickest brew, arrives first.
- conditional conformance
- Conforming to a protocol only when a type parameter does too. For example, `Queue: Equatable where Element: Equatable`.
- constraint
- A rule on a type parameter, such as `T: Comparable`. A `where` clause can add constraints to functions, extensions and conformances.
- copy-on-write
- Copies of an array, dictionary or string share one block of storage until one of them changes, which makes passing collections cheap.
D
- Data
- Foundation's type for raw bytes, the form that files and networks use. `JSONEncoder` produces `Data`, and `String(decoding:as:)` reads it as text.
- data race
- Two tasks touching the same value at the same time, with at least one changing it. If two tasks both read 4 and write 5, a bag is lost from Jo's stock counter.
- default implementation
- A method or property body in a protocol extension that conforming types get for free. A type may write its own version, but a call through `any` uses it only if the member is also listed in the protocol's requirements.
- defer
- A block that runs when the current scope ends, however it ends: at the closing brace, a `return` or a thrown error. The outbox uses it to reset `isSyncing`.
- deinitializer
- Code in a class, written `deinit`, that runs just before ARC frees an object. Printing from it shows exactly when the object goes. Structs have none.
- dependency
- Another package that yours uses, listed in `Package.swift` with its Git address (such as a GitHub URL) and a version, like swift-argument-parser.
- display name
- The optional text in `@Test("Ten bags get 10% off")`, shown in test output instead of the function name.
E
- Equatable
- A standard protocol for types whose values can be compared with `==`. Comparable includes it, and a type with Equatable parts can be made Equatable, as `SyncReport` is so tests can compare reports.
- escaping closure
- A closure kept after the function it was passed to returns, for example stored in a property. Swift requires the parameter to be marked `@escaping`.
F
- failable initializer
- An initializer written `init?` that returns `nil` for input it can't accept, so the caller gets an optional. A rep typing `lots` for a bag count gets `nil`.
- fake
- A stand-in that follows the same protocol as the real service. `FakeServer` lets you test syncing offline, online and rejected cases without a network.
- FileManager
- Foundation's tool for working with files and folders. `FileManager.default.temporaryDirectory` is the system's temporary folder, and `removeItem(at:)` deletes a file.
- final
- A keyword meaning no class may inherit from this class, or no subclass may override this method.
- Foundation
- Apple's standard library of everyday types, such as `URL`, `Data`, `JSONEncoder` and `FileManager`, loaded with `import Foundation`.
- freestanding macro
- A macro written with `#` that stands on its own where an expression or statement could go, such as `#expect`, `#warning` (compiler warning, program still runs) or `#error` (stops the build).
G
- generic function
- Code with a type parameter such as `<T>`, written once to work for many types. `largest` works for arrays of `Int`, `Double` or `String`, and `Queue<Element>` is a generic type.
H
- Hashable
- A standard protocol that lets a type be a dictionary key or go in a `Set`. Swift writes it for you if every property is hashable.
I
- Identifiable
- A standard protocol that requires one property, `id`, different for every value. SwiftUI lists use it to keep track of rows.
- identity
- Being the same object. `===` is true when two references point at one object, and `!==` when they don't, unlike `==`, which compares values.
- inheritance
- Building one class on another. The subclass gets the superclass's properties and methods, can replace methods with `override`, and reaches the superclass's version with `super`. `WholesaleCustomer` is a subclass of `Customer`.
- inout parameter
- A parameter whose changes reach the caller's variable. The caller passes it with `&`, as in `restock(&decaf, bags: 5)`.
- isolated
- Owned by an actor, so outside code can reach it only with `await`. A `nonisolated` member touches none of the actor's state and needs no `await`.
J
- JSON
- The text format apps and servers use to exchange data. `JSONEncoder` turns a Codable value into JSON and `JSONDecoder` builds it back.
L
- lazy property
- A property marked `lazy var` that is created the first time it's used, not in `init`. That lets it use other properties, such as `title` using `cafe`.
M
- macro
- Code that writes code at compile time. Macros only add to what you wrote, and the compiler checks the added code like your own.
- macro expansion
- The code a macro adds. Xcode's Editor > Expand Macro or the `-dump-macro-expansions` compiler option shows it.
- main actor
- The built-in actor that runs user-interface code. Mark code `@MainActor` to keep it there, and other tasks call it with `await`.
- metatype
- A type used as a value. `[WholesaleOrder].self` passes the type itself to `load`, whose parameter has type `T.Type`.
- module
- A unit of code that Swift compiles together and others load with `import`, like `Foundation`. Each target in a package builds one module.
- mutating
- Marks a struct method, or a protocol requirement, that changes the value. Classes don't need it.
N
- nonisolated
- Marks an actor method that touches none of the actor's state, so callers can use it without `await`.
O
- Observation
- The framework behind `@Observable`. `withObservationTracking` remembers which properties its first closure read, then calls `onChange` once just before one of them changes.
- offline-first
- A design where orders are saved on the device and sent when a connection returns. Kettle & Crane's tablet `Outbox` saves orders to a JSON file at once so they survive the app closing.
- opaque type
- `some Protocol`: one specific, fixed type that conforms to the protocol, known to Swift but hidden from the caller. As a parameter it is shorthand for a generic function.
- OrderServer
- A protocol for any server that can receive orders, with one `send` method that uses typed throws to report only `SyncError` values. Code written against it can swap a fake server for a real one without changes.
P
- Package.resolved
- The file that records the exact dependency versions a build chose. Commit it so everyone builds the same versions.
- Package.swift
- A package's manifest file, listing its name, platforms, products, dependencies and targets. The `swift-tools-version` comment on the first line says which Swift version it needs; 6 or later turns on Swift 6 mode.
- parameterized test
- One test run once per value in `@Test(arguments:)`. A failure names the value that caused it.
- ParsableCommand
- swift-argument-parser's protocol for a command. Its `@Argument`, `@Option` and `@Flag` properties are filled from the command line, then `run()` is called.
- product
- What a package offers to the outside world: a library other packages can use, or a program people run.
- property observer
- Code that runs when a stored property changes. `didSet` runs right after the change, and inside it `oldValue` is the value before.
- protocol composition
- `A & B`: a type that must conform to both protocols, such as `Named & Shippable`.
- protocol extension
- An extension on a protocol that adds shared code, such as default implementations, to every conforming type.
- protocol inheritance
- A protocol that includes another's requirements, so conforming to `Shippable` also means meeting everything in `Priced`.
- protocol-oriented design
- Describing each ability with a small protocol and sharing code through protocol extensions instead of base classes. A type picks up only the abilities it needs.
Q
- queue
- A line where items leave in the order they arrived (first in, first out). A generic `Queue<Element>` can hold any element type, such as orders waiting on a tablet to be sent.
R
- reduce
- A collection method that combines all items into one value, given a starting value and a closure: `$0` is the running total and `$1` the next item.
- reentrancy
- While an actor's method waits at an `await`, other calls can run on the actor, so its state may change in between. Set a flag before the first `await` to guard against double syncs.
- Result
- A standard enum with two cases, `.success(value)` and `.failure(error)`. It lets a task group report every order, whether it succeeded or failed.
- result builder
- A type marked `@resultBuilder` that turns a block of plain statements into one value by calling its static `build…` methods. SwiftUI builds screens this way, and Alex's packing slip uses `LinesBuilder`.
- retain cycle
- Objects holding strong references to each other, so their counts never reach zero and ARC never frees them. A café and its tab can do this unless one link is `weak`.
S
- Self
- Inside a protocol, the type that conforms to it. For `Mug`, `Self` is `Mug`.
- semantic versioning
- Version numbers as major.minor.patch. A patch fixes bugs, a minor adds features, and a new major may break code, so `from: "1.5.0"` allows any 1.x from 1.5.0 but never 2.0.
- Sendable
- A type that is safe to hand to another task or actor because the two can't race on it. Value types of Sendable parts, actors, and final classes with only `let` properties qualify.
- some vs any
- `some Store` is one fixed hidden type, while `any Store` is a box that may hold any conforming type, even a mix in one array. Prefer `some` and generics unless you truly need a mix.
- starter test
- The empty test that `swift package init` writes, ready to fill in.
- static
- A property or method that belongs to the type itself rather than to each instance, used through the type's name, like `Mug.sample`.
- strong reference
- The default kind of reference to a class instance. Each one adds to the object's count, keeping it alive.
- structured concurrency
- Every job you start belongs to the code that started it, and that code doesn't finish until all its child tasks have. Nothing is left running by accident, and child errors reach the parent.
- suite
- A type that groups related tests, marked `@Suite`. Each test gets a fresh instance, so tests can't spoil each other's data.
- Swift 6 language mode
- The compiler setting that checks for data races when you build. A package turns it on with tools version 6 or later, and a single file with `-swift-version 6`.
- swift build
- Command-line tools for packages: `swift build` compiles, `swift run` runs a program, `swift test` runs tests, `swift package init` creates a package, and `swift package update` moves to the newest allowed dependency versions.
- Swift Testing
- The testing library built into Swift 6, loaded with `import Testing`, where tests are functions marked `@Test`. XCTest is the older framework still found in many projects.
- swift-argument-parser
- Apple's package for reading command-line input. It builds the `kc` tool's parsing and `--help` output for you.
- swift-syntax
- The large package that a macro's compiler plug-in is built on, which presents your code as a tree of Swift syntax.
- SwiftUI
- Apple's framework for building app screens in Swift. It relies on features such as result builders, `@Observable` and the main actor.
T
- target
- One part of a package that builds a single module: a library, an executable program or tests (a test target).
- task group
- A group of child tasks run side by side, for any number of jobs. You add children with `addTask` and read results with `for await`, in completion order.
- Task.sleep
- A function that pauses the current task for a given time. It throws a `CancellationError` if the task is canceled, so `try?` is used where the caller can't throw that error.
- try?
- Runs a throwing call and gives `nil` instead of an error if it throws, turning the result into an optional. It is handy for a missing file (`try? Data(contentsOf:)`) or for ignoring a canceled sleep.
- type parameter
- The placeholder in `<T>` or `Queue<Element>`, filled in by the caller.
- typealias
- A second name for a type, like `typealias Product = Named & Shippable`.
- typed throws
- Writing `throws(SyncError)` to name the one error type a function throws. A `catch` then gets that exact type, so a `switch` can cover every case without `default`.
U
- unowned
- A reference that doesn't keep an object alive and isn't optional. It crashes if the object is gone, so use it only when the other object always outlives this one.
- unstructured task
- A task started with `Task { }` that belongs to no parent. Keep it in a constant to wait for `task.value` or call `cancel()`.
- URL
- The type that names a file or web address. `URLSession` is Foundation's networking API, suggested for a real server type.
- UUID
- A random identifier, different every time, such as `E621E1F8-…`. The tests use `UUID().uuidString` to give each test its own file.
V
- value semantics
- Each copy is independent, so changing one leaves the others alone. Structs, enums, strings and collections work this way.
- variadic parameter
- A parameter that takes any number of values, written `String...`; the function sees them as an array. `buildBlock` uses one.
W
- weak reference
- A reference that doesn't keep an object alive and becomes `nil` when the object is freed, so it must be an optional `var`. It breaks retain cycles, such as a tab's link back to its café.