Search code examples
iosswiftcrashsegueforced-unwrapping

Error "fatal error: unexpectedly found nil while unwrapping an Optional value" when a segue is fired


I added a segue to my app and when I fire the segue the app crashes in the iPhone 5S simulator (In other simulators it crash before then because of code specific to the device. Here is a link to the question), the message "fatal error: unexpectedly found nil while unwrapping an Optional value" gets printed to the console and the error Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0) is displayed on the line initializing the array. I think the reason why is because I have an animation of UIImageViews that requires un-wrapping. Here is how I do it:

// dieImages are UIImageViews that I am animating then displaying a random image of a die

    let dieImages = [dieImage0, dieImage1, dieImage2, dieImage3, dieImage4, dieImage5, dieImage6, dieImage7]

// Die animation
                for die in dieImages {
                    die.animationImages = [  <====== Error on this line
                        UIImage(named: "dicey-die2")!,
                        UIImage(named: "dicey-die6")!,
                        UIImage(named: "dicey-die1")!,
                        UIImage(named: "dicey-die4")!,
                        UIImage(named: "dicey-die3")!,
                        UIImage(named: "dicey-die5")!,
                        UIImage(named: "dicey-die3")!,
                        UIImage(named: "dicey-die1")!,
                        UIImage(named: "dicey-die6")!,
                        UIImage(named: "dicey-die3")!,
                        UIImage(named: "dicey-die5")!,
                        UIImage(named: "dicey-die2")!,
                        UIImage(named: "dicey-die4")!
                    ]

                die.animationRepeatCount = 1
                die.animationDuration = 1.0
            }

But segue is of type AnyObject so I think that is the reason un-wrapping doesn't work:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {}

How do I un-wrap the images safely, or not un-wrap at all when the segue is fired? I have seen the methods of using two question marks (but that causes the error of "could not find an overload for 'init' that accepts the supplied arguments") or an if statement, but I don't know how to implement that.

update

I tried this code and the same thing happens as before

        let image0 = UIImage(named: "dicey-die1")!
        let image1 = UIImage(named: "dicey-die2")!
        let image2 = UIImage(named: "dicey-die3")!
        let image3 = UIImage(named: "dicey-die4")!
        let image4 = UIImage(named: "dicey-die5")!
        let image5 = UIImage(named: "dicey-die6")!

        let dieImages = [dieImage0, dieImage1, dieImage2, dieImage3, dieImage4, dieImage5, dieImage6, dieImage7]

        for die in dieImages {
            die.animationImages = [
                image1,
                image5,
                image0,
                image3,
                image2,
                image4,
                image2,
                image0,
                image5,
                image2,
                image4,
                image1,
                image2
            ]

            die.animationRepeatCount = 1
            die.animationDuration = 1.0
        }

update 2

I created an array with all the images

let image0:[UIImage] = [ UIImage(named: "dicey-die2")!,
            UIImage(named: "dicey-die6")!,
            UIImage(named: "dicey-die1")!,
            UIImage(named: "dicey-die4")!,
            UIImage(named: "dicey-die3")!,
            UIImage(named: "dicey-die5")!,
            UIImage(named: "dicey-die3")!,
            UIImage(named: "dicey-die1")!,
            UIImage(named: "dicey-die6")!,
            UIImage(named: "dicey-die3")!,
            UIImage(named: "dicey-die5")!,
            UIImage(named: "dicey-die2")!,
            UIImage(named: "dicey-die3")!]

Then I used the array in my animation

for die in dieImages {
    die.animationImages = [
      image0
    ]

The app crash at the AppDelegate so I added an exception breakpoint and the crash happened on the animation array:

die.animationImages = [

And this message was printed to the console:

2015-09-08 07:23:05.192 Diced[80843:14927078] -[Swift._SwiftDeferredNSArray _isResizable]: unrecognized selector sent to instance 0x7fa738dd7460

Solution

  • Best bet is that the images are not available.

    A simple test, does this work and instantiate the image:

    let image = UIImage(named: "dicey-die2")!
    

    If not how are you adding the images to the project?