Swift Quick Tip: lazy variables

Dave Poirier
Swift2Go
Published in
1 min readAug 1, 2019

--

Photo by Fotis Fotopoulos on Unsplash

You can replace:

With:

The object SomeObject will only be instantiated when referenced/needed. This lazy behaviour may potentially save memory in case the object is never referenced in a given code path, and guarantee the object is always defined if needed without having to resort to using “!” and potentially referencing the object before it is initialized.

--

--

Dave Poirier
Swift2Go

Senior iOS Developer | Mobile Security And Reliability Specialist