Keyword Lazy in Swift

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.

--

--

--

needone.app

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Java

Creating a movie recommendation AI with Python and MySQL

Daily Newsletter — 27th December 2020

We’ve Launched!

Virtual Private Cloud

Wireless Sensor Range vs. Scalability: Understanding the Key Trade-Offs

Just started my journey on FLUTTER

Things every developer should know to improve site performance

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Needone App

Needone App

needone.app

More from Medium

Introduction to Swift UI

Build an iOS app with NotificationCenter

Create a Modular iOS App in Xcode 13

Implementing Game Controller for iOS or Mac app