A trick to keep the Struct's Default Memberwise initializer in Swift

Easy Swift Trick!

Subscribe to my newsletter and never miss my upcoming articles

Hallo vrienden, Leo here.

Today we see a very short example of 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 developer 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 new like Xcode evolution and new components for UIKit/SwiftUI! And a lot of great talks about design/debuggin/etc.

The painting

Today 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 for him two other epithets, "The Thunderbolt" (Fulmine) and "The Proteus" of painting.

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

Let's code!

The Problem

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 you Human and...

Screen Shot 2021-06-10 at 08.39.01.png

Yeah, great! Swift did automatically the memberwise initializer for us. But what if for you the height of Human was irrelevant and you only want to set the name. You can do it just 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 capability of creating a Human with height different from the default one. See below:

Screen Shot 2021-06-10 at 08.43.11.png

As you can see, the Human Struct lost its default memberwise initializer in the moment we defined a custom initializer to it. 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 Human struct we still get the both initializers.

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

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

Resulting in:

Screen Shot 2021-06-10 at 08.49.27.png

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

Conclusion

This week 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 this as I liked writing. If you want to support this blog you can Buy Me a Coffee or just leave a comment saying hello.

Thanks for the reading and... That's all folks.

credits: image

Interested in reading more such articles from Leonardo Maia Pugliese?

Support the author by donating an amount of your choice.

Recent sponsors

No Comments Yet