UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: "Unexpectedly found nil while implicitly unwrapping an optional value"...that I'd already gone out of my way to verify should be valid.

Forums > Swift

So here's the block of code in question:

        if let _ = UIImage(named: "\(storyCharacter.spriteBaseName)-\(status)") {
            fileName = "\(storyCharacter.spriteBaseName)-\(status)"
        } else {
            // Try just the default image if one isn't found for the status
            if let _ = UIImage(named: storyCharacter.spriteBaseName) {
                fileName = storyCharacter.spriteBaseName
            } else {
                // Take the defaults out of the classes
                if (storyCharacter.isGraphic == true) {
                    fileName = defaultGraphicImage
                } else {
                    fileName = defaultCharacterImage
                }
            }
        }

        // Skin the sprite as appropriate
        storyCharacter.currentSprite.texture = SKTexture(imageNamed: fileName) // Since it's been created and we're technically re-skinning it...

As you can see, I use UIImage to check to ensure that the image to be called for the texture exists without any question whatsoever, and if I fall back it checks the fallback before going to a known, set default image.

This code was working just fine on my devices until I started running on the simulator to test different screen sizes. Since doing that, one particular image seems to have started throwing it off and producing the error in the title when it hit the last line. I re-loaded it on my phone and it's now doing the same error there where it wasn't before. Haven't tried putting the rebuilt version on my iPad right now because I'm going to be demoing this for someone this afternoon and prefer to have one copy that's still working right.

Question is, how can it be coming up as nil in this circumstance? Digging around in the debugger, I did see the following in Thread 1, one level deeper than the offending function call:

0x1a497c230 <+464>: bl     0x1a497c598               ; closure #1 (Swift.UnsafeBufferPointer<Swift.UInt8>) -> () in Swift._assertionFailure(_: Swift.StaticString, _: Swift.StaticString, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never

->  0x1a497c234 <+468>: brk    #0x1 // Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

Thanks for any insights!

3      

OK, figured this one out on my own. Leaving the question up and sharing the answer in case someone else encounters a similar issue in the future.

Essentially, I have classes for managing each character in my game, and those classes are supposed to instantiate a sprite with the current representation of the character. The clip above comes from the routine that re-skins them as needed along the way.

I recently finished adding unique prites for all of the characters, so I was going through removing the lines instantiating the SKSpriteNodes with the placeholder sprite and uncommenting the ones that pulled up the default sprite for the given character. As it turns out, in the class file for the one character that kept tripping the error I deleted the line with the placeholder, but forgot to uncomment the line instantiating the sprite as intended.

Thus, I was trying to update the texture on an SKSpriteNode that didn't exist, and that was what was being unwrapped in the error—not the string being fed through the line I wrote that prompted the error.

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.