为什么Xcode用箭头指向“计时器”这个词说“预期声明”?

时间:2017-04-23 06:28:21

标签: swift xcode

我在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()
    }
}

1 个答案:

答案 0 :(得分:1)

在这种情况下,期望声明表示代码必须位于方法/函数中,例如

func startTimer()
{
    timer = Timer.scheduledTimer(timeInterval: 1.0, target:self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
}