看看这个代码:

var timepenalty = UInt8(0)
var currentTime = NSDate.timeIntervalSinceReferenceDate()

// Find the difference between current time and start time
var elapsedTime: NSTimeInterval = currentTime - startTime

let adjustedTime = UInt8(timepenalty + elapsedTime)

error-
"Could not find an overload for "+" that accepts the requested arguments.
"

这是一款为秒表式计时器增加时间的游戏,每次玩家出错时都会这样做。当我只使用整数而不是elapsedTime变量时,代码工作:
let adjustedTime = UInt8(elapsedTime + 5)

但是用变量替换5会产生一个错误。
下面是updateTime函数的完整代码:
func updateTime() {


        var currentTime = NSDate.timeIntervalSinceReferenceDate()

        // Find the difference between current time and start time
        var elapsedTime: NSTimeInterval = currentTime - startTime

        let adjustedTime = UInt8(timepenalty + elapsedTime)
        // calculate the minutes in elapsed time
        let minutes = UInt8(elapsedTime / 60.0)
        elapsedTime -= (NSTimeInterval(minutes) * 60)

        // calculate the seconds in elapsed time


        seconds = UInt8(elapsedTime)
        elapsedTime -= NSTimeInterval(seconds)
//        seconds += timepenalty

        // find out the fraction of millisends to be displayed
        let fraction = UInt8(elapsedTime * 100)

        //            if seconds > 20 {
        //                exceedMsgLabel.text = "超过20秒了"
        //            }

        // add the leading zero for minutes, seconds and millseconds and store them as string constants


        let startMinutes  = minutes > 9 ? String(minutes):"0" + String(minutes)
        let startSeconds  = seconds > 9 ? String(seconds):"0" + String(seconds)
        let startFraction = fraction > 9 ? String(fraction):"0" + String(fraction)

        displayTimeLabel.text = "\(startMinutes):\(startSeconds):\(startFraction)"
        var penalty = String(timepenalty)
        penaltylabel.text = "+ " + penalty


    }

最佳答案

这一行:

let adjustedTime = UInt8(timepenalty + elapsedTime)

试图添加一个UInt8(时间惩罚)和一个NSTimeInterval(double,elapsedTime)失败,因为Swift中没有隐式类型转换。更改为:
let adjustedTime = timepenalty + UInt8(elapsedTime)

在添加之前将NSTimeInterval转换为UInt8。

关于ios - 找不到接受提供的参数的“+”重载,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28607700/

10-13 03:49