Swift Lazy Initialization with Closures
Learn how to create objects with modularity and readability
Welcome my lovely readers. Good to see you here today.
Motivation
In the beginning of my iOS journey, I followed tutorials on YouTube. I saw a few using something like below to create UI objects.
let makeBox: UIView = {
let view = UIView()
return view
}()
As a learner, I copied the practice and used it. One day, however, one of my readers asked me, “why do you add {}
and why does ()
exist at the end? Is it a computed property?” I could not answer. I was a zombie.
I wrote this tutorial for my younger self. Yet, some may find it useful.
Objectives
There are three objectives. First, understand how to initialize an object using the unconventional way as shown above. Second, learn when to use lazy var
in Swift. Last, join my mailing list.
Prerequisites
To fully enjoy the ride with me, I highly recommend you to be familiar with the topics below.
- Closures
- Capture List and retention cycle [weak self]
- Descent Object Oriented Programming
Create UI Components
Before I explain the unconventional method above, let’s look into your past. In order to create a button in Swift, you probably have done something like this,
// Determine Size
let buttonSize = CGRect(x: 0, y: 0, width: 100, height: 100)// Create Instance
let bobButton = UIButton(frame: buttonSize)
bobButton.backgroundColor = .black
bobButton.titleLabel?.text = "Bob"
bobButton.titleLabel?.textColor = .white
This is Okay.
Assume, you have to create three other buttons, you probably have to copy the code above and then change the name from bobButton
to bobbyButton
.
It’s quite tedious.
// New Button
let bobbyButton = UIButton(frame: buttonSize)
bobbyButton.backgroundColor = .black
bobbyButton.titleLabel?.text = "Bob"
bobbyButton.titleLabel?.textColor = .white
To make things just a bit easier, you may
If you don’t wish to repeat yourself, you may create a function instead.
func createButton(enterTitle: String) -> UIButton {
let button = UIButton(frame: buttonSize)
button.backgroundColor = .black
button.titleLabel?.text = enterTitle
return button
}createButton(enterTitle: "Yoyo") // 👍
However, in iOS development, it is rare that custom buttons look similar. Therefore, a function may require a lot more parameters including background color, title, border radius, shadow, and so on. You function may end up looking like,
func createButton(title: String, borderWidth: Double, backgrounColor, ...) -> Button
The code above is not ideal even if you add default parameters to the function. It decreases readability. Therefore, it’s better to stay with the tedious method above.
But, is there any way we can make it less tedious and more organized? Of course. We’ve looked into your past — It’s time to step up and look into your future.
Introducing the Unconventional Way
Before we create UI components with the unconventional way, let’s first answer the initial questions my reader asked. What does {}
mean, and is it a computed property
?
The content has been migrated from Medium to personal platform. You can read the rest of the content about lazy/closure initialization here