Why do I need underscores in swift?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Swift, Apple's powerful and intuitive programming language for macOS, iOS, watchOS, and tvOS, often introduces developers to unique syntax and conventions designed to create safe and expressive code. One such convention is the use of underscores (_) in various contexts. This article will delve into why underscores are used in Swift, exploring their significance with technical explanations and examples.
Key Uses of Underscores in Swift
Underscores in Swift serve multiple purposes, from acting as wildcards to improving code readability. Here, we'll explore some of the primary uses.
Ignoring Values
In Swift, you might encounter situations where a value is returned by a function, but you don't need to use it. In such cases, underscores allow you to ignore these values. Here's an example:
This is useful when the value isn't needed to execute the loop. The underscore signifies an intentional disregard for that particular value.
Discarding Function Parameters
Swift functions can have parameters that are not used within the function body. To signify this, you can use underscores to discard these parameters. For instance:
This typically happens in closures where unnecessary parameters are required due to the function signature but aren't used. By convention, using an underscore makes it clear that these parameters are ignored.
Omitting External Parameter Names
Swift allows you to specify both internal and external names for parameters in functions and methods. Sometimes, you might want to suppress the external parameter name to make a function call more readable. For this, you can use an underscore:
Here, using _ means a doesn't need an external name, allowing a more fluent syntax when calling divide.
Pattern Matching and Wildcards
In Swift's powerful pattern matching syntax, underscores can act as wildcards to denote any value without caring about what it is. For example, when switching over an enumeration or using if case:
If we do not care about all cases we can use _:
Readability in Large Numbers
For better readability, Swift allows underscores to be used as separators in numeric literals, without affecting their actual value:
This practice helps improve the readability of large numbers, aligning them with customary formatting (e.g., thousands, millions) and enhancing the developer's ability to quickly understand the code.
Summary
The varied uses of underscores in Swift contribute to more concise, readable, and intention-revealing code. Below is a table summarizing the key reasons and uses for underscores in Swift.
| Purpose | Description |
| Ignoring Values | Used to discard unneeded variable values, e.g., in loops and function calls. |
| Discarding Parameters | Omits unused parameters or variables within functions or closures, signaling they are not important. |
| Omitting External Names | Suppresses external parameter names for cleaner and more readable function calls. |
| Pattern Matching | Utilized as wildcards in pattern matching, allowing for matching without specifying a value. |
| Readable Numbers | Used as separators in numeric literals, improving readability without affecting value. |
By utilizing underscores strategically, Swift developers can maintain clean and expressive code, aligning with Swift’s design philosophy. Understanding these nuances can greatly enhance one’s proficiency and efficiency in writing Swift programs.

