Multiple variadic parameters in functions
About 2 min
Multiple variadic parameters in functions 관련
HACKING WITH SWIFT
What's new in Swift?
Multiple variadic parameters in functions | Changes in Swift 5.4
Multiple variadic parameters in functions
Available from Swift 5.4
SE-0284 (apple/swift-evolution
) introduced the ability to have functions, subscripts, and initializers use multiple variadic parameters as long as all parameters that follow a variadic parameter have labels. Before Swift 5.4, you could only have one variadic parameter in this situation.
So, with this improvement in place we could write a function that accepts a variadic parameter storing the times goals were scored during a football match, plus a second variadic parameter scoring the names of players who scored:
import Foundation
func summarizeGoals(times: Int..., players: String...) {
let joinedNames = ListFormatter.localizedString(byJoining: players)
let joinedTimes = ListFormatter.localizedString(byJoining: times.map(String.init))
print("\(times.count) goals where scored by \(joinedNames) at the follow minutes: \(joinedTimes)")
}
To call that function, provide both sets of values as variadic parameters, making sure that all parameters after the first variadic are labeled:
summarizeGoals(times: 18, 33, 55, 90, players: "Dani", "Jamie", "Roy")
Other Changes in Swift 5.4
Improved implicit member syntax | Changes in Swift 5.4
Improved implicit member syntax
Local functions now support overloading | Changes in Swift 5.4
Local functions now support overloading
Creating variables that call a function of the same name | Changes in Swift 5.4
Creating variables that call a function of the same name
Result builders | Changes in Swift 5.4
Result builders
Property wrappers are now supported for local variables | Changes in Swift 5.4
Property wrappers are now supported for local variables
Packages can now declare executable targets | Changes in Swift 5.4
Packages can now declare executable targets