Let It Go: Late Initialization of Let in Swift

One of the BIG improvements that came in Swift 1.2 was the ability to initialize let a little bit later. According to the Swift blog:

let constants are now more powerful and consistent — The new rule is that a let constant must be initialized before use (like a var), and that it may only be initialized, not reassigned or mutated after initialization.

This enables patterns like:

I was of course aware of this change, yet I haven’t fully internalized it. I’m way too into my habit of using vars in cases like the one mentioned above!

This week, @allonsykraken pointed out at least one common use-case for this in my code. Creating a cell in a table view of course!

So glad I get to use more lets instead of vars!!! Going to have to keep that in mind as I rebuild my var habit.

And of course, the moment you’ve all been waiting for….

You’re welcome!

Enjoy the article? Join over 20,000+ Swift developers and enthusiasts who get my weekly updates.