Articles and podcasts about Swift development, by John Sundell.

Genius Scan SDK

Presented by the Genius Scan SDK

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.

Default enum associated values

Published on 03 Jul 2019
Basics article available: Enums

New in Swift 5.1: Enum cases can now specify defaults for their associated values — which comes very much in handy when we want to add options or overrides to a given enum case.

// Associated enum value defaults are specified the same way as
// default function arguments:
enum Content {
    case text(String)
    case image(Image, description: String? = nil)
    case video(Video, autoplay: Bool = false)
}

// At the call site, any associated value that has a default
// can be omitted, and the default will be used:
let video = Content.video(Video(url: url))
Genius Scan SDK

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.