lazy var in ios swift

This article explains the working of lazy var in swift.You must have some knowledge in closures.

Abhimuralidharan
3 min readMay 14, 2017

Read this article for more info on closures.

While dealing with iOS app development we should be really very much conscious about the amount of memory used by the application. If the app is a complex one, then the memory issues are one of the major challenges for the developer. So, the developer should be really writing an optimised code which consider memory allocation at first place. The developer need to avoid doing expensive work unless it’s really needed. These complex allocations will take more time and it will seriously affect the performance of the application as well.

Swift has a mechanism built right into the language that enables just-in-time calculation of expensive work, and it is called a lazy variable. These variables are created using a function you specify only when that variable is first requested. If it’s never requested, the function is never run, so it does help save processing time.

Apple’s official documentation says:

A lazy stored property is a property whose initial value is not calculated until the first time it is used. You indicate a lazy stored property by writing the lazy modifier before its declaration.

--

--