I’m currently working on a Swift project and encountered an issue when trying to pass a variadic parameter from one function to another.
Specifically, I expected that I could directly pass the variadic parameter from one function to another that also accepts variadic parameters. However, this approach doesn’t seem to work as I expected, and I’m not sure why.
Here’s a simplified version of my code:
func myFunctionA(integers: Int...) {
// Some operations with integers
}
func myFunctionB(integers: Int...) {
myFunctionA(integers: integers) // This line throws an error
// Cannot pass array of type 'Int...' as variadic arguments of type 'Int'
}
In this code, myFunctionB
is supposed to accept a list of integers, and then pass them to myFunctionA
. However, I’m getting an error on the line where myFunctionA
is called, and I’m not sure why the variadic parameter can’t be passed along like this.
Could someone explain why this doesn’t work in Swift? Is there a correct way to pass variadic parameters from one function to another, or am I approaching this incorrectly?
For broader context, myFunctionA
is part of a centralised, shared set of methods. myFunctionB
is in a convenience wrapper class, and thought being able to pass the parameter onwards would maintain the code simplicity.