Swift Variadic Parameters

«  Operational Transformation
User Privacy and Data Use  »

A variadic parameter accepts zero or more values of a specified type. You use a variadic parameter to specify that the parameter can be passed a varying number of input values when the function is called. Write variadic parameters by inserting three period characters (…) after the parameter’s type name.

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

ex: A type of Double... will be treated as a constant array [Double] type.

func arithmeticMean(_ numbers: Double...) -> Double {
    var total: Double = 0
    for number in numbers {
        total += number
    }
    return total / Double(numbers.count)
}
arithmeticMean(1, 2, 3, 4, 5)
// returns 3.0, which is the arithmetic mean of these five numbers
arithmeticMean(3, 8.25, 18.75)
// returns 10.0, which is the arithmetic mean of these three numbers

Note

  • A function may have at most one variadic parameter.
  • In-out parameters cannot have default values, and variadic parameters cannot be marked as inout.

Reference

https://docs.swift.org/swift-book/LanguageGuide/Functions.html

Published on 12 Aug 2020 Find me on Facebook, Twitter!

«  Operational Transformation
User Privacy and Data Use  »

Comments

    Join the discussion for this article at here . Our comments is using Github Issues. All of posted comments will display at this page instantly.