Lazy Stored Properties

Sometimes a stored property’s value cannot be assigned immediately. The necessary information may be available but computing the values of a property immediately would be costly in terms of memory or time. Or, perhaps a property depends on factors external to the type that will be unknown until after the instance is created. These circumstances call for lazy loading.

In terms of properties, lazy loading means that the calculation of the property’s value will not occur until the first time it is needed. This delay defers computation of the property’s value until after the instance is initialized. This means that lazy properties must be declared with var, because their values will change.

Create a new lazy ...

Get Swift Programming: The Big Nerd Ranch Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.