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

Improved memberwise initializers in Swift 5.1

Published on 19 Jun 2019

New in Swift 5.1: When a struct property has a default value, it’s no longer required to pass a value for it when using the compiler-generated initializer. One of my favorite tweaks in this version of Swift — it’s a small feature, but makes structs feel even more lightweight.

// In Swift 5.1, when a struct property has a default value,
// it's no longer required to pass a value for it when calling
// the compiler-generated initializer.
struct Article {
    var title: String
    var tags = [Tag]()
}

// Meaning that we can now create an Article value without
// supplying any tags:
Article(title: "Shifting paradigms in Swift")

// We can of course still pass values for all properties when
// we want to:
Article(title: "Access control", tags: [Tag(name: "Basics")])