How to Keep the Struct Default Initializer in Swift

guide on How to Keep the Struct Default Initializer

Hallo vrienden, Leo here. Today we will learn how to keep the struct default initializer in your Swift codebase.

We’ll check a very short example of an initializer in Swift. Structure types automatically receive a memberwise initializer if they don’t define any of their own custom initializers. Unlike a default initializer, the structure receives a memberwise initializer even if it has stored properties that don’t have default values.

This is week is like Christmas for iOS developers because it’s WWDC week. This year Santa Claus was so awaited (if you know what I mean) but it turns out to be a really really hard news for some in the community. The so-awaited Async (for now) is just for iOS 15+ because the Swift runtime is shipped along with iOS. And I’ll not start the confusion about “async VS Combine” because this is for another post… So Christmas turn into a hell of a mess this year!

But still such great news like Xcode evolution and new components for UIKit/SwiftUI! And a lot of great talks about design/debugging/etc.

 

Painting of The Day

Today’s painting is a 1670 painting called Perseus Fighting Phineas and His Companions from
Luca Giordano. He lived from 18 October 1634 to 12 January 1705 and was an Italian late Baroque painter and printmaker in etching. Fluent and decorative, he worked successfully in Naples and Rome, Florence and Venice, before spending a decade in Spain. He acquired the nickname **Luca fa presto**, which translates into “Luca paints quickly.” His speed, in design as well as handiwork, and his versatility, which enabled him to imitate other painters deceptively, earned him two other epithets, “The Thunderbolt” (Fulmine) and “The Proteus” of painting.

This painting was chosen because of the discomposure the iOS community showed these days. A fighting painting is more than appropriate for those moments.

Let’s code!

 

The Problem – How to Keep the Struct Default Initializer

You want to keep the default memberwise initializer in your Struct and you also want to have custom initializers.

To illustrate the problem see the code below:

struct Human {
    let name: String
    let height: Double
}

You can now try to instantiate your Human and…

How to Keep the Struct Default Initializer first guide image

Yeah, great! Swift did automatically the memberwise initializer for us. But what if for you the height of a Human was irrelevant and you only want to set the name? You can do it just by adding a custom initializer to Human Struct like this:

struct Human {
    let name: String
    let height: Double
    
    init(name: String) {
        self.name = name
        height = 0
    }
}

So far so good… Isn’t it? No, because now we lost the capability of creating a Human with a height different from the default one.

See below:

How to Keep the Struct Default Initializer second guide image

As you can see, the Human Struct lost its default memberwise initializer the moment we defined a custom initializer for it. But How to Keep the Struct Default Initializer in Swift? Let’s now check how can we have the default initializer back.

 

The Solution

The solution is a simple trick. If we move the custom initializer to an **extension** of the Human struct we still get both initializers.

struct Human {
    let name: String
    let height: Double
}

extension Human {
    init(name: String) {
        self.name = name
        height = 0
    }
}

Resulting in:

How to Keep the Struct Default Initializer third guide image

And now you have the default and the custom initializer. Cool isn’t it!?

 

Summary – How to Keep the Struct Default Initializer

This week’s post was a very quick trick to don’t lose the default initializer from your structs.

That’s all my people, I hope you liked reading this article as much as I enjoyed writing it. If you want to support this blog you can Buy Me a Coffee or leave a comment saying hello. You can also sponsor posts and I’m open to freelance writing! You can reach me on LinkedIn or Twitter and send me an e-mail through the contact page.

Thanks for reading and… That’s all folks.

Credits: image

Share this post:

Related posts

Sponsor