计时器调用函数时,线程1:EXC_BAD_INSTRUCTION(代码= EXC_i386_INVOP,子代码= 0x0)

时间:2017-07-18 21:45:44

标签: ios swift

我遇到两个问题,第一个问题是每次Timer调用此函数时,我都会收到上述错误信息(包括漂亮的崩溃):

func updateTime() {

    let currentTime = Int(player.currentTime)

    let minutes = currentTime/60
    let seconds = currentTime - minutes * 60

    if seconds < 10 {

        tempo.text = String(minutes) + ":0" + String(seconds)

    } else {

        tempo.text = String(minutes) + ":" + String(seconds)

    }

    durationSliderOutlet.value = Float(player.currentTime)

    if currentTime == Int(player.duration - 1) {

        playpauseOutlet.setTitle("Play", for: .normal)
        isPlaying = false

    }

    print("currentTime: \(currentTime)")
    print(Int(player.duration))
    print("minutes: \(minutes)")
    print("seconds: \(seconds)")
    print(isPlaying)

}

我只是想在标签中显示音频文件的当前时间,如果我注释掉这段代码

if seconds < 10 {

    tempo.text = String(minutes) + ":0" + String(seconds)

} else {

    tempo.text = String(minutes) + ":" + String(seconds)

}

......一切正常!

当我按下按钮时,第二个引发相同的崩溃和错误......

 @IBAction func playpauseButton(_ sender: UIButton) {

    if isPlaying == false {

        playpauseOutlet.setImage(#imageLiteral(resourceName: "pause.png"), for: .normal)
        player.play()
        isPlaying = true


    } else {

        playpauseOutlet.setImage(#imageLiteral(resourceName: "play.png"), for: .normal)
        player.pause()
        isPlaying = false


    }
}

注意:(#imageLiteral(resourceName: "play.png")显示为一个小的白色矩形。如果我尝试类似playpauseOutlet.setImage(UIImage(named: "play.png"), for: .normal)的内容,它也会崩溃。

我假装使用此代码的方法是根据isPlaying状态将按钮的“播放”图像更改为“暂停”图像,反之亦然。提前谢谢!

0 个答案:

没有答案