-1

I am trying to set up a timer that counts from 60 seconds to 0 and display the count on my game scene. However, I keep running into issues when I do the following: In my GameScene.swift file I created the following:

 var gameTimer = 60
 var timer = Timer()
 @IBOutlet var timerLabel: UILabel!

under the GameScene class, then under the ViewDidLoad I did:

timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(GameViewController.startTimer), userInfo: nil, repeats: true)

where startTimer was a function with the following content:

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

and updateTimer is another function to update the timer:

    @objc func updateTimer(){
    gameTimer -= 1
    timerLabel.text = String(gameTimer)

}

But the game keeps crashing when I launch it. What am I doing wrong here? Any help is appreciated.

For those of you who asked about the error message: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

And it is pointing to this line of code:

        timerLabel.text = String(gameTimer)
Ali
  • 5
  • 3
  • what is the crash message? – Milan Nosáľ Feb 08 '18 at 21:29
  • When you post a question about a crash you must include, in the question, the complete and exact error message and you must point out the exact line of code causing the crash. Please [edit] your question to include this information (do not post it in a comment). – rmaddy Feb 08 '18 at 21:30
  • Why do you have a timer that runs every second which in turn creates another new timer that also runs ever second? That just can't be right. – rmaddy Feb 08 '18 at 21:33
  • I added the error message. – Ali Feb 08 '18 at 21:35
  • The timer is supposed to call startTimer every second, which in turn calls updateTimer to count down. – Ali Feb 08 '18 at 21:36
  • post the crash message – Gihan Feb 08 '18 at 21:39
  • I also tried with just one function. To updateTimer and within the ViewDidLoad to call that function only. It gave me the same message and pointed to the same line of code I added above (as the error). – Ali Feb 08 '18 at 21:39
  • at what line is the crash? – Milan Nosáľ Feb 08 '18 at 21:40
  • Milan - it's highlighting the line in the updateTimer() function: timerLabel.text = String(gameTimer) – Ali Feb 08 '18 at 21:43
  • @Ali it seems that your label is not connected to storyboard – Shehata Gamal Feb 08 '18 at 21:49

2 Answers2

1

If the line timerLabel.text = String(gameTimer) causes this crash, it means that the timerLabel variable holds nil at that point. Make sure that your @IBOutlet is connected with the label in the storyboards.

Also, unrelated to your problem right now, instead of

timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(GameViewController.startTimer), userInfo: nil, repeats: true)

call

startTimer()

in the viewDidLoad directly.

The timer created in startTimer() is responsible for updating the number every second, you should not start it again and again

Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
0

1- Declare timer like this

 var timer:Timer ?

2- it must be 1 timer say in viewDidLoad

 timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(GameViewController.startTimer), userInfo: nil, repeats: true)

with that method

@objc func updateTimer(){
  gameTimer -= 1
  timerLabel.text = String(gameTimer)
}

Also check timerLabel connection to stoyboard as the line

@IBOutlet var timerLabel: UILabel!

Explicit unwarp ! causes the crash if the label is not hooked to the label in storyboard

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • Thank you! That was it! I am new to Swift and I thought I could create the label without explicitly creating one in the main storyboard – Ali Feb 08 '18 at 22:07