Wouldn't both happen at almost the same time? Would the time lapse be so minuscule that it doesn't really matter which I put code in?
Edit: my question is different than that link because I am talking about the functions of set and willSet whereas the link is talking about the functions of willSet and didSet. Therefore, two different keywords being discussed. I know the difference between willSet and didSet and would like to know more about set and willSet.
willSet is called before the data is actually changed and it has a default constant newValue which shows the value that is going to be set. didSet is called right after the data is stored and it has a default constant oldValue which shows the previous value that is overwritten.
In Swift, you can attach property observers to variables to run code when the variable changes. These property observers are called willSet and didSet. The former runs right before the property changes, and the latter runs right after the changes were made.
Swift's solution is property observers, which let you execute code whenever a property has changed. To make them work, we use either didSet to execute code when a property has just been set, or willSet to execute code before a property has been set.
Property Observers. Property observers observe and respond to changes in a property's value. Property observers are called every time a property's value is set, even if the new value is the same as the property's current value. You can add property observers in the following places: Stored properties that you define.
You all have done a great job explaining this concept of willSet
/ didSet
with official references. I'd like to address it in my own words:
The MOST prominent difference between set
and willSet
is that
Properties with
set
/get
do NOT store values!!!
Please inscribe this line on your Swifty brain.
set
/ get
act basically like two methods that are evoked by calling upon the property. That means, declaring a property with set
/ get
does not allocate the memory for that property.
On the contrary, properties with
willSet
/didSet
DO store values.
And these two methods are executed before / after writing the new value to the property. Other answers have explained these two methods in depth.
set and willSet have two completely different purposes.
set is used similarly to setter methods in other languages, thus the programmer clearly knows why and when he wants/needs to use it.
willSet is comparable to a trigger. So, whenever the value will be set - and this could be outside of the programmer's influence - this function will be called (except for initializers as stated in other answers).
Imagine a public class that you have created and that you offer to other programmers as part of your framework. Within a willSet you could trigger other functions (e. g. writing a log entry with timestamp), whenever the value will be set. In addition it allows you to take any actions before the value is set, e.g. saving the old value to somewhere else before it gets overwritten.
Of course, you could do everything I described within the set function without using willSet.
But willSet and didSet give you the opportunity to write code which is better readable:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With