How to deal with "Unexpectedly found nil while unwrapping an Optional value"?

Hi all,

This is my code:



import UIKit


var imagePickerController:UIImagePickerController!

var secondViewController = SecondViewController()


@IBOutlet weak var camera1: UIButton!


@IBAction func takePhoto(_ sender: UIButton) {

// self.camera1.transform = CGAffineTransform(rotationAngle: CGFloat.pi)

if(UIImagePickerController.isSourceTypeAvailable(.camera))

{

self.imagePickerController = UIImagePickerController()

self.imagePickerController.delegate = self

self.imagePickerController.allowsEditing = true

self.imagePickerController.sourceType = UIImagePickerController.SourceType.camera

self.present(self.imagePickerController,animated: true,completion: nil)

}

}

override func viewDidLoad() {

super.viewDidLoad()

}


@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

self.present(secondViewController,animated: true,completion: nil)

secondViewController.infoFromViewOne = info[UIImagePickerController.InfoKey.editedImage] as? UIImage

self.dismiss(animated: true, completion: nil)

}


@objc func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {

self.dismiss(animated: true, completion: nil)

}

}



class SecondViewController: UIViewController {

var infoFromViewOne:UIImage?

@IBOutlet var image1: UIImageView!

override func viewDidLoad() {

super.viewDidLoad()

image1.image = infoFromViewOne

// Do any additional setup after loading the view.

}

}


I am trying to transmit a UIImage from another view when a button is clicked andI get an error saying: "fatal Error: Unexpectedly found nil while unwrapping an Optional value.


I really need help on this one as I have searched online for this topic and tried many things but nothing has worked. I would love to know if there is anything that can be done to solve this error or whether there is another war of changing a label from a different view.


All suggestions would be great!

Thanks

Answered by OOPer in 354235022

Thanks for confirmation. Considering the confirmed behavior this part may be causing the issue:

        self.present(secondViewController,animated: true,completion: nil)
        self.dismiss(animated: true, completion: nil)

You present the secondViewController without dismissing the UIImagePickerController, and dismiss it immediately after that.

Which causes Nothing happens.


Please try replacing the two lines with the following:

        self.dismiss(animated: true, completion: {
            self.present(secondViewController,animated: true,completion: nil)
        })


With this change, the code first dismisses the UIImagePickerController, and after dismissing completed, the secondViewController will be presented.

Accepted Answer

Thanks for confirmation. Considering the confirmed behavior this part may be causing the issue:

        self.present(secondViewController,animated: true,completion: nil)
        self.dismiss(animated: true, completion: nil)

You present the secondViewController without dismissing the UIImagePickerController, and dismiss it immediately after that.

Which causes Nothing happens.


Please try replacing the two lines with the following:

        self.dismiss(animated: true, completion: {
            self.present(secondViewController,animated: true,completion: nil)
        })


With this change, the code first dismisses the UIImagePickerController, and after dismissing completed, the secondViewController will be presented.

I received 3 messages: "shall instantiate" "did instantiate" "presented"

and after I inserting "print("UIImage", secondViewController.infoFromViewOne)" there is a warning :Expression implicitly coerced from 'UIImage?' to 'Any'

Through your method I successfully achieve what I want to see.


Thank you very much,you helped me a lot.


Best wishes.

Happy to hear that. May your app be great.

How to deal with "Unexpectedly found nil while unwrapping an Optional value"?
 
 
Q