-1

I have an UIImageView outlet set up as below:

@IBOutlet weak var imageView: UIImageView!

And then have some logic that changes the image if a certain value is matched:

if(someValue == "1"){
   imageView.image = UIImage(named:"image1")
}else if(someValue == "2"){
   imageView.image = UIImage(named:"image2")
}

When this is executed, I got the following error:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
Praveen Matanam
  • 2,773
  • 1
  • 20
  • 24
peterbonar
  • 559
  • 3
  • 6
  • 24

2 Answers2

5

Ensure that the outlet is correctly set up. The outlet will have a filled circle on its left if it is connected to a view in a storyboard, otherwise you'll see an empty circle. If you have an empty circle, you can either click and drag to the view you want to connect the outlet to or delete it and recreate it.

enter image description here

Francesco Puglisi
  • 2,140
  • 2
  • 18
  • 26
0

The outlet was referenced in the .swift file, however, wasn't properly connected. Removing this reference and reconnecting resolved this.

peterbonar
  • 559
  • 3
  • 6
  • 24