Search code examples
iosswiftxcodeiboutlet

Referencing IBOutlet causes crash


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

Solution

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