class A: Timer {
var myTimer: Timer!
}
class TimerTestViewController: UIViewController {
var a = A()
override func viewDidLoad() {
super.viewDidLoad()
a.myTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerRun), userInfo: nil, repeats: true)
RunLoop.current.add(a, forMode: RunLoop.Mode.common)
a.myTimer.fire()
}
}
Notice in RunLoop.current.add(a, forMode: .common) that i didn't add a.myTimer to runloop but “accidentally” added a to the runloop.
why does this code work at all?
scheduledTimerhas already added theTimerto aRunLoopand that's why the next line is not even necessary.See
Timer.scheduledTimer(timeInterval:target:selector:userInfo:repeats:)The second line passes with
aonly because you have declaredAto be aTimerwhich is probably an error: