Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Intermediate Swift 2 Properties Lazy Stored Properties

Carson Carbery
Carson Carbery
9,876 Points

When to use lazy properties

Hi I have a question about the lazy stored properties. So I understand what was mentioned in the video, however the teacher didn't mention any reason or situation where you should not use Lazy properties. So does that mean that in fact it could be good practice to define all stored properties that are not used immediately when the App load as Lazy? Thanks for the clarification on this.

1 Answer

Carson, great question!

I'm sure you read the Apple documentation, and noticed that it doesn't say anything about when not to use it.

"Lazy properties are useful when the initial value for a property is dependent on outside factors whose values are not known until after an instance’s initialization is complete. Lazy properties are also useful when the initial value for a property requires complex or computationally expensive setup that should not be performed unless or until it is needed."

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html

I suppose we could conclude that we shouldn't use them except in cases like the ones mentioned.

I also found one commentator on lazy properties noting a possible problem: "Accessing the lazy property is a mutating operation because the property’s initial value is set on the first access. When a struct (which is a value type) contains a lazy property, any owner of the struct that accesses the lazy property must therefore declare the struct as a variable, too, because accessing the property means potentially mutating its container. "

His example is:

struct Image {
    lazy var metadata: [String:AnyObject] = {
        // Load image file and parse metadata, expensive
        // ...
        return ...
    }()
}

And he points out that this requires any user of the struct to create images as vars, not constants:

let image = Image()  //error

http://oleb.net/blog/2015/12/lazy-properties-in-structs-swift/

I couldn't find much else in the way of any warnings.

Another commentator seems in favor!

"As you can see, using lazy loading is easier than ever in Swift. Use it whenever possible, since it’ll save memory and make your code more efficient."

https://www.natashatherobot.com/swift-lazy/

Looks like "best practice" is still evolving! Your question may help that process.