Skip to content

Swift Basics

Glossary

127 terms from Swift Basics, in plain words, each with the chapter that teaches it.

#

$0
A ready-made name for a closure's first parameter (`$1` is the second), so short closures like `{ $0 * 2 }` need no named parameters.

A

allSatisfy
A collection method that returns `true` only if every item passes a test, such as checking that every character of a card number is a digit.
argument
The actual value you pass into a function call, such as `2` and `4.75` in `lineTotal(quantity: 2, price: 4.75)`. Arguments fill the function's parameters.
argument label
The name written before an argument in a call, like `quantity:` or `terminator:`, which makes calls read like sentences. A `_` before a parameter removes its label.
array
An ordered collection of values of one type, written in square brackets like `["lat", "esp"]`. Items are read by their index.
assignment
Storing a value under a name with the `=` sign, such as `bagsLeft = 18`. It is different from `==`, which compares.
associated value
Extra data attached to an enum case, like the last four digits in `.card(last4: "4242")`. You read it with a `switch` and `let`.
async function
A function marked `async` that can pause while it waits, such as for a drink to brew, without freezing the program. You call it with `await`.
async let
Starts an async call and lets the code carry on immediately, so several jobs, like brewing the latte and the espresso, run side by side. You `await` each result later when you need it.
await
The keyword that marks where an async call may pause until its result is ready. If the call can also throw, you write `try await`.

B

block
A group of lines between braces `{ }`, such as the code that runs for an `if` or a loop.
Bool
A type that holds only `true` or `false`. Comparisons give a `Bool`.
break
A keyword that leaves a loop immediately, as when the register stops asking once a valid quantity is typed. Inside a `switch` it only leaves the `switch`.
bug
A mistake in a program. Debugging means finding and fixing it.

C

camelCase
A naming style with a small first letter and a capital for each new word, like `bagsLeft`.
case
One of the options in a `switch` or one of the fixed values of an enum, such as `.small` in `Size`.
case sensitive
Capital and small letters count as different, so `print` works but `Print` gives an error.
CaseIterable
A standard protocol that makes Swift build `allCases`, an array of every case of an enum. `Size.allCases` lists small, medium and large.
catch
The block after `do` that handles an error thrown inside it. A plain `catch` receives the error as a constant named `error`.
cd
A terminal command that moves you into a folder, such as `cd swift-basics`.
Character
A type that holds a single letter or symbol.
class
A type like a struct, but a reference type: assigning it to another name shares one object instead of copying. It needs its own `init`.
closure
A piece of code in braces that you can store in a constant or pass to a function, like a rule such as `{ $0 * 0.9 }` for taking 10% off.
command
One instruction typed into a terminal and run when you press Enter. It can also mean an instruction typed at a program's own prompt, such as an order entered at a register.
comment
A note for people that Swift ignores, starting with `//` or placed between `/*` and `*/`.
Comparable
A standard protocol that makes `<` work for a type, so it can be sorted with `sorted()` and `max()`. You write the `<` yourself.
compiler
The program that checks your code and translates it into instructions the computer can run, stopping with an error if it finds a mistake.
computed property
A property with no stored value, worked out by code each time it is read, like `needsReorder` on `StockItem`. It can't go stale.
conform
A type conforms to a protocol when it provides everything the protocol lists. You write `: Protocol` after the type's name and Swift checks it.
constant
A name for a value that never changes, made with `let`. Prefer it so Swift catches accidental changes.
continue
A keyword that skips the rest of the current loop pass and goes on to the next one.
CustomStringConvertible
A standard protocol that requires a `description` string, which `print` and string interpolation then show instead of the default text.

D

default (switch)
The `switch` case that catches every value no other case matches. It is needed unless the cases are already exhaustive, as with an enum.
default value
A value a parameter uses when the caller leaves that argument out, written with `=`, like `shop: String = "Kettle & Crane"`.
dictionary
A collection that stores values under unique keys, like a menu mapping `"lat"` to 4.75. It has no fixed order and lookups give an optional.
dot shorthand
Writing just `.large` instead of `Size.large` when Swift already knows the type.
Double
A type for numbers with a decimal point, like `4.75`. It can't store most decimals exactly, so `0.1 + 0.2` shows tiny errors.

E

