线程1:当使用带有两个参数的选择器时,EXC_BAD_ACCESS

时间:2019-03-28 18:34:29

标签: swift4.2

由于两个参数,它在runTimer()内部崩溃。如果我只放一个或不放-它正常工作。 崩溃至AppDelegate,错误为

  

线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x4)

var timer = Timer()
var seconds = Int()

@IBOutlet weak var twoMinView: UIView!
@IBOutlet weak var twoMinLabel: UILabel!

@objc func twoMinTimer() {
        seconds = 120
        runTimer(view: twoMinView, label: twoMinLabel)
}

@objc func runTimer(view: UIView, label: UILabel) {
    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(MyViewController.updateTimer(view:withLabel:)), userInfo: nil, repeats: true)
}

@objc func updateTimer(view: UIView, withLabel label: UILabel) {
    seconds -= 1

    if seconds < 1 {
        view.isHidden = true
        timer.invalidate()

        if view == twoMinView {
            streamView.isHidden = false
        }
    }

    label.text = timeString(time: TimeInterval(seconds))
}

@objc func timeString(time: TimeInterval) -> String {
    let minutes = Int(time) / 60 % 60
    let seconds = Int(time) % 60

    return String(format: "%02i : %02i", minutes, seconds)
}

1 个答案:

答案 0 :(得分:0)

只需删除null并将updateTimer替换为下一个

runTimer