What is the difference between willSet
- didSet
, and get
- set
, when working with this inside a property?
From my point of view, both of them can set a value for a property. When, and why, should I use willSet
- didSet
, and when get
- set
?
I know that for willSet
and didSet
, the structure looks like this:
var variable1 : Int = 0 {
didSet {
println (variable1)
}
willSet(newValue) {
..
}
}
var variable2: Int {
get {
return variable2
}
set (newValue){
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…