editor
An app for writing plain text, and so code, such as Xcode or VS Code.
enum
A type with a fixed list of possible values, called cases, such as small, medium or large. A `switch` over one needs no `default` if it covers every case.
enumerated()
An array method that hands a `for` loop each item together with its index, as in `for (i, drink) in drinks.enumerated()`.
Equatable
A standard protocol that makes `==` and `!=` work for a type. For a struct whose properties are all equatable, writing `: Equatable` is enough.
error
A value describing what went wrong, of a type that conforms to the `Error` protocol (usually an enum). Unlike a crash or an optional, it says why.
exhaustive
A `switch` is exhaustive when it covers every possible value. Swift refuses to compile one that isn't.
expression
Code that produces a value, such as `2 + 3`. An `if` can also be an expression that gives a value.
extension
Adds new computed properties and methods to an existing type, like `money` on `Double`. It can't add stored properties.

F

for loop
A loop that runs a block once for each value in a range, array or string, putting the value in a name you choose.
force unwrap
A `!` after an optional that takes the value out, but crashes the program if it is `nil`. Avoid it; use `if let` or `??`.
Foundation
Apple's library of ready-made tools that comes with Swift, brought in with `import Foundation`. It provides `String(format:)` and `trimmingCharacters`.
function
A few lines of code with a name, so you can run them whenever you need them. You define one with `func`, and running it is a function call.
function call
Running a function by writing its name with arguments in parentheses, like `print("Hi")`.
function type
The type of a function or closure, like `(Double) -> Double` meaning takes a `Double` and gives back a `Double`.

G

generics
Code that works for many types at once, instead of being written separately for each type. Protocols are often combined with generics so one piece of code can handle any type that meets certain requirements.
global variable
A constant or variable declared outside any function or type. In a package, a global `var` outside `main.swift` is an error, but a global `let` such as `menu` is fine.
guard
Checks that something is true and, if not, runs an `else` block that must leave the function, usually with `return`. `guard let` unwraps an optional that stays available for the rest of the function.

H

Hashable
A standard protocol that lets a type be a dictionary key or go in a set. Like `Equatable`, writing it is usually enough.

I

if and else
`if` runs a block only when a condition is true. `else if` adds another check and `else` covers everything else; an `if` can also be an expression that gives a value.
if let
Unwraps an optional: if it holds a value, the block runs with a plain name for it, otherwise the `else` block runs.
implicit return
When a function body is a single expression, Swift returns its value without you writing `return`.
import
A line at the top of a file that makes a library's tools available, like `import Foundation`.
index
The position of an item in an array, starting at 0. Asking for one past the end crashes with `Index out of range`.
initializer
The code that sets up a new instance. Your own `init` replaces the memberwise one and must give every stored property a value.
instance
One actual value made from a struct or class definition, such as `latte` made from `MenuItem`.
Int
A type for whole numbers like `20` or `-3`.
integer division
Dividing two `Int`s gives an `Int` and drops the remainder, so `7 / 2` is `3`.

K

keyword
A word Swift reserves for itself, such as `let`, `func` or `if`.

L

loop
Code that repeats, using `for`, `while` or `repeat`.

M

main.swift
The file in a package target where the program starts. Only `main.swift` may hold top-level statements such as `print(...)`; other files hold only definitions.
map
Collection methods that take a closure: `map` makes a new array by applying it to each item, and `filter` keeps the items for which it returns `true`.
memberwise initializer
The initializer Swift writes automatically for a struct, with one labeled argument per property, like `MenuItem(code:name:price:)`.
method
A function that belongs to a type and is called with a dot, like `name.uppercased()`. Protocols list method requirements without bodies.
mutating
A keyword marking a struct or enum method that changes the value's own properties, like `receive` on `StockItem`. It can't be called on a `let` instance.

N

nil
Swift's value for "nothing here". Only optionals can be `nil`.
nil-coalescing
The `??` operator, which gives the value inside an optional or a default on its right when it is `nil`, as in `nickname ?? "guest"`.

O

operator
A symbol such as `+`, `==` or `??` that works on values.
optional
A type written with a `?` that holds either a value or `nil`, like `String?`. You must unwrap it before using the value.
optional chaining
Using `?.` to call a method on an optional; if it is `nil`, the call is skipped and the result is `nil`.

P

package
A folder with a `Package.swift` description file and a `Sources` folder of Swift files, used to organize a bigger program.
parameter
A named input of a function, with a type, such as `quantity` and `price` in `lineTotal`. Each call fills them with arguments.
precondition
A check that stops the program if a condition is false. Use it for mistakes that should never happen, as in `OrderLine`.
print
A ready-made function that shows the values in its parentheses and starts a new line. It accepts several values separated by commas.
private
`private` hides a property or method from code outside its type. `private(set)` lets outside code read a property but not change it.
program
A list of instructions for a computer, written as source code.
protocol
A list of properties and methods a type promises to provide, without saying how, such as `Priced` for bags and merchandise.

