Page 394 problem! fatal error in pest control! PLEASE HELP

okay so I got through the whole of pest control and finally came to the end 394, and added all of the init(coder:) to the player, bug, game scene, then I go to run and it fatal errors saying unexpectedly found nil whilst unwrapping an optional on this line

“animations = aDecoder.decodeObject(forKey: “Player.animations”) as! [SKAction]”

I even tried to just run the final source file for this chapter and I still get the same error, so it can’t be me? it must be something wrong with the book? I have had this EXACT same error with catNap twice, where I have even used the source final files which is provided by raywenderlich and I get the same errors? what is going on PLEASE HELP

required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) animations = aDecoder.decodeObject(forKey: "Player.animations") as! [SKAction] hasBugspray = aDecoder.decodeBool(forKey: "Player.hasBugspray") if hasBugspray { removeAction(forKey: "blink") blink(color: .green, on: hasBugspray) } }

@joeeydubu - I’m sorry to hear that you’re having trouble running the code.

I just ran the project in the final folder in Chapter 14, and it runs without error. Are you having trouble running that project?

I can get the error that you’re describing if I don’t encode animations. In that case, the decoder is looking for something that doesn’t exist in the saved archive file.

If you open the final project, in Player.swift on line 118, there’s an encode(with:) method which encodes animations. Do you have that in the project where you’re having an error?

It might also be that you have a bad archive file saved in the Documents folder. Sometimes during development, you can get an archive file from a previous development phase. Try removing the project from the simulator.

(Thinking about it, it’s more likely that you have a bad file. You can also go to the simulator folder as described in the book and delete that archive file.)

1 Like

oh my thank you so much!! that was such an easy fix!
but now I am wondering, how would I have known to do that for the future of when I start making my own games? like it says no optional when unwrapping for animations, but if it was something to do with the archive file, how was I suppose to know that?

thankyou so much for your help!! (::

@joeeydubu - that’s a very good question. I think when I was writing the chapter, I scratched my head a bit when I first got that error.

However, when you see a ! in code, think DANGER!

Because we (authors) have limited space and it’s also important to have brevity to get the concept across, we often use the ! (implicitly unwrapped optional) as a shortcut to always checking that something is there.

If I were doing full error checking, I would write something like this:

if let animations = aDecoder.decodeObject(forKey: "Player.animations") as? [SKAction] {
 self.animations = animations
} else {
  fatalError("animations were not found in the archive file")
}

The error is an obvious one - you’re looking for animations in a coded file, and if you don’t find one and it should be there, and it’s an error that should crash the app, put out a crash error that it’s not there.

Your app would still have crashed, but it would have crashed with the message "animations were not found in the archive file", and may have prompted you to think about why they weren’t in the archive file.

I would recommend not shipping with a ! anywhere in your code. Except for @IBOutlets and except for times when you are really really really sure that the property will have something other than nil in it every time you access it.

I also use print a lot. When I get crashes, I print out contents of properties every time I set or access them and make sure that what’s being printed makes sense.

I had the same problem and you saved me from a headache

thanks Caroline

1 Like