0
我想在Swift中創建一個節拍器應用程序,並且我遇到了一些尷尬的結果。下面是代碼:意外的分裂結果在Swift中
@IBAction func playStop(sender: AnyObject) {
if !timer.valid{
println(Double(60/tempo))
timer = NSTimer(timeInterval: NSTimeInterval(Double(60/(tempo))), target: self, selector: "update", userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSDefaultRunLoopMode)
}
else{
timer.invalidate()
}
}
func control(_control: NSControl,
textShouldEndEditing fieldEditor: NSText) -> Bool
{
if (tempoLabel.integerValue <= 0)
{
return false
}
tempo = tempoLabel.integerValue
timer.invalidate()
println(tempo)
playStop(self)
return true
}
節奏由用戶設置爲與數字格式化一個NSTextfield
,通過缺省設定爲60。當文本字段的值發生變化時,控制檯明確寫入tempo
變量的正確值,並且只有當速度設置爲60時,程序才能正常工作。當值不是60時,我得到結果Double(60/tempo)
每次都等於0,並且調用update
函數就好像處於無限循環一樣。使用Double(60/tempo)
,Float(60/tempo)
或只是60/tempo
不會改變任何東西。我不明白爲什麼我會得到這樣的結果,或者我如何解決這個問題。
試試'60.0 /雙(節奏)'。首先做的是做整數除法,得到一個整數(在你的情況下爲零),然後將其轉換爲「Double」。你想對兩個數字使用'Double'進行分割。 – vacawama 2014-10-29 00:45:31