5

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.

stumped
  • 3,235
  • 7
  • 43
  • 76

3 Answers3

6

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.

Ben Lu
  • 2,982
  • 4
  • 31
  • 54
4

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
Ivan Rigamonti
  • 696
  • 5
  • 7
1

willSet and didSet are called Property Observers, as the name say they observe and respond to changes in a property’s value and it is completely up to you when to use it depending a log in what you want to archive, basically willSet happens just before the value get set and didSet happens just after, from apple documentation:

willSet is called just before the value is stored. didSet is called

immediately after the new value is stored.

 class StepCounter {
    var totalSteps: Int = 0 {
        willSet(newTotalSteps) {
            print("About to set totalSteps to \(newTotalSteps)")
        }
        didSet {
            if totalSteps > oldValue  {
                print("Added \(totalSteps - oldValue) steps")
            }
        }
    }
}

let stepCounter = StepCounter()
stepCounter.totalSteps = 200
// About to set totalSteps to 200
// Added 200 steps
stepCounter.totalSteps = 360
// About to set totalSteps to 360
// Added 160 steps
stepCounter.totalSteps = 896
// About to set totalSteps to 896
// Added 536 steps

**Two important notes:

Note 1:**

If you assign a value to a property within its own didSet observer, the new value that you assign will replace the one that was just set.

Note 2:

willSet and didSet observers are not called when a property is set in an initializer before delegation takes place.

Community
  • 1
  • 1
Icaro
  • 14,585
  • 6
  • 60
  • 75