Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 317 characters in body
Source Link
Kamran
  • 15.3k
  • 4
  • 35
  • 51

As the Int... is considered as an [Int] in the function body so compiler will not allow to pass [Int] in place of Int.... You better calculate the sum as below,

func makeSomething(_ numbers : Int..., workFunction : (Int...) -> Int) { let sum = numbers.map({ workFunction($0)}).reduce(0, +) print(sum) } 

Or introduce another method that accepts array of Int and returns sum. Something as below,

func anotherSum(_ numbers : [Int]) -> Int { return numbers.reduce(0, +) } func makeSomething(_ numbers : Int..., workFunction : ([Int]) -> Int) { print(workFunction(numbers)) } 

As the Int... is considered as an [Int] in the function body so compiler will not allow to pass [Int] in place of Int.... You better calculate the sum as below,

func makeSomething(_ numbers : Int..., workFunction : (Int...) -> Int) { let sum = numbers.map({ workFunction($0)}).reduce(0, +) print(sum) } 

As the Int... is considered as an [Int] in the function body so compiler will not allow to pass [Int] in place of Int.... You better calculate the sum as below,

func makeSomething(_ numbers : Int..., workFunction : (Int...) -> Int) { let sum = numbers.map({ workFunction($0)}).reduce(0, +) print(sum) } 

Or introduce another method that accepts array of Int and returns sum. Something as below,

func anotherSum(_ numbers : [Int]) -> Int { return numbers.reduce(0, +) } func makeSomething(_ numbers : Int..., workFunction : ([Int]) -> Int) { print(workFunction(numbers)) } 
Source Link
Kamran
  • 15.3k
  • 4
  • 35
  • 51

As the Int... is considered as an [Int] in the function body so compiler will not allow to pass [Int] in place of Int.... You better calculate the sum as below,

func makeSomething(_ numbers : Int..., workFunction : (Int...) -> Int) { let sum = numbers.map({ workFunction($0)}).reduce(0, +) print(sum) }