Search code examples
swifttimersigabrt

Repeating Swift timer gives SIGABRT error


I am trying to make a repeating timer that will go off every second in the background in Swift, but once I run the code, it comes back SIGABRT.

var CAPS = 0

let timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(CAPSTimer), userInfo: nil, repeats: true)

@objc func CAPSTimer() {
    let closedAdds = Int(closedAddsCounter.text!)!
    var CAPSAddOne = closedAdds + CAPS
    closedAddsCounter.text = "\(CAPSAddOne)"
}

I've tried to change the code, but the only other one that works doesn't repeat.


Solution

  • try with this lines

    var CAPS = 0
    var CAPSAddOne = 0
    var closedAdds = 0
    override func viewDidLoad()
    {
       let timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(CAPSTimer), userInfo: nil, repeats: true)
       closedAdds = Int(closedAddsCounter.text!)!
       CAPSAddOne = closedAdds
    }
    
    @objc func CAPSTimer() {
        CAPSAddOne += CAPS
        closedAddsCounter.text = "\(CAPSAddOne)"
    }