title: Application crashes on device - works perfectly on Simulator
body: <p>Recently, I have been working on an application. The goal of the application is to show you your last 10 photos taken in the Today view of the Notification Center, among other things. The application works perfectly in the iOS Simulator, but as soon as I put it on a real device for testing, it fails. It returns the error:</p>
  
  <pre><code>fatal error: Unexpected nil while unwrapping an Optional value
  </code></pre>
  
  <p>Typically this is very easy to fix since XCode highlights the code that returned nil and gives you the opportunity to fix it. In this case, none of my code gets highlighted. Instead, it highlights a line from Thread 1 (Is this the correct term? Thread 1?), as seen below:
  <img src="https://i.stack.imgur.com/5OYeC.png" alt="enter image description here">
  Also note that above the highlited line is the line</p>
  
  <pre><code>; function signature specialization &lt;Arg[0] = Exploded, Arg[1] = Exploded of Swift.(_fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -&gt; ()).(closure #2)
  </code></pre>
  
  <p>I included this line in the picture because of the "fatalErrorMessage" part of it. I suspect this could clue me in on the error, but I have no idea what this means. I'm not yet to the point of understanding that.</p>
  
  <p>Also, after some thought, I placed a breakpoint at the viewDidLoad() function in the hopes of tracking any nil values, but the code appears to never even get to this point. It seems as if none of my code is being run.</p>
  
  <p>Has anyone had problems like this/understands what that error code (If that's what it is) means? I'm pretty desperate now, hence me being here. </p>
  
  <p>Thanks,
  CodeIt</p>
  
  <p><strong>EDIT:</strong>
  I placed a println line inside the viewDidLoad function to double check if it was being run. The println function runs and outputs correctly, so I think I may just have messed up my breakpoint somehow. Anyway - the code runs, but it still doesn't highlight any of my code causing the nil value.</p>
  
  <p><strong>EDIT 2:</strong>
  As requested, I have inserted parts of my code. Please keep in mind that this code is a "first draft", if you will, and I have not yet gotten around to cleaning it up. I'm just trying to get it to work:</p>
  
  <pre><code>@IBOutlet weak var currentPosLabel: UILabel!
  var currentImgPos = 0
  @IBOutlet weak var imageView: UIImageView!
  var images: NSMutableArray!
  var totalImageCountNeeded: Int!
  
  func fetchPhotos() {
      images = NSMutableArray()
      totalImageCountNeeded = 10
      self.fetchPhotoAtIndexFromEnd(0)
  }
  
  
  func fetchPhotoAtIndexFromEnd(index: Int) {
      let imgManager = PHImageManager.defaultManager()
  
      var requestOptions = PHImageRequestOptions()
      requestOptions.synchronous = true
  
      var fetchOptions = PHFetchOptions()
      fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
  
      if let fetchResult = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions) {
          if fetchResult.count &gt; 0 {
              imgManager.requestImageForAsset(fetchResult.objectAtIndex(fetchResult.count - 1 - index) as? PHAsset, targetSize: view.frame.size, contentMode: PHImageContentMode.AspectFill, options: requestOptions, resultHandler: { (image, _) in
                  self.images.addObject(image)
                  if index + 1 &lt; fetchResult.count &amp;&amp; self.images.count &lt; self.totalImageCountNeeded {
                      self.fetchPhotoAtIndexFromEnd(index + 1)
                  } else {
                      println("Completed array: \(self.images)")
                  }
  
  
  
              })
          }
      }
  
  }
  
  
  
  
  
  
  
  override func viewDidLoad() {
      super.viewDidLoad()
  }
  
  override func viewDidAppear(animated: Bool) {
      // NOTE: I am calling the fetchPhotos() function here since earlier in my debugging I thought the problem may be that I was calling it too early in the viewDidLoad function. It an incorrect theory, but it was worth a try.
      fetchPhotos()
      if images.count &gt; 0 {
          imageView.image = images[1] as? UIImage
      }
  }
  </code></pre>
  
  <p>I removed some parts of the code that I know have no reasons to cause my error, such as @IBActions, didReceiveMemoryWarning(), etc..</p>