Programming in Swift: Functions and Types · Stored Properties | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5429279-programming-in-swift-functions-and-types/lessons/29

Hi there, I am a little unclear on the didSet and willSet property observers and how they fit into what stored properties do. The difference between a stored property on an instance vs. stored property on a type made sense but I’m having trouble thinking about when the didSet and willSet would be useful in an app, didSet seems almost like a default option or guidelines when trying to change the firstName variable on a specific instance later on. willSet seems to be saying what will happen to the first instance of firstName and what it will be updated to?

Thanks so much for the support!
Mitch

Hi! Great question.

didSet can be a way to set guidelines for a property, like you said!

It can also create a sort of dependency on the value of a property. A question to ask yourself might be: “When the value of this property changes, should something else happen?” That could be changing the value of another property, calling a method, reloading a view, saving some data, etc.

You’ve got the right idea about willSet! As for when willSet is useful in an app, that’s a tough question. I have used it as a basic debugging tool to see what is happening before a property changes. Other than that, I’ve never personally had a practical use for it, over didSet. :woman_shrugging:t4:

Your video player DESPERATELY needs a 15 second rewind button. Your videos take forever to get through just trying to rewind it to the correct time stamp where you lose me.

Hi! There should be a way for you to do quick rewinding you’re after. In a standard web browser, try using the right and left arrow keys to jump 5 seconds forward and back (sometimes I need to pause the video before they will work). You can do the same in Safari on the iPad with a double tap on the left or right side of the video. If you ever try our iOS app, that does have a standard set of 15 second buttons.