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

Support Swift by Sundell by checking out this sponsor:

Essential Developer

Essential Developer: Learn the most up-to-date techniques and strategies for testing new and legacy Swift code in this free practical course for developers who want to become complete senior iOS developers. This virtual event includes lectures, mentoring sessions, and step-by-step instructions. Click to learn more.