Variadic Parameter

  • The values passed to a variadic parameter are made available within the functionโ€™s body as an array of the appropriate type.

  • A function can have multiple variadic parameters.

  • The first parameter that comes after a variadic parameter must have an argument label.

// โญ๏ธ variadic parameter
func f1(_ ints: Int...) -> Int { return 1 }

func f2(_ ints: Int...) -> Int {     // `ints` is of type `[Int]`
    // โ›” cannot convert value of type '[Int]' 
    //    to expected argument type 'Int'
    return f1(ints) 
}

// The only way to solve this (for now) is to add 
// another method overload accepting an array

Last updated