1

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
Community
  • 1
  • 1
Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
  • 1
    What line has the error? What to `dieImages` have to do with the `UIStoryboardSegue`? You need to improve the question. – zaph Sep 07 '15 at 17:09
  • 1
    The first rule to forced unwrapping is no never, every, under any circumstances force unwrap any value that could ever be nil. That is what `if let ... { good code } {error code}` is for. – zaph Sep 07 '15 at 17:15
  • I added some comments to my code. – Caleb Kleveter Sep 07 '15 at 17:23
  • See the first comment. – zaph Sep 07 '15 at 17:26
  • Better. Do all the images exist? Can you create one, say: `let imageTest = UIImage(named: "dicey-die2")!`? Break the problem down until there is just one statement/line that causes th error, the smallest code that causes the error. – zaph Sep 07 '15 at 18:44
  • It generally helps not to stuff to much code into a single statement, instead use intermediate statements to make debugging and readability easier. Ii this case first create the array of images in a separate statement and use that array in the `for in`. There is no cost to these intermediate statements in the release code. – zaph Sep 07 '15 at 18:45

1 Answers1

1

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?

zaph
  • 111,848
  • 21
  • 189
  • 228
  • I added the images in the images.xcassets folder. The error only happens when the segue is fired. I added an update to my post. – Caleb Kleveter Sep 07 '15 at 19:33
  • Does `let image0 = UIImage(named: "dicey-die1")!` succeed? Anywhere, say in `didFinishLaunchingWithOptions`? – zaph Sep 07 '15 at 19:53