2016-11-22 45 views
1

此代碼必須被轉換爲斯威夫特3.Xcode的斯威夫特3計時器錯誤

func checkIfCorrect (_ buttonPressed:Int) { 
     if buttonPressed == playlist[numberOfTaps] { 
      if numberOfTaps == playlist.count - 1 { // we have arrived at the last item of the playlist 

       let time = DispatchTime(uptimeNanoseconds: DispatchTime.now()) + Double(Int64(NSEC_PER_SEC))/Double(NSEC_PER_SEC) 

       DispatchQueue.main.asyncAfter(deadline: time, execute: { 
         self.nextRound() 
       }) 

       return 
      } 

代碼返回,說:「無法將類型的價值‘DispatchTime’預期參數類型UINT64」錯誤。這將是一個很好的解決辦法?

回答

1

在斯威夫特3,它更容易

例如第二,從現在DispatchTime是:

let time : DispatchTime = .now() + .seconds(1) 

這些其他單位可供選擇,太:

  • .milliseconds()
  • .microseconds()
  • .nanoseconds()
0

這是Swift 3表現力的一個很好的例子。

DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: { 
        self.nextRound() 
      })