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:

// 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:

Essential Developer

Essential Developer: If you’re a mid/senior iOS developer who’s looking to improve both your skills and your salary level, then join the iOS Architect Crash Course, starting on January 31st. It’s 100% free and held entirely online. Click to learn more.