Expressions
An expression is a piece of code that produces a value. Swift organizes expressions into four families — prefix, infix, primary, and postfix — that compose to form everything from a literal to a chained method call. This reference surveys each.
Prefix expressions
A prefix expression applies an optional prefix operator to an
expression, for example -x, !flag, or ~bits.
The & in front of an argument forms an in-out
expression, passing a variable by reference to an inout
parameter.
var n = 5
swap(&a, &b) // & forms an in-out expression
let negative = -n
Infix (binary) expressions
An infix expression combines two operands with a binary
operator: a + b, x == y, flag && other.
The compiler reorders chains of infix operators according to their
precedence and associativity, so the source
order is just a flat list until then. Special forms in this family include
assignment (=), the ternary conditional
(cond ? a : b), type-casting operators (is,
as, as?, as!), and the
~= pattern-match operator used by switch.
Primary expressions
Primary expressions are the most basic building blocks. They
include identifiers, literal expressions, self and
super expressions, closure expressions, parenthesized and tuple
expressions, wildcard _, key-path expressions, and selector
expressions.
Literal expressions
Besides ordinary literals, Swift provides playground/diagnostic literals such
as #file, #fileID, #line,
#function, and #column, which expand to information
about the source location.
Closure expressions
A closure expression is an inline, unnamed function. It may include a capture list, parameter list, return type, and body — any of which can be inferred.
let doubled = nums.map { $0 * 2 }
let handler = { [weak self] value in self?.update(value) }
Key-path and selector expressions
A key-path expression, written \Type.path,
produces a KeyPath value referring to a property without accessing
it. A selector expression, #selector(...),
produces an Objective-C selector, and #keyPath(...) produces an
Objective-C key-path string.
let kp = \Person.name
let name = somePerson[keyPath: kp]
let sel = #selector(MyView.handleTap)
Postfix expressions
Postfix expressions attach a suffix to a primary expression. The main forms:
| Form | Example | Meaning |
|---|---|---|
| Function call | f(1, label: 2) | Invoke a function/closure |
| Initializer | Int.init(3.0) | Reference an initializer |
| Member access | p.name, .red | Explicit or implicit member |
| Subscript | arr[0] | Access via subscript |
| Forced value | opt! | Force-unwrap an optional |
| Optional chaining | opt?.name | Access only if non-nil |
| Postfix operator | x++ (custom) | Apply a postfix operator |
A function call may end with one or more trailing closures,
and an initializer expression (SomeType.init) lets you refer to an
initializer as a value.
let first = people.first?.name // optional chaining → String?
let forced = dictionary[key]! // forced value
makeRequest(url) { result in ... } // trailing closure
Note: optional chaining short-circuits — if any link is
nil, the whole expression evaluates to nil and the
result type gains a single layer of optionality.
Summary
Expressions in Swift form a layered grammar: primary expressions sit at the core, postfix expressions chain accesses and calls onto them, prefix operators wrap them, and infix operators join them — with precedence and associativity resolving the final shape. The specialized forms (key-path, selector, literal macros, in-out, optional chaining, forced value) round out the everyday toolkit.