Ben M
Ben M

Reputation: 21

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

I am new to code and I am not sure how to get rid of my optional values here. I read somewhere that this could be my problem. any help would be great!

I have been following this tutorial:https://www.youtube.com/watch?v=Qyy8pJd4IWU

@IBAction func dropPhoto(sender: AnyObject) {

    presentViewController(imagePicker, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [NSObject : AnyObject]?) {

    self.dismissViewControllerAnimated(true, completion: nil)

    let thumbnail = image.resizedImageWithContentMode(UIViewContentMode.ScaleAspectFit, bounds: CGSizeMake(400, 400), interpolationQuality: CGInterpolationQuality.High)
    let imgData = UIImagePNGRepresentation(thumbnail)
    let base64EncodedImage = imgData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions())

    let uniqueReference = firebase?.childByAutoId()
    uniqueReference!.setValue(base64EncodedImage)

    let key = uniqueReference?.key
    _ = mapView.userLocation.location
    geofire!.setLocation(mapView.userLocation.location,forKey: key)

}

Upvotes: 2

Views: 406

Answers (2)

Price Ringo
Price Ringo

Reputation: 3440

You could also add an assert or precondition checking firebase and geofire at the beginning of the function. Here is a third approach that will check those values and stop execution on your debug builds but otherwise simply return on release builds. That will make the later method calls into firebase and geofire safe.

You will still need to identify why one of your references is unexpectedly nil and handle that case. Maybe never call this image picker function in the first place or simply remove the assertFailure statements and let the function return silently without doing anything. Your choice.

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [NSObject : AnyObject]?) {
    guard let firebase = firebase else {
       assertionFailure("Missing Firebase reference")
       return
    }
    guard let geofire = geofire else {
       assertionFailure("Missing Geofire reference")
       return
    }
    self.dismissViewControllerAnimated(true, completion: nil)

    let thumbnail = image.resizedImageWithContentMode(UIViewContentMode.ScaleAspectFit, bounds: CGSizeMake(400, 400), interpolationQuality: CGInterpolationQuality.High)
    let imgData = UIImagePNGRepresentation(thumbnail)
    let base64EncodedImage = imgData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions())

    uniqueReference = firebase.childByAutoId()
    uniqueReference.setValue(base64EncodedImage)
    geofire.setLocation(mapView.userLocation.location,forKey: uniqueReference.key)
}

Upvotes: 1

GetSwifty
GetSwifty

Reputation: 7746

Whenever you see this error, look for "!"s

There are two lines here that contain a force-unwrap

geofire!.setLocation(mapView.userLocation.location,forKey: key)

and

 uniqueReference!.setValue(base64EncodedImage)

you should be able to fix it by simply replacing the ! with a ? , e.g.

geofire?.setLocation(mapView.userLocation.location,forKey: key)

which will cause setLocation to be called only if geoFire is a real value, otherwise if you want to also handle the nil case, the swift way is:

if let geoFire = geoFire {
      geoFire.setLocation(mapView.userLocation.location, forKey: key)
}
else{
     *do something*
}

Upvotes: 2

Related Questions