此代码在计数器 Label.text 变量的第二个 + 符号上引发错误“二进制运算符 '+' 不能应用于 'String' 和 'Double' 类型的操作数”。如果我删除计数器 Label.text 中的 minutesLabel 之后的所有内容,它只会将错误放在这里,错误就会消失(这是用 Swift 编写的)。对于任何格式混乱,我也很抱歉,我是第一次使用。
func updateCounter(timer: NSTimer) {
let hours = floor(stopWatchTime / pow(60, 2))
let hoursInSeconds = hours * pow(60, 2)
let minutes = floor((stopWatchTime - hoursInSeconds) / 60)
let minutesInSeconds = minutes * 60
let seconds = floor((stopWatchTime - hoursInSeconds - minutesInSeconds) / 60)
let secondsInCentiseconds = seconds * 100
let centiseconds = stopWatchTime - hoursInSeconds - minutesInSeconds - secondsInCentiseconds
let hoursLabel = String(format: "%02.0f:", hours)
let minutesLabel = String(format: "%02.0f:", minutes)
let secondsLabel = String(format: "%02.0f:", seconds)
let centisecondsLabel = String(format: "%02.0f", centiseconds)
counterLabel.text = hoursLabel + minutesLabel + secondsLabel + centiseconds
stopWatchTime = stopWatchTime + 1