Swift SceneKit template not idiomatic
| Originator: | blocksom | ||
| Number: | rdar://17896956 | Date Originated: | 03-Aug-2014 11:14 AM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode6 B4 |
| Classification: | Other Bug | Reproducible: | Always |
Summary:
Non-idiomatic Swift in the SceneKit template makes it easy to introduce a run time crash when modifying the template.
Steps to Reproduce:
1. Create a new project using the Game template specifying Swift and SceneKit
2. Change this code so that it searches for a different node instead of “ship” and then animates it:
// retrieve the ship node
let ship = scene.rootNode.childNodeWithName("ship", recursively: true)
// animate the 3d object
ship.runAction(SCNAction.repeatActionForever(SCNAction.rotateByX(0, y: 2, z: 0, duration: 1)))
The only thing you need to change is the string “ship”, the animate line is included for context because that’s where the crash occurs. Note that a good reason to do search for a different node is you are using a different .dae file or constructing the scene yourself, but you do not need to do this to see the bug.
3. Build and run
Expected Results:
It finds nothing and doesn’t animate the ship
Actual Results:
Runtime crash with message “fatal error: unexpectedly found nil while unwrapping an Optional value”
Regression:
None
Notes:
To fix, change the code to this:
// retrieve the ship node
if let ship = scene.rootNode.childNodeWithName(“ship”, recursively: true) {
// animate the 3d object
ship.runAction(SCNAction.repeatActionForever(SCNAction.rotateByX(0, y: 2, z: 0, duration: 1)))
}
This will not crash even if the node is not found, and while my knowledge of Swift is limited I think this is the best way to do it. It’s possible that a root cause is that SceneKit should return an optional (?) instead of explicitly unwrapped (!) node, but I’m still figuring that stuff out.
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!