2014-11-08 47 views
0

我從Swift編程語言粘貼此代碼PDF屬性章節,我想知道如何使用willSet中提供的定製和didSet 我的意思是在什麼情況下此代碼使用println在willSet和didSet中提供並打印「即將設置步驟tp(newTotalSteps)「或另一個...?如何使用屬性觀察器中提供的定製?

class StepCounter { 
    var totalSteps:Int=0 
     { 
      willSet(newTotalSteps){ 
       println("about to set total steps to\(newTotalSteps)") 
      } 
     didSet{ 
      if totalSteps>oldValue 
      { 
       println("Added\(totalSteps-oldValue)") 
      } 
     } 
    } 
} 
let stepcounter=StepCounter() 
stepcounter.totalSteps=200 
stepcounter.totalSteps=360 

回答

1

打開一個操場。複製&將代碼粘貼進來。然後在右側面板上單擊+,您可以看到控制檯輸出並播放它。

enter image description here

相關問題