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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.