1

I read through this SO didSet q&a and Apple’s Property Observers and a few other posts. What I can’t seem to wrap my head around is what is the benefit of using didSet when mutating a variable when if you change the variable without using a property observer it’s going to change anyway?

Scenario 1:

var someVal = 0

someVal = 10

// someVal now holds 10

Scenario 2:

var someVal: Int = 0{

    didSet{
    }
}

someVal = 10

// again someVal now holds 10

Scenario 3:

var someVal: Int = 0{

    didSet{

       if someVal > oldValue{

            someVal = newValue
       }
    }
}

someVal = 10

// once again someVal holds a value of 10

The only thing I see in Scenario 3 is that if the condition isn’t met then someVal won’t change. But instead of adding it inside the didSet I can simply do this and the same exact thing will occur.

var someVal = 0

var anotherVal = 10

if someVal < anotherValue{
     someVal = anotherValue
}

// once again someVal holds a value of 10

So other then adding a condition inside a didSet observer what is the benefit?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256

3 Answers3

4

Well, it's an observer. Many times you want to react to changing a value of a viewController property. Now if you modify that property on 10 different places, you don't have to copy/paste the same code 10 different times - the didSet observer will take care of that.

E.g.:

var someVal: Int = 0 {
    didSet {
        somePostprocessing()
    }
}


someVal = 10

// a bit later

someVal = 15

// etc.

is much better than:

var someVal: Int = 0


someVal = 10
somePostprocessing()

// a bit later

someVal = 15
somePostprocessing()

// etc.

In first case you can be sure that anytime the value changes, the postProcessing will happen. In the second case, even if we would be willing to accept copy/pasting (which is not OK), how can you be sure that you won't forget to add postProcessing in every case you modify the value?

Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
  • I understand your example and thanks for the clarity. It seems like the greatest benefit is you don’t have to repeat code? Basically D.R.Y. – Lance Samaria Feb 15 '18 at 22:01
  • @LanceSamaria to me it seems so :).. I use it quite often (instead of setters as they are used e.g. in Java) – Milan Nosáľ Feb 15 '18 at 22:02
  • Thanks out of everything I’ve read your the only person who gave a very simple example and explanation. I’m sure it’ll help others. I might post a question about getters and setters also and I’ll message you when I do. ✌✌ – Lance Samaria Feb 15 '18 at 22:06
2

Suppose you have an array the feeds a tableView that array can be changed any where in code instead of writing

 self.tableView.reloadData()

every change it's rob oust to use didSet

var someVal: [String] = [] { 
   didSet{ 
    self.tableView.reloadData()
   }
}

this is a simple example there are many useful cases

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
2

didSet is a property observer in Swift

didSet{

}

didSet is a property observer. It is used to perform some tasks when a particular value is set.

For example:

I have a UILabel in our view (as in MVC)

@IBOutlet weak private var someLabel: UILabel!

and we have its value in another variable in our program model as in (MVC) as

var someValue: Int = 10

so when ever I update my variable in the model the UILabel in the view should also be updated.

This can be done by didSet as

var someValue: String = "Hello"{
    didSet{
        someLabel.text = someValue
    }
}

So here you can get some intuition of what didSet didSet does. In the above example whenever the value of the variable in the model changes the view is also updated.