I've a timer, after clicking a button I want the timer to begin, and when the timer gets 0 I want to stop the timer. I know I can do that with invalidate() function, but I can't access the timer here's code
var timer = 5 {
didSet {
label.text = String(timer)
}
}
@IBAction func onClickAnimate(_ sender: Any) {
let timerCount = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateCounter), userInfo: nil, repeats: true)
}
@objc func updateCounter(){
if timer > 0 {
timer -= 1
}
if timer == 0 {
timer = 5
timerCount.invalidate() // error is here
}
}
If I decide to create timerCount globally I get an error unrecognized selector sent to instance 0x600003b466a0"
any solution will be appericated
Solution 1 : make a variable
Solution 2 : use block