I new in swift and now learning this hard as i can. I wrote function that got arguments: numbers and function that work with this numbers:
func anotherSum(_ numbers : Int...) -> Int { return numbers.reduce(0, +) } func makeSomething(_ numbers : Int..., f : (Int...) -> Int) { print(workFunction(numbers)) } makeSomething(1,2,3,4,5,6,7,8, f: anotherSum) But compile gives out error cannot convert value of type '[Int]' to expected argument type 'Int' . When i tried to change argument like
workFunction : ([Int]) -> Int) and
func anotherSum(_ numbers : [Int]) -> Int
It work perfectly, but i still can't understand why realisation with Int... doesn't work and why compiler gives this error.
Int...and[Int]are completely unrelated types in this context. You need to modifyanotherSumto take a[Int]instead, if you want this to work. There's no current way to "splat" arrays in Swift.Int...will turn into[Int]immediately in the function body.anotherSum(Int...)is supposed to take multiple parameters, while each parameter is anInt. CallingworkFunction(numbers)is essentiallyanotherSum([Int]), you've just passed a single parameter, which is[Int]notInt, toanotherSum. That's why types do not match.