Learn iOS with Examples, Code Snippets and Video Tutorials

Sprite Kit crashing with NSGenericException “mutated while being enumerated”

Sprite Kit crashing with NSGenericException “mutated while being enumerated”
July 11, 2014 Justin

For those of you Sprite Kit users jumping on the newest version of Xcode, you might run into this…

Terminating app due to uncaught exception ‘NSGenericException’, reason*** Collection <__NSArrayM: 0x7edf17d0> was mutated while being enumerated.’

Its a bit of a misleading message because although you see NSArray in there, what’s most likely the cause is code like this (which has worked fine in iOS 7)…

 

Previously, I had never noticed an issue removing a node from a scene while enumerating through children, but I guess the latest versions of Xcode will complain about this. You can get into similar trouble if you add an object to the scene during the enumeration block. For example, in the past you might have checked through all enemy objects to see if any should fire, and if so, added a laser beam to the scene during that block. Well as tempting as that is, you’ll just have to delay adding or removing nodes.

Alternatives include, running an SKAction which calls a method. Something like this…

Or in the case of removing a node, try this…

Yup, not as simple, but it won’t crash.

In other news, I added ladders to my Fantastic Worlds iOS Starter Kit and the demo app has finally made it in the App Store. Check out Meta Platformer (its free) and made entirely with properties in a plist and Tiled. Zero coding is needed. Now check out these ladders…

 

Justin Dike is the owner and lead developer at CartoonSmart.com. He's taught hundreds of hours of game development video tutorials, covering everything from coding to art. These days he's working mostly on Swift and Sprite Kit tutorials, which often lead into highly polished iOS / tvOS Starter Kits, which require no programming at all, but are capable of making nearly any type game! Yeah, you read that write. You can also find Justin at the official CartoonSmart Podcast

0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*

Check out our premium Swift and Sprite Kit Tutorial content!

Visit CartoonSmart.com