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

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 = url))