Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use set vs willSet in Swift? [duplicate]

Tags:

class

swift

set

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.

like image 452
stumped Avatar asked Jun 22 '15 21:06

stumped


People also ask

What is difference between willSet and didSet in Swift?

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.

What is willSet in Swift?

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.

What is the use of didSet in Swift?

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.

What is property Observer in Swift?

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.


2 Answers

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.

like image 128
Ben Lu Avatar answered Sep 18 '22 22:09

Ben Lu


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:

  • do whatever you you need to do prior to setting the value in willSet
  • set the value in set
  • do whatever you need to to after having set the value (e.g. logging or cleaning up) in didSet
like image 42
Ivan Rigamonti Avatar answered Sep 19 '22 22:09

Ivan Rigamonti