Why am I getting error unwrapping Optional if it has a value?

Keep getting fatal error: unexpectedly found nil while unwrapping an Optional value

  • I keep getting fatal error: unexpectedly found nil while unwrapping an Optional value, whenever I'm done picking a picture Edited version the value of image is there Update2

  • Answer:

    First of all add your image view to your view controller then you will see your output. Currently you are not able to see your output because you didn't added this image view to your view. So please do override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. imagePicked = UIImageView() imagePicked.frame = CGRectMake(100, 100, 100, 100) self.view.addSubview(imagePicked); } func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) { picker.dismissViewControllerAnimated(true, completion: nil) imagePicked!.image = image } this is happening because you are assign image without allocation of your imagePicked. Hope it will help you.

sinusGob at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

The first error you get is because your imagePicked in not initialised, and you are assigning an Image to it. Solution to this problem is simply initialise it. Use the following code either in viewdidLoad or imagePickerController(_:didFinishPickingMediaWithInfo:) imagePicked = UIImageView(image: image) Remember to dismiss the imagePicker using the following code picker.dismissViewControllerAnimated(true, completion: nil) To make the image visible add the image view in your case imagePicked to the view. To do so add the following code after you dismiss the imagePicker imagePicked.frame = CGRectMake(0, 0, 100, 100) // Use values that you need self.view.addSubview(imagePicked) Your imagePickerController(_:didFinishPickingMediaWithInfo:) should look like this func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { picker.dismissViewControllerAnimated(true, completion: nil) imagePicked = UIImageView(image: info[UIImagePickerControllerOriginalImage] as! UIImage) imagePicked.frame = CGRectMake(0, 0, 100, 100) // whatever frame you want self.view .addSubview(imagePicked) } Note: - imagePickerController:didFinishPickingImage:editingInfo: is deprecated

Vivek Molkar

imagePicked is nil. You declare it at the top ( var imagePicked : UIImageView!). The exclamation says "hey, this won't be nil, I promise." So you silence all compile warnings and errors, but... You never instantiate it, so you break the promise. In did finish picking image with info do: if imagePicked == nil { //init and configure your imageView imagePicked = UIImageView(...) } //you can safely use it here... At the top change the ! To ? Note: The init should be done in a place before there's a posibility of anyone accessing it. Maybe on viewDidLoad...

William Falcon

You have to unwrap the optional value of imagePicked like imagePicked!.image = image Also another issue of init imagePicked = UIImageView(image: image)

Özgür Ersil

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.