0

此代码在计数器 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
4

1 回答 1

0

您忘记将 Label 添加到变量centiseconds中,因为它不是字符串centisecondsLabel而是String. 这就是您收到二元运算符错误的原因。使用以下代码:

 counterLabel.text = hoursLabel + minutesLabel + secondsLabel + centisecondsLabel

这将解决问题!

于 2015-04-16T06:26:14.537 回答