title: Thread 1: EXC_BAD_INSTRUCTION Swift 2
body: <p>I'm trying to make an app that will tell the time remaining until a specific date. Like some alarm apps say how much time until it will wake you up. I was having issues with ways of calculating this so I created my own function for it. My <strong><em>original version</em></strong> on a <strong><em>playground</em></strong> seemed to be working fine:</p>
  
  <pre><code>//: Playground - noun: a place where people can play
  
  import UIKit
  
  var str = "Hello, playground"
  
  
  let Test = NSCalendar.currentCalendar().component(NSCalendarUnit.Day, fromDate: NSDate())
  
  func Calculation() {
  
      //Months
      var month: String {
          let dateFormatter = NSDateFormatter()
          dateFormatter.dateFormat = "MM"
          return dateFormatter.stringFromDate(NSDate())
      }
  
      print(month)
  
      //Days
      var day: String {
          let dateFormatter = NSDateFormatter()
          dateFormatter.dateFormat = "dd"
          return dateFormatter.stringFromDate(NSDate())
      }
  
      print(day)
  
  
      //Hours
      var hour: String {
          let dateFormatter = NSDateFormatter()
          dateFormatter.dateFormat = "hh"
          return dateFormatter.stringFromDate(NSDate())
      }
  
      print(hour)
  
      //Minutes
      var minute: String {
          let dateFormatter = NSDateFormatter()
          dateFormatter.dateFormat = "mm"
          return dateFormatter.stringFromDate(NSDate())
      }
  
      print(minute)
  
      //Current Time
      let CurrentMonths = Int(month)
      let CurrentDays = Int(day)
      let CurrentHours = Int(hour)
      let CurrentMinutes = Int(minute)
  
      //End Date
      let EndMonths = 11;
      let EndDays = 23;
      let EndHours = 12;
      let EndMinutes = 60;
  
      //Calculation
  
      var LeftMonths = EndMonths - CurrentMonths!
      var LeftDays = EndDays - CurrentDays! - 1
      var LeftHours = EndHours - CurrentHours! - 1
      var LeftMinutes = EndMinutes - CurrentMinutes! - 1
  
      //Update Labels
  
  
  
      //Re-Run Loop To Update Time
  }
  Calculation()
  </code></pre>
  
  <p>I had to change it a little so it would work as a standalone iOS app in Swift 2. Currently I have the error <code>Thread 1: EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP, subcode=0x0)</code> In the console it says </p>
  
  <pre><code>fatal error: unexpectedly found nil while unwrapping an Optional value
  (lldb) `. I checked the debug menu/console to the left of it and none of my variables are set to `nil.
  </code></pre>
  
  <p>Here is my ViewController.swift code <strong><em>UPDATED CODE To Fix Local Var Problem</em></strong>:</p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController {
  
      //Current Time
      var CurrentMonths:Int = 0
      var CurrentDays:Int = 0
      var CurrentHours:Int = 0
      var CurrentMinutes:Int = 0
  
      //End Date
      var EndMonths:Int = 0
      var EndDays:Int = 0
      var EndHours:Int = 0
      var EndMinutes:Int = 0
  
      //Calculation
  
      var LeftMonths:Int = 0
      var LeftDays:Int = 0
      var LeftHours:Int = 0
      var LeftMinutes:Int = 0
  
      //Define
      var month:String = ""
      var day:String = ""
      var hour:String = ""
      var minute:String = ""
  
      //Labels
      @IBOutlet weak var DayL: UILabel!
      @IBOutlet weak var HourL: UILabel!
      @IBOutlet weak var MinuteL: UILabel!
  
  
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
          Calculation()
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
      //Months
      func Month() {
          var month: String {
              let dateFormatter = NSDateFormatter()
              dateFormatter.dateFormat = "MM"
              return dateFormatter.stringFromDate(NSDate())
          }
          print(month)
          Day()
      }
  
      //Days
      func Day() {
          var day: String {
              let dateFormatter = NSDateFormatter()
              dateFormatter.dateFormat = "dd"
              return dateFormatter.stringFromDate(NSDate())
          }
          print(day)
          Hour()
      }
  
      //Hours
      func Hour() {
          var hour: String {
              let dateFormatter = NSDateFormatter()
              dateFormatter.dateFormat = "hh"
              return dateFormatter.stringFromDate(NSDate())
          }
          print(hour)
          Minute()
      }
      //Minutes
      func Minute() {
          var minute: String {
              let dateFormatter = NSDateFormatter()
              dateFormatter.dateFormat = "mm"
              return dateFormatter.stringFromDate(NSDate())
          }
          print(minute)
      }
  
      func Calculation() {
  
          //Start Calculations
          Month()
  
          //Current Time
          CurrentMonths = Int(month)!
          CurrentDays = Int(day)!
          CurrentHours = Int(hour)!
          CurrentMinutes = Int(minute)!
  
          //End Date
          EndMonths = 11;
          EndDays = 23;
          EndHours = 12;
          EndMinutes = 60;
  
          //Calculation
  
          LeftMonths = EndMonths - CurrentMonths
          LeftDays = EndDays - CurrentDays - 1
          LeftHours = EndHours - CurrentHours - 1
          LeftMinutes = EndMinutes - CurrentMinutes - 1
  
          //Update Labels
          DayL.text = String(LeftDays)
          HourL.text = String(LeftHours)
          MinuteL.text = String(LeftMinutes)
  
  
  
          //Re-Run Loop To Update Time
          Calculation()
      }
  
  }
  </code></pre>
  
  <p>The error occurs on <code>CurrentMonths = Int(month)!</code>
  All of the solutions from similar problems have not seem to work.
  Also, when removing the var tag in the functions in the Calculation function, it gives me an error so I can't remove it.</p>