R

range
A run of numbers: `1...5` includes 5 and `1..<5` stops just before it. Ranges work in loops, `contains` and `switch` cases.
raw value
A fixed value linked to each enum case, like `"m"` for `Size.medium`. `Size(rawValue:)` goes back and gives an optional.
readLine
A function that waits for the user to type a line and gives back a `String?`, which is `nil` when the input has run out.
reduce
A collection method that combines every item into one value, such as `prices.reduce(0, +)` to get a total.
reference type
A type whose instances are shared: assigning one to another name gives a second name for the same object. Classes are reference types.
repeat while
A loop that checks its condition after the block, so the block always runs at least once.
return
`return` leaves a function and hands back its value, which takes the place of the call. `-> Double` states the return type.

S

self
Inside a type, the instance (or enum case) the code is working on. You need it mainly when a parameter has the same name as a property.
set
A collection of unique values with no order, written like `Set<String>`. Adding a duplicate does nothing.
sorted
A collection method that returns a new sorted array, smallest first, or in the order a closure decides.
source code
The text of a program, kept in files such as `hello.swift`.
split and joined
`split(separator:)` cuts a string into `Substring` parts, and `joined(separator:)` glues an array of strings into one string.
statement
One instruction in a program, usually one line. Swift runs statements from top to bottom.
stored property
A constant or variable kept inside each instance of a struct or class, such as `price` on `MenuItem`.
String
A type for text in double quotes, like `"Latte"`.
string interpolation
Putting a value or calculation inside a string by writing `\(` and `)` around it, like `"\(name) owes $\(total)"`.
String(format:)
A Foundation tool that builds a string from a number in a set layout; `String(format: "%.2f", total)` gives exactly two digits after the point, as for money.
struct
A value type that groups properties and methods into your own type, like `MenuItem`. Assigning one makes a copy.
Substring
A piece of a string that still points into the original, which `split` produces. `String(part)` turns it back into a `String`.
swift
The terminal command that compiles a Swift file and runs it straight away. `swift --version` shows which Swift is installed.
Swift
Apple's programming language, used for iPhone, iPad and Mac apps, and also running on Linux, Windows and servers.
swift run
Package commands: `swift package init` creates a package, `swift build` compiles it and `swift run` compiles and runs it. Run them from the package folder.
swiftc
The compiler on its own: `swiftc hello.swift -o hello` makes a program you can run again without compiling.
swiftly
The official Swift installer for Linux, which downloads and sets up the latest Swift.
switch
Compares one value against a list of cases and runs the first match. It must be exhaustive and never falls through to the next case.

T

target
One set of source files in a package. An executable target makes a program you can run.
Task
A piece of async work. `Task.sleep(for:)` waits for a time without blocking the program, and `Task { }` starts async work from non-async code.
terminal
A window where you type commands: Terminal on a Mac, PowerShell on Windows, or a terminal app on Linux. In examples, a leading `$` stands for the terminal's prompt and isn't typed.
terminator
A label on `print` that sets what it prints at the end, instead of a new line. `terminator: ""` keeps the cursor on the same line.
throws
A function marked `throws` can report an error with `throw`, and every call to it needs `try`.
toolchain
The Swift compiler together with its helper tools.
trailing closure
A closure written after a call's parentheses with its label dropped, like `priceAfter(72) { $0 - 5 }`.
try
Marks a call that can throw. `try?` turns an error into `nil`, and `try!` crashes if it throws (avoid it).
tuple
A few values grouped in parentheses, like `(code: "cb", price: 4.25)`, read by name or by position.
type
The kind of a value, such as `Int` or `String`, which says what you can do with it. Swift never mixes types silently.
type annotation
Stating a type yourself after a colon, like `let bagPrice: Double = 55`.
type inference
Swift working out a value's type from the value itself, so `20` becomes an `Int`.

V

value type
A type that is copied when assigned to another name, so changing the copy leaves the original alone. Structs, enums, `Int` and arrays are value types.
variable
A name for a value that can be given a new value later, made with `var`.
Void
The return type of a function with no `->`, meaning it gives back no value.
VS Code
A free editor that, with the Swift extension, colors your code and underlines mistakes on any system.

W

while loop
A loop that repeats as long as its condition is true. Something inside must change the condition or it runs forever.
WinGet
The package installer built into Windows, used to install the Swift toolchain.

X

Xcode
Apple's free app for writing Swift on a Mac. It includes the Swift compiler.