[英]Why use didSet when you can just mutate a variable and the value changes anyway?
I read through this SO didSet q&a and Apple's Property Observers and a few other posts. 我通读了didSetQ& Apple的《 Property Observers》和其他几篇文章。 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?
我似乎无法确定的是,如果您在不使用属性观察器的情况下更改变量而仍要进行更改,那么在对变量进行更改时使用didSet有什么好处?
Scenario 1: 方案1:
var someVal = 0
someVal = 10
// someVal now holds 10
Scenario 2: 方案2:
var someVal: Int = 0{
didSet{
}
}
someVal = 10
// again someVal now holds 10
Scenario 3: 方案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. 我在方案3中看到的唯一一件事是,如果不满足条件,则someVal不会改变。 But instead of adding it inside the didSet I can simply do this and the same exact thing will occur.
但是,除了将其添加到didSet中之外,我可以简单地执行此操作,并且将发生完全相同的事情。
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? 那么其他然后在didSet观察器中添加条件有什么好处呢?
Well, it's an observer. 好吧,它是一个观察者。 Many times you want to react to changing a value of a viewController property.
很多时候,您想对更改viewController属性的值做出反应。 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. 现在,如果您在10个不同的地方修改该属性,
didSet
必将相同的代码复制/粘贴10次不同的时间didSet
观察器将负责该工作。
Eg: 例如:
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?
在第二种情况下,即使我们愿意接受复制/粘贴(这不行),如何确保在每次修改值时都不会忘记添加postProcessing?
Suppose you have an array the feeds a tableView
that array can be changed any where in code instead of writing 假设您有一个数组,该表将提供一个
tableView
,该数组可以在代码中的任何位置更改,而无需编写
self.tableView.reloadData()
every change it's rob oust to use didSet 每次更改都不要使用didSet
var someVal: [String] = [] {
didSet{
self.tableView.reloadData()
}
}
this is a simple example there are many useful cases 这是一个简单的例子,有很多有用的情况
didSet is a property observer in Swift didSet是Swift中的属性观察器
didSet{
}
didSet is a property observer. didSet是一个属性观察器。 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) 我认为我们有一个UILabel(与MVC中一样)
@IBOutlet weak private var someLabel: UILabel!
and we have its value in another variable in our program model as in (MVC) as 并且我们在程序模型中的另一个变量中具有它的值,如(MVC)
var someValue: Int = 10
so when ever I update my variable in the model the UILabel in the view should also be updated. 因此,每当我在模型中更新变量时,视图中的UILabel也应更新。
This can be done by didSet as 可以通过didSet作为
var someValue: String = "Hello"{
didSet{
someLabel.text = someValue
}
}
So here you can get some intuition of what didSet didSet does. 因此,在这里您可以直观了解didSet didSet的功能。 In the above example whenever the value of the variable in the model changes the view is also updated.
在上面的示例中,只要模型中变量的值发生更改,视图也会被更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.