Step into Swift logo

What is a lazy stored property?

Lazy Properties

What is a lazy stored property?

  • Used to defer expensive computation until property is used for the first time.
  • Must always be declared as a var as its initial value might not be set until after initialisation completes.
  • Add the lazy key word in front of the var keyword.
  • : Type followed by closure syntax.
  • () on the end of the closure.
  • Stored properties and lazy stored properties cannot be defined in an extension.
lazy var something: Int {
  // Carry out some expensive computation

  return result

< All Posts