【发布时间】:2019-03-08 17:23:50
【问题描述】:
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()
}
}
在RunLoop.current.add(a, forMode: .common) 中注意,我没有将a.myTimer 添加到runloop,而是“意外”将a 添加到了runloop。
为什么这段代码可以工作?
【问题讨论】: