Keyword Lazy in Swift

Needone App
Dec 31, 2020

lazy variables are variables will be computed until accessing. In other words, lazy variables won't have the real value until get called/used. As lazy variables can be re-assigned, so it can't be used on constants which normally defined by let.

One example of using lazy variables:

Scenarios of use

One of the good use scenarios is we can use the lazy feature on View initialization setup. For example, if the view have multiple states, such as empty list or populated list, they can be lazily loaded when in different situation, so the compiler won’t have to compute both of the two views (empty and populated) at one time.

What about Objective-C

Objective-C doesn’t natively support the lazy, but some simple workout can be easily implemented.

Reference

https://needone.app/keyword-lazy-in-swift/

Originally published at https://needone.app on December 31, 2020.

--

--