Group Group Group Group Group Group Group Group Group

Programming in Swift: Functions and Types · Stored Properties |

This is a companion discussion topic for the original entry at

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!

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: