【问题标题】:Why does Xcode say "expected declaration" with an arrow pointing to the word "timer"? [duplicate]为什么 Xcode 说“预期声明”,箭头指向“计时器”这个词? [复制]
【发布时间】:2017-04-23 20:46:38
【问题描述】:

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

【问题讨论】:

    标签: swift xcode


    【解决方案1】:

    在这种情况下,预期声明意味着代码必须在方法/函数中,例如

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-29
      • 2018-04-18
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      • 2013-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多