在Swift中,如何将数组转换为元组?
问题出现了,因为我试图调用一个函数,该函数在一个带有可变数量参数的函数中接受可变数量的参数。
// 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
}
这个averageOf
实现对我来说似乎很合理,但它不能编译。当您尝试呼叫sumOf(numbers)
时,它会出现以下错误:
Could not find an overload for '__converstion' that accepts the supplied arguments
在averageOf
内,numbers
的类型为Int[]
。我相信sumOf
期待一个元组而不是数组。
因此,在Swift中,如何将数组转换为元组?
答案 0 :(得分:24)
这与元组无关。无论如何,在一般情况下,不可能从数组转换为元组,因为数组可以具有任何长度,并且必须在编译时知道元组的arity。
但是,您可以通过提供重载来解决您的问题:
// This function does the actual work
func sumOf(_ numbers: [Int]) -> Int {
return numbers.reduce(0, +) // functional style with reduce
}
// This overload allows the variadic notation and
// forwards its args to the function above
func sumOf(_ numbers: Int...) -> Int {
return sumOf(numbers)
}
sumOf(2, 5, 1)
func averageOf(_ numbers: Int...) -> Int {
// This calls the first function directly
return sumOf(numbers) / numbers.count
}
averageOf(2, 5, 1)
也许有更好的方法(例如,Scala使用特殊类型的ascription来避免需要重载;你可以在sumOf(numbers: _*)
内写入Scala averageOf
而不定义两个函数),但我没有& #39; t在文档中找到它。
答案 1 :(得分:0)
从Swift 4.1开始(在Xcode 9.2中),不需要使用sumOf(_ numbers:Int ...)重载,转发可变参数的函数将IMPLICITLY将其更改为数组的单个参数个别参数。例如。以下代码将在没有重载的情况下工作:
// This function does the actual work
func sumOf(_ numbers: [Int]) -> Int {
return numbers.reduce(0, +) // functional style with reduce
}
func averageOf(_ numbers: Int...) -> Int {
// This calls the first function directly
return sumOf(numbers) / numbers.count
}
print(averageOf(2,5,1))
不知道这是否是编译器的错误:)
答案 2 :(得分:0)
我意识到这是一篇较旧的文章,但是在搜索结果中占了相当高的比例,我找到了一个可行的解决方案。
您可以编写sumOf函数以接受整数数组作为number参数,并重载sumOf函数以接受number参数的可变输入,该参数将作为数组传递给第一个版本。这样,averageOf函数可以将其可变参数输入作为数组传递给sumOf。
这似乎不是很理想,因为您需要重载每个像这样工作的函数,但是它将按您想要的方式工作。
func sumOf(numbers: [Int]) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
// Function 1
func sumOf(numbers: Int...) -> Int {
return sumOf(numbers: numbers)
}
// Example Usage
sumOf(2, 5, 1)
// Function 2
func averageOf(numbers: Int...) -> Int {
return sumOf(numbers: numbers) / numbers.count
}
答案 3 :(得分:-2)
我认为他不需要使用.reduce。而只需在sumOf函数中更改参数定义。而不是:
func sumOf(numbers: Int...) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
写:
func sumOf(numbers: [Int]) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}