2014-10-28 3 views
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을 사용하면 아무 것도 변경되지 않습니다. 왜 그런 결과가 나오는지, 어떻게 해결할 수 있는지 이해할 수 없습니다.

+3

시도'60.0/더블 (템포)'마십시오. 먼저 나누기 작업을 수행하면 정수로 나뉘며 정수로 나눠진 결과 (0은 귀하의 경우) 결과가 'Double'으로 변환됩니다. 두 숫자 모두에 대해 'Double'을 사용하여 나누기를 원합니다. – vacawama

답변

1

두 배로 분자 또는 분모를 전송하십시오.

는 60.0/템포 또는 60/더블 (템포)

관련 문제