title: Fetching last photo taken with Swift
body: <p>I will start by saying I am new to swift, as well as stack overflow, so I apologize if I mess anything up. </p>
  
  <p>I am trying to fetch the last photo taken with Swift. I found code that seems like it should work. 
  <a href="http://blog.swilliams.me/words/2015/08/09/finding-the-last-photo-taken/" rel="nofollow">http://blog.swilliams.me/words/2015/08/09/finding-the-last-photo-taken/</a></p>
  
  <pre><code>import UIKit
  import Photos
  
  typealias ImageCallback = (UIImage? -&gt; Void)
  
  func fetchLastPhoto(resizeTo size: CGSize?, imageCallback: ImageCallback) {
  let fetchOptions = PHFetchOptions()
  fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
  fetchOptions.fetchLimit = 1
  
  if let fetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions) {
      if let asset = fetchResult.firstObject as? PHAsset {
          let manager = PHImageManager.defaultManager()
          let targetSize = size == nil ? CGSize(width: asset.pixelWidth, height: asset.pixelHeight) : size!
          manager.requestImageForAsset(asset,
              targetSize: targetSize,
              contentMode: .AspectFit,
              options: nil,
              resultHandler: { image, info in
              imageCallback(image)
          })
      } else {
          imageCallback(nil)
      }
    }
  }
  </code></pre>
  
  <p>I keep getting an error on the line that starts "<code>if let fetchResult = PHAsset.fet....</code>"</p>
  
  <p>The error says, "</p>
  
  <blockquote>
    <p>Initializer for conditional binding must have Optional type, not
    'PHFetchResult'</p>
  </blockquote>
  
  <p>When I option click on "<code>fetchResult</code>", it doesn't tell me what type of value it is and gives and error, so I know it is not getting set correctly. </p>
  
  <p>I feel like the solution is probably something really simple that I have not tried yet. </p>
  
  <p>Does anybody have any idea what is causing this error?</p>