Articles, podcasts and news about Swift development, by John Sundell.

The power of variadic parameters

Published on 29 Jun 2019

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)

Support Swift by Sundell by checking out this sponsor:

Emerge

Emerge: Continuously monitor and reduce your app’s size. Emerge’s easy to use plugins for GitHub and fastlane will automatically scan your app’s binary and provide you with simple, actionable suggestions on how to make it smaller and, in turn, faster for your users to download. Set up a demo now!