What's the _ underscore representative of in Swift References?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you have spent any time reading Swift code, you have almost certainly encountered the underscore character _ appearing in unexpected places. Unlike most languages where _ is simply a valid identifier character, Swift gives it special meaning in several distinct contexts. Understanding these uses is essential for reading Swift APIs, writing idiomatic functions, and working with pattern matching effectively.
Omitting External Parameter Labels
By default, every Swift function parameter has both an external label (used at the call site) and an internal name (used inside the function body). Placing _ as the external label tells the compiler that callers should not provide a label for that argument.
This is extremely common in the Swift standard library and Apple frameworks. For example, print(_:) uses an underscore so you write print("Hello") instead of print(items: "Hello"). You will also see it on the first parameter of many UIKit methods where the label would be redundant with the function name, like tableView(_:numberOfRowsInSection:).
Discarding Return Values
When a function returns a value that you intentionally do not need, Swift normally produces a compiler warning. You can silence that warning by assigning the result to _.
This communicates to both the compiler and other developers that you deliberately chose not to use the return value.
Ignoring Tuple Elements
When destructuring a tuple, you can use _ to skip elements you do not care about.
You can use _ for as many elements as you like. If you only need the age, write let (_, age, _) = getUser().
Ignoring Loop Variables
In a for loop where you only need to repeat an action a certain number of times without using the loop index, _ replaces the loop variable.
This is a clear signal that the iteration count matters but the current index does not.
Pattern Matching in switch Statements
In switch statements and if case expressions, _ acts as a wildcard pattern that matches any value.
This is especially useful with enums that have associated values when you want to match the case but ignore the payload.
Numeric Literals Readability
Swift allows _ as a visual separator inside numeric literals to make large numbers easier to read. This is purely cosmetic and has no effect on the value.
Ignoring Closure Parameters
When a closure receives parameters you do not need, replace them with _.
Common Pitfalls
- Overusing
_for external labels: Removing all parameter labels can make call sites ambiguous. Use_only when the function name already makes the argument's role obvious. - Accidentally discarding important return values: Assigning to
_suppresses warnings, but if the return value indicates an error condition, you may be silently ignoring failures. - Confusing
_in patterns withdefault: In aswitchstatement,case _:anddefault:both match everything, butdefaultmust be the last case. Preferdefaultfor catch-all branches. - Using
_as an actual variable name: While_is technically assignable, you cannot read from it. Writinglet _ = valueand then trying to use_later will not compile. - Forgetting
_in delegate methods: Many Cocoa delegate methods require_as the external label for their first parameter. Omitting it changes the method signature and the method will not be called.
Summary
_as an external parameter label lets callers omit the argument label at the call site._ = expressionexplicitly discards a return value and silences compiler warnings._in tuple destructuring,forloops, and closure parameters ignores values you do not need._inswitchcases acts as a wildcard pattern matching any value._inside numeric literals serves as a visual separator for readability.

