You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classStepCounter{vartotalSteps:Int=0{
willSet {print("will set total to a new value \(newValue)")}
didSet {
if (totalSteps > oldValue){print("add \(totalSteps - oldValue) steps")}else{print("退步了\(oldValue - totalSteps)")}}}}letstes=StepCounter()
stes.totalSteps =200
stes.totalSteps =360
stes.totalSteps =896
varcount:Int=0{
willSet{print("about to use \(newValue)")}
didSet {
if count > oldValue {print("add\(count - oldValue)")}}}
count =10
if count ==10{print("ten")}
The text was updated successfully, but these errors were encountered:
1. 属性观察者
willSet
会在该值被存储之前被调用。didSet
会在一个新值被存储后被调用。willSet
实现中为这个参数定义名字。如果你没有为它命名,那么它会使用默认的名字newValue
。didSet
观察者,一个包含旧属性值的常量形式参数将会被传递。你可以为它命名,也可以使用默认的形式参数名oldValue
。如果你在属性自己的didSet
观察者里给自己赋值,你赋值的新值就会取代刚刚设置的值。The text was updated successfully, but these errors were encountered: