I am a development novice and I am currently learning Swift 4. I am writing an egg timer app and I am confused. I have created a function to decrease the time within the scheduledTimer
. However, it will not let me compile and build until I create the decreaseTimer()
function with @objc.
Why is this? I am confused lol. My code is working but just want to know how ?
Here is my code:
import UIKit
class ViewController: UIViewController {
var timer = Timer()
var time = 210
@objc func decreaseTimer() {
if time > 0 {
time -= 1
timerLabel.text = String(time)
} else {
timer.invalidate()
}
}
@IBOutlet weak var timerLabel: UILabel!
@IBAction func play(_ sender: Any) {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.decreaseTimer), userInfo: nil, repeats: true)
}
Any advice would be most appreciated. Thank you