我在func pauseAction
下面的行显示“预期声明”并且有一个指向“计时器”一词的箭头
var timeLeft = 10
var timer = Timer()
var score: Int = 0 {
didSet {
scoreLabel.text = "\(score)"
}
}
@IBOutlet var scoreLabel: UILabel!
@IBAction func tapButton(_ sender: Any) {
score += 1
}
@IBAction func pauseAction(_ sender: Any) {
}
timer = Timer.scheduledTimer(timeInterval: 1.0, target:self, selector: Selector(("updateTimer")), userInfo: nil, repeats: true)
func updateTimer() {
timeLeft -= 1
timeLabel.text = String(timeLeft)
if timeLeft == 0 {
timer.invalidate()
}
}
答案 0 :(得分:1)
在这种情况下,期望声明表示代码必须位于方法/函数中,例如
func startTimer()
{
timer = Timer.scheduledTimer(timeInterval: 1.0, target:self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
}