This article has been archived, as it was published several years ago, so some of its information might now be outdated. For more recent articles, please visit the main article feed.
The power of variadic parameters
I’m a big fan of functions with variadic parameters in Swift, especially when I want an API to feel a bit more “lightweight”, since no array needs to be created at the call site in order to pass multiple (or zero) arguments.
// When using a variadic parameter, any number of arguments can
// be passed, and the compiler will automatically organize them
// into an array.
func send(_ message: Message, attaching attachments: Attachment...) {
...
}
// Passing no variadic arguments:
send(message)
// Passing either a single, or multiple variadic arguments:
send(message, attaching: image)
send(message, attaching: image, document)

Swift by Sundell is brought to you by the Genius Scan SDK — Add a powerful document scanner to any mobile app, and turn scans into high-quality PDFs with one line of code. Try it today.