【问题标题】:NSTimer: floating point value can not be converted to UInt8 because it is greater than UInt8.maxNSTimer:浮点值无法转换为 UInt8,因为它大于 UInt8.max
【发布时间】:2014-12-31 22:14:14
【问题描述】:

我的代码返回错误:致命错误:浮点值无法转换为 UInt8,因为它大于 UInt8.max。按下按钮时会调用代码(在不同的 swift 文件中),但我知道问题不在于调用函数,所以我没有包含它。

代码:

//starts the timer
func startTimer(sender: AnyObject)
{
if !timer.valid
{
    timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: sender, selector:     Selector(updateTime()), userInfo: nil, repeats: true)
    startTime = NSDate.timeIntervalSinceReferenceDate()
    //disp = timerDisplay
}
}

//function that configures the timer
func updateTime()
{
var currentTime = NSDate.timeIntervalSinceReferenceDate()

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

//calculate the seconds in elapsed time
let seconds = UInt8(elapsedTime)
elapsedTime -= NSTimeInterval(seconds)

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

//add the leading zero for minutes, seconds and millseconds and store them as string constants
//let strMinutes = minutes > 9 ? String(minutes):"0" + String(minutes)
let strSeconds = seconds > 9 ? String(seconds):"0" + String(seconds)
let strFraction = fraction > 9 ? String(fraction):"0" + String(fraction)

//display the time left to a string
timerDisplay = "\(strSeconds):\(strFraction)"
}

func stopTimer()
{
timer.invalidate()
}

【问题讨论】:

  • 有什么问题?
  • @recursive 我想知道如何解决错误并让代码正常工作。
  • 我明白了。这个问题基本上在帖子标题中解释了。请问您为什么使用UInt8?它最多只能存储 255 个值,因此会受到限制。
  • 这就是教程所说的。我尝试了 UInt16,并得到了同样的错误。尝试 Uint32 或 Uint64 时,应用程序崩溃并且没有错误
  • 错误信息专门针对UInt8,所以如果您尝试UInt16并得到同样的错误,您可能没有执行您认为的代码。

标签: xcode swift timer int nstimer


【解决方案1】:

我会采取不同的方法,因为您需要多次表示 NSTimeInterval,因此您应该考虑添加一个只读计算属性作为项目的扩展,以返回它的字符串表示形式,如下所示:

extension NSTimeInterval {
    var time:String {
        return String(format:"%d:%02d:%02d.%02d", Int(self/3600.0), Int((self/60.0) % 60), Int((self) % 60 ), Int(self*100 % 100 ))
    }
}

class ViewController: UIViewController {
    //declarations
    @IBOutlet weak var tappedLabel: UILabel!
    @IBOutlet weak var timerLabel: UILabel!

    var startTime = NSTimeInterval()
    var timer = NSTimer()

    //starts the timer
    func startTimer(sender: AnyObject) {
        if !timer.valid {
            timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: sender, selector: "updateTime", userInfo: nil, repeats:true)
            startTime = NSDate.timeIntervalSinceReferenceDate()
        }
    }
    // calculates the elapsed time in seconds (Double = NSTimeInterval).extension NStimeInterval
    func updateTime() {
        //find the difference between the current time and the start time and return a string out of it
                // updates the text field
        timerLabel.text = (NSDate.timeIntervalSinceReferenceDate() - startTime).time
    }

【讨论】:

  • 谢谢!我在哪里放 let myTimeStringDescription = elapsedTime.time?
  • 您可以将扩展名放在一个单独的 swift 源文件中,并在任何 NSTimeInterval 对象的任何地方使用 .time 扩展名,它将从中返回一个字符串。
  • yourTextOutlet.text = elapsedTime.time
  • @LeoDabus -- 好回答我的兄弟,我很快就学会了新的,
  • 我从这个答案中学到了 3 种不同的东西。谢谢!!
【解决方案2】:

UNint8 最多只能存储 64k (64*1024) 的值。如果桶装满了,你就不能再往里面塞东西了。使用更大的桶Int 将处理数以亿计的数字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 2021-05-11
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 2019-01-09
    • 1970-01-01
    相关资源
    最近更新 更多