Lazy Stored Procedure in Swift

In this tutorial, you are going to learn about the lazy stored procedure. What is the lazy keyword and how to use it in Swift? Let's learn.

In iOS development, it is very common to deal with complex objects. But you need to use them until it’s really needed.

In Swift, there are some declaration modifiers that modify the behavior of the declaration. An example of a lazy modifier is one that is used to modify the meaning of the stored variables of a class or structure.

What Swift's stored properties are?

In Swift, stored properties are the basic properties that contain values for an instance of a class or structure. Stored properties can be either variables or constants. You can provide a default value while defining it. You can also modify the initial value during initialization.

Lazy Stored Properties

A lazy var is a property whose initial value is not calculated until the first time it's called. It is also a part of properties in which we have other properties like constant, computed, and mutable properties.

Remember that lazy property must always be declared as variables with the var keyword, not constants. It is because its initial value might not be retrieved until after instance initialization completes. Constant properties must always have a value before the initialization completes, and therefore can’t be declared lazy.

What is lazy var in Swift?

It is more accurate to describe what a lazy variable is by its name. This means the initial value is not calculated until it is called or used for the first time.

In other words, a lazy variable gets initialized on demand.

It is true that you can only use lazy variables in a class or structure.

Let's look at it with an example −


struct Student { var rollNumber: Int var name: String lazy var uniqueId: String = { "ID_\(name)_\(rollNumber)" }() } // Instance creation var amit = Student(rollNumber: 12009, name: "Amit") print(amit.uniqueId)




In the above example, we have created a struct classed Student having some stored properties and one lazy property. We defined a lazy variable to create and get a unique ID.

The stored properties get initialized with their initial values when you create an instance of this Student struct. However, the lazy property called uniqueId is not initialized. So now the question is when will it be initialized?

When the lazy property is read by calling it, it will be initialized and return the calculated value of the type string.

Anatomy of a lazy variable

  • Lazy variables belong to classes or structures. A lazy variable cannot be declared anywhere in the code.

  • Use the lazy keyword modifier before var to make a variable lazy.

  • Lazy variables must be computed rather than directly assigned values.

    • A block of code performs the computation.

    • Declare the block of code using curly braces.

    • The block of code should return a value. After that, it is assigned to the lazy variable.

    • Assign the block of code to the lazy variable.

  • Add the parenthesis () after the curly braces.

Difference between lazy var and computed property

It is imperative to understand the difference between lazy stored property and computed property. For beginners, it can be confusing to distinguish between them sometimes.

  • A lazy variable is a stored property whose initialization is delayed. The initial value is computed only once.

  • A computed property is a value that is not stored anywhere. Instead, it is always computed on demand.

Limitations on lazy variables

There are some limitations when it comes to using lazy variables in Swift.

  • The use of lazy variables is restricted to structures and classes.

  • You cannot declare a lazy constant (lazy let) because then its value could not change.

  • A computed property cannot be a lazy variable. This is because a computed property is always recalculated when it is called.

  • Initialization of a lazy variable is not an atomic operation. Thus, lazy variables are not thread-safe.


You learned about stored properties and lazy stored properties. These are very helpful and important to use in your codebase. Go deep and do more practice with lazy stored properties.