-1

Getting below error message

"fatal error: unexpectedly found nil while unwrapping an Optional value"

on this part all image is no problem.

(segue.destination as! DetailViewController).imgView.image = UIImage(named: "horsestar.png")!

Below code is whole prepare function

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    if segue.identifier == "dog" {
        let imgView = UIImageView(image: UIImage(named: "dogstar.png")!)
        print("dog")

        (segue.destination as! DetailViewController).scrollView.addSubview(imgView)
    } else if segue.identifier == "horse" {
        print("horse")
        (segue.destination as! DetailViewController).imgView.image = UIImage(named: "horsestar.png")!
        (segue.destination as! DetailViewController).scrollView.addSubview((segue.destination as! DetailViewController).imgView)
    } else if segue.identifier == "bear" {
        let imageView = UIImageView(image: UIImage(named: "bear.png")!)
        print("bear")
    } else if segue.identifier == "eagle" {
        let imageView = UIImageView(image: UIImage(named: "eagle.png")!)
        print("eagle")
    } else {
        let imageView = UIImageView(image: UIImage(named: "rabbit.png")!)
        print("rabbit")
    }
}
Noah.Kim
  • 119
  • 8

2 Answers2

1

Be careful with force-unwrapping (!), because it propagates a lot of errors in runtime. If you don't expect nil value in your code, user guard let.

In your code:

(segue.destination as! DetailViewController).imgView.image = UIImage(named: "horsestar.png")!

You have two dangerous places:

  1. Cast segue.destination to DetailViewController. If it's impossible, throw "fatal error: unexpectedly found nil while unwrapping an Optional value"
  2. Load image with name "horsestar.png". If image isn't founded, throw "fatal error: unexpectedly found nil while unwrapping an Optional value"

Better use guard let (or if let):

guard let destinationViewController = segue.destination as? DetailViewController else {
    print("unable to get destinationViewController")
    return
}

guard let image = UIImage(named: "horsestar.png") else {
    print("unable to load image")
    return
}

destinationViewController.imgView.image = image
Axazeano
  • 890
  • 9
  • 23
  • i tried your code. but, it is okay in guard phrase. but, "destinationViewController.imgView.image = image" is error. i don't understand.. – Noah.Kim Oct 04 '17 at 17:43
  • Which error do you have? Is your `imgView` not equal nil when you assign image? – Axazeano Oct 04 '17 at 18:07
  • fatal error: unexpectedly found nil while unwrapping an Optional value that error. yes imgView is not equal nil.. – Noah.Kim Oct 04 '17 at 18:48
  • Before calling `viewDidLoad` in `destinationViewController` outlets equal `nil` and the view controller doesn't fully load. Read about ViewControllers lifecycle, because it's very important for developing on iOS platform. – Axazeano Oct 04 '17 at 20:04
0

Change your code at else if condition

else if segue.identifier == "horse" {
    if let toViewController = segue.destination as? DetailViewController {
      toViewController.imgView.image = UIImage(named: "horsestar.png")

     /// your other stuff 
    }
}

Still don't know below line from you code what you want?

  (segue.destination as! DetailViewController).scrollView.addSubview((segue.destination as! DetailViewController).imgView)
iPatel
  • 46,010
  • 16
  • 115
  • 137