In Swift, how do you convert an Array to a Tuple?
The issue came up because I am trying to call a function that takes a variable number of arguments inside a function that takes a variable number of arguments.
// Function 1
func sumOf(numbers: Int...) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
// Example Usage
sumOf(2, 5, 1)
// Function 2
func averageOf(numbers: Int...) -> Int {
return sumOf(numbers) / numbers.count
}
This averageOf
implementation seemed reasonable to me, but it does not compile. It gives the following error when you try to call sumOf(numbers)
:
Could not find an overload for '__converstion' that accepts the supplied arguments
Inside averageOf
, numbers
has the type Int[]
. I believe sumOf
is expecting a Tuple rather than an Array.
Thus, in Swift, how do you convert an Array to a Tuple?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…