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.
Improved memberwise initializers in Swift 5.1
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")])

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.