Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
An argument label in Swift is an external identifier used at the call site of a function, method, or initializer to explicitly define the role of the value being passed. It separates the external API interface from the internal implementation by allowing a parameter to have one name when the function is called (the argument label) and a different name when the parameter’s value is accessed within the function body (the parameter name).
By default, every function parameter in Swift possesses both an argument label and a parameter name. If an explicit argument label is not provided during declaration, the Swift compiler automatically assigns the parameter name to act as the argument label.
Syntax Anatomy
The argument label is declared immediately preceding the parameter name, separated by a single space.
func functionName(argumentLabel parameterName: String) {
// 'parameterName' is used internally within this scope
print(parameterName)
}
let stringValue = "Example"
// 'argumentLabel' is required at the call site
functionName(argumentLabel: stringValue)
Structural Variations
1. Default Behavior (Implicit Argument Label)
When only one identifier is provided, it serves as both the internal parameter name and the external argument label.
func configure(timeout: Int) {
print("Timeout set to \(timeout)") // Internal use
}
configure(timeout: 30) // External use
2. Explicit Argument Label
When two identifiers are provided, the first acts strictly as the argument label for the caller, and the second acts strictly as the parameter name for the internal scope.
func move(from source: String, to destination: String) {
// Internal scope uses 'source' and 'destination'
print("Moving from \(source) to \(destination)")
}
// Call site uses 'from' and 'to'
move(from: "Cache", to: "Disk")
3. Omitted Argument Label
An argument label can be explicitly suppressed using the wildcard token (_). This forces the caller to pass the argument positionally, without any label.
func process(_ data: String, with options: Int) {
// Internal scope uses 'data' and 'options'
print("Processing \(data) using option \(options)")
}
let rawBytes = "01010101"
// Call site omits the label for the first argument
process(rawBytes, with: 1)
Method Signature Resolution
In Swift, argument labels are a fundamental component of a function’s signature. Two functions can share the same base name and parameter types but remain distinct entities if their argument labels differ.
// Signature: load(file:)
func load(file: String) {}
// Signature: load(url:)
func load(url: String) {}
The compiler resolves the correct function overload at compile time based on the entire function signature. While argument labels differentiate the two functions in the example above, overload resolution evaluates the base name, parameter types, return types, generic constraints, and argument labels collectively to determine the correct implementation to invoke.
Master Swift with Deep Grasping Methodology!Learn More