3

I'm getting the following Error:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

Please do not mark this as a duplicate, I know what Optionals and unwrapping them are.

  1. This error is appearing on line 9.
  2. The error happens when the app is running and I perform a segue.

Code:

 @IBOutlet weak var rewardVideoImage: UIImageView!

 let arrayOfPhotos = ["photo1", "photo2", "photo3"]
 var randomImageIndex : Int = 0

 func selectImage() {
    randomImageIndex = Int(arc4random_uniform(3))

    rewardVideoImage.image = UIImage(named: arrayOfPhotos[randomImageIndex])
    print(randomImageIndex)
 }

 override func viewDidLoad() {
    super.viewDidLoad()
    selectImage()
 }
  • Unwrap `UIImage(named: arrayOfPhotos[randomImageIndex])` with an `if let` to see if the image exists. –  May 10 '18 at 10:55
  • 3
    I think `rewardVideoImage` is nil, are you sure it's connected properly? – James P May 10 '18 at 11:05
  • @user770 image property is optional so there is no need to unwrap the result. https://developer.apple.com/documentation/uikit/uiimageview/1621069-image – Leo Dabus May 10 '18 at 11:09
  • I don't know what doing `if let` is called. Maybe not "unwrapping", but clearly the image name used is incorrect, or something like that. And I'm trying to have the OP check that. –  May 10 '18 at 11:11
  • I am saying that setting the property to nil won't throw an error – Leo Dabus May 10 '18 at 11:13
  • Your code looks fine. Try removing re-importing the photos, and checking they are named correctly and case-sensitive. – rbaldwin May 10 '18 at 11:18
  • Only reason is `rewardVideoImage` is `nil` here, either you missed connecting from `UIStoryboard` or it is taking wrong reference. Remove the outlet of `rewardVideoImage` and connect it again on `UIStoryboard` will solve your problem. – Ankit Jayaswal May 10 '18 at 12:54

2 Answers2

7

arc4Random isn't the problem here, it's your IBOutlet as a nil image wouldn't cause a crash.

rewardVideoImage is nil, you need to connect it in your storyboard.

James P
  • 4,786
  • 2
  • 35
  • 52
  • AFAIK, attempting to access a property that is not connected to storyboard doesn't even build. –  May 10 '18 at 11:35
  • @user770 It does if it's an implicitly unwrapped optional, and it will crash with this exact error. – James P May 10 '18 at 11:39
  • Ah, thanks for the info. So my suggestion to use `if let` wasn't so off after all :) –  May 10 '18 at 11:42
  • @JamesP It is already connected to the storyboard –  May 11 '18 at 06:36
  • @Callum9966 Maybe you should show how you create and push the viewController. From the code you've shown `rewardVideoImage` being nil is the only explanation. – James P May 11 '18 at 10:46
1

Two things here can cause optional issue as far as I think:

  1. Make sure you have images named "photo1", "photo2", "photo3" in your assets catalogue.

  2. Make sure your storyboard has proper reference to your UIImageView

Akshansh Thakur
  • 5,163
  • 2
  • 21
  • 38