Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode NSTimer:无法将浮点值转换为UInt8,因为它大于UInt8.max_Xcode_Swift_Timer_Int_Nstimer - Fatal编程技术网

Xcode NSTimer:无法将浮点值转换为UInt8,因为它大于UInt8.max

Xcode NSTimer:无法将浮点值转换为UInt8,因为它大于UInt8.max,xcode,swift,timer,int,nstimer,Xcode,Swift,Timer,Int,Nstimer,我的代码返回错误:致命错误:浮点值无法转换为UInt8,因为它大于UInt8.max。当按下按钮时(在不同的swift文件中)会调用代码,但我知道问题不在于调用函数,所以我没有包含它 代码: UNint8最多只能存储64k(64*1024)的值。如果桶满了,你就不能再往里面塞东西了。使用更大的存储桶Int将处理无数的数字。UNint8最多只能存储64k(64*1024)的值。如果桶满了,你就不能再往里面塞东西了。使用较大的桶 int >代码>将处理数十亿位数。< p>我将采取不同的方法,因为您需

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

代码:


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

UNint8
最多只能存储64k(64*1024)的值。如果桶满了,你就不能再往里面塞东西了。使用较大的桶<代码> int >代码>将处理数十亿位数。

< p>我将采取不同的方法,因为您需要多次表示NSTEMETAL间隔,因此您应该考虑将只读计算的属性添加到项目的一个扩展,以返回如下的字符串表示:

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
    }

我将采取一种不同的方法,因为您需要多次表示NSTEMETAL间隔,因此您应该考虑将只读计算属性添加到项目的扩展中,以返回它的字符串表示如下:

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
    }


问题是什么?@recursive我想知道如何解决错误并使代码正常工作。我明白了。这篇文章的标题基本上解释了这个问题。请问您为什么要使用
UInt8
?它最多只能存储255个值,所以这将是一个限制。这就是教程所说的。我试过UInt16,但也犯了同样的错误。当尝试Uint32或Uint64时,应用程序崩溃并且没有给出错误。错误消息特别指的是
UInt8
,因此如果您尝试
UInt16
,并得到相同的错误,您可能没有执行您认为自己是的代码。问题是什么?@recursive我想知道如何解决错误并使代码正常工作。我明白了。这篇文章的标题基本上解释了这个问题。请问您为什么要使用
UInt8
?它最多只能存储255个值,所以这将是一个限制。这就是教程所说的。我试过UInt16,但也犯了同样的错误。当尝试Uint32或Uint64时,应用程序崩溃并且没有给出错误。错误消息特别指的是
UInt8
,因此如果您尝试
UInt16
,并得到相同的错误,您可能没有执行您认为正确的代码。谢谢!我应该将myTimeStringDescription=elapsedTime.time放在哪里?您可以将扩展名放在单独的swift源文件中,并将.time扩展名与任何NSTimeInterval对象一起使用。它将从中返回一个字符串。yourTextOutlet.text=elapsedTime。time@LeoDabus回答得好,我的兄弟,我在斯威夫特学会了新的,我从这个答案中学到了三个不同的东西。非常感谢。谢谢我应该将myTimeStringDescription=elapsedTime.time放在哪里?您可以将扩展名放在单独的swift源文件中,并将.time扩展名与任何NSTimeInterval对象一起使用。它将从中返回一个字符串。yourTextOutlet.text=elapsedTime。time@LeoDabus回答得好,我的兄弟,我在斯威夫特学会了新的,我从这个答案中学到了三个不同的东西。非常感谢。