What is lazy property is Swift?

App Developer
6 min readDec 18, 2022

--

This article revolves around the concept of Lazy property in Swift. We will try to understand lazy property with an interview perspective and will also deal with important interview questions that can be asked regarding lazy property in Swift.

https://www.youtube.com/watch?v=ptpivKdpPv0

Consider the following code snippet

class Person {
var firstName: String
var lastName: String
init(firstName: String, lastName: String){
self.firstName = firstName
self.lastName = lastName
}
}
let person = Person(firstName: "Nilesh", lastName:"Jha")

In the above code snippet we can make out that as soon as we create an object of the class Person, properties like firstName and lastName will be initialised and memory would be allocated to them i.e with normal properties no sooner we create an object then memories are allocated to them. Here itself lazy property comes into picture, let’s say the size of the object would have been so large that even allocation of memory to these properties at the very start would have been a costlier move so with help of lazy property we can curb the initial allocation of memory to properties and thereby memory would be allocated not at the start but only when that particular property is explicitly called.

Following is the code implementation of lazy property

class Person{
var firstName: String
var lastName: String
init(firstName: String, lastName: String){
self.firstName = firstName
self.lastName = lastName
}
lazy var fullName: String = {
return firstName + lastName
}()
}
let person = Person(firstName: "Nilesh", lastName:"Jha")
print(person.fullName)

In the above code snippet we declared property fullName as lazy property and the syntax for doing so is:

lazy var property name: type name = {
statements
return value
}()

Here comes our first question and that is

Can we declare a lazy with the let keyword?

So the answer is No. We cannot declare a lazy with let keyword the reason being if we declare a property with let keyword then it means that it’s initial value is determined at the time of definition but in case of lazy we are aware that a lazy property’s initial value is not determined rather it gets initial value only when we call it explicitly.

Moving onto next interesting question then

Isn’t the syntax of lazy looking much like a computed property!

Consider the following code snippet

// Lazy property
lazy var fullName: String = {
return firstName + lastName
}()
// Computed property
var fullNameComputed: String {
return firstName + lastName
}

So the question is

Is computed property and lazy property the same, if not then how are those two different?

The answer is computed property and lazy property are two different things. The major difference between the two is that a lazy property is a stored property. Following code snippet depicts the same

class Person{
var firstName: String
var lastName: String
init(firstName: String, lastName: String){
self.firstName = firstName
self.lastName = lastName
}
// Lazy property
lazy var fullName: String = {
return firstName + lastName
}()
// Computed property
var fullNameComputed: String {
return firstName + lastName
}
}
let person = Person(firstName: "Nilesh", lastName:"Jha")
print(person.fullName)
print(person.fullNameComputed)
person.firstName = "Mobile trainer"
print("**********")
print(person.fullName)
print(person.fullNameComputed)

Output:

From the above code snippet and output we can figure out that the computed property fullNameComputed gave the output different as the updated one but the lazy property fullName didn’t change. The reason being as lazy property is stored property so the value of the lazy property will behave same as it was at the time of calling it just like the following

var fullName = firstName + lastName

But in case of computed property since these don’t have any value so whenever we call that property it returns the value based on the value at that moment of time.

If the lazy property can’t be changed with the above way then does this mean that we cannot change its value?

The answer is no, we can change the value. Consider the following code snippet for the same:

class Person{
var firstName: String
var lastName: String
init(firstName: String, lastName: String){
self.firstName = firstName
self.lastName = lastName
}
// Lazy property
lazy var fullName: String = {
return firstName + lastName
}()
// Computed property
var fullNameComputed: String {
return firstName + lastName
}
}
let person = Person(firstName: "Nilesh", lastName:"Jha")
print(person.fullName)
print(person.fullNameComputed)
person.firstName = "Mobile trainer"
print("**********")
print(person.fullName)
print(person.fullNameComputed)
person.fullName="iOS Trainer"
print("**********")
print(person.fullName)

Output:

From the above code and output we can clearly make out that we can easily change the value of a lazy property with help of the above approach which is by changing the entire property value.

Let’s move onto another important interview centric code snippet regarding the lazy property.

Consider the following code snippet and predict the output:

Moving line by line to the flow of execution of the above program then first of all the program will look for y as it reaches line number 16. After line number 16 the control will move to line number 6 and then it will move inside the function so it will move to line 7 thereby printing y now the flow will to line 8 and here it will encounter x so the control will move to line 2 where in the control will move inside it and reach line 3 thereby printing x now the control will move to line 4 and will return nil and the control will get back to line 8, now since there is a nil checker and the value was nil as well so the function will return 4 and the control will finally get back to line 16 and since the return value was not nil so foo.z() will not get executed. Therefore the output screen will look like the following

This wraps our article on lazy property in Swift

Enjoying!! I will be adding more post like this. Follow me so that you don’t miss any of them.

If you enjoyed reading this post, please share and give some claps so others can find it 👏👏👏👏👏 !!!!

You can follow me on medium for fresh articles. Also, subscribe me on youtube . It takes little effort from you to subscribe , but it gives me as a content writer some revenue and more motivation to make more content for you. So its my kind request to support me.

If you have any comment, question, or recommendation, feel free to post them in the comment section below!

--

--