【发布时间】:2019-01-17 10:04:28
【问题描述】:
在进入视图时,我调用一个函数来加载一个计时器,就像这样......
var count = 10
func startTimer() {
timer = Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(self.update), userInfo: nil, repeats: true)
}
而update 函数被赋予为..
@objc func update() {
while (count != 0) {
count -= 1
countdownLabel.text = "\(count)"
}
timer.invalidate()
}
但是当我看到这个视图时,会直接显示数字 0,而不是理想地显示序列中的所有数字 9,8,7,6,5,4,3,2,1,0
我在这里做错了什么..?
【问题讨论】:
-
while (count != 0) { count -= 1; ... },想想那个循环在几秒钟内做了什么:它一次计数到 0,然后立即使你的计时器失效。