我需要将字符串转换为 Int,以便可以使用 if > 语句来启用和禁用按钮。正如您从 updateStopwatch 中的注释行中看到的那样,我尝试了多种转换方法,但没有任何成功
我的班级看起来像
class Stopwatch {
var startTime:Date?
func startTimer() {
startTime = Date();
}
func elapsedTimeSinceStart() -> String {
var elapsed = 0.0;
if let elapsedTime = startTime {
if firstHalfTime {
elapsed = elapsedTime.timeIntervalSinceNow
} else {
elapsed = elapsedTime.timeIntervalSinceNow - 45*60
}
}
elapsed = -elapsed
let minutes = Int(floor((elapsed / 60)));
let seconds = Int(floor((elapsed.truncatingRemainder(dividingBy: 60))));
// print(elapsed)
let timeString = String(format: "%02d:%02d", minutes, seconds)
// print(timeString)
return timeString
}
}
我的更新计时器功能
func updateStopwatch() {
let stopWatchString = stopWatch.elapsedTimeSinceStart()
stopwatchLabel.text = stopWatchString
// let minutesString:Int = Int(stopWatchString)!
// minutes = minutesString
if minutes > 1 {
endFirstHalf.isEnabled = true
self.endFirstHalf.alpha = 1
}
}