title: What is the best method in preventing this Optional-related crash?
body: <p><strong>Context</strong></p>
  <p>I am beginner learning Swift and I'm trying to make sure I understand this bug &quot;fix&quot;. I followed along a Youtube video showing how to create a timer in Xcode. I changed some things around in effort to learn some things but the issue that arose was from the original code.</p>
  <p><strong>The Bug</strong></p>
  <p>Every time I started the simulator, if I pressed the Stop or Reset button without first starting the timer, the app would crash and show this error on the same line as <code>timer.invalidate()</code>:</p>
  <blockquote>
  <p>Unexpectedly found nil while implicitly unwrapping an Optional value</p>
  </blockquote>
  <p><strong>My Fix</strong></p>
  <p>To fix the issue I added <code>?</code> to make it <code>timer?.invalidate()</code> as seen in the code below. I tried this after some Googling to get a very loose understanding of what is going on.</p>
  <p>Did this work because the timer did not exist yet and therefore it was &quot;nil&quot;?</p>
  <p>Is this the best way to prevent the crash? It seems that I could also change the variable timer to use <code>?</code> instead and have it work, but only after adding <code>?</code> to the timer.invalidate() line in the step function as well as the others. Clearly I need to brush up on my understanding of Optionals.</p>
  <pre><code>import UIKit
  
  class ViewController: UIViewController {
      
      @IBOutlet weak var label: UILabel!
      
      
      var timeRemaining: Int = 10
      var timer: Timer!
      
  
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view.
          
          
      }
  
      @IBAction func start(_ sender: Any) {
          timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(step), userInfo: nil, repeats: true)
          
      }
      
      @IBAction func stop(_ sender: Any) {
          timer?.invalidate() //added ? to timer
      }
      
      @IBAction func reset(_ sender: Any) {
          timer?.invalidate() //added ? to timer
          timeRemaining = 10
          label.text = &quot;\(timeRemaining)&quot;
      }
      
      @objc func step() {
          if timeRemaining &gt; 0 {
              timeRemaining -= 1
          } else {
              timer.invalidate()
          }
          label.text = &quot;\(timeRemaining)&quot;
      }
      
  
  }
  </code></pre>