What is a lazy stored property?
- Used to defer expensive computation until property is used for the first time.
- Must always be declared as a
varas its initial value might not be set until after initialisation completes. - Add the
lazykey word in front of thevarkeyword. : Typefollowed by closure syntax.()on the end of the closure.- Stored properties and lazy stored properties cannot be defined in an extension.
lazy var something: Int {
// Carry out some expensive computation
return result
}()
< All Posts