-5

I'm trying to have a sceneView load a scene but I get the error Value of optional type 'SCNScene?' must be unwrapped to a value of type 'SCNScene' With options of Coalesce using '??' to provide a default when the optional value contains 'nil' or Force-unwrap using '!' to abort execution if the optional value contains 'nil'

override func viewDidLoad() {
        super.viewDidLoad()
        sceneView.scene = SCNScene(named: "Models.scnassets/CDMTEST1");

        VirtualObject.readCoreData()
    }

Error message : Value of optional type 'SCNScene?' must be unwrapped to a value of type 'SCNScene' Coalesce using '??' to provide a default when the optional value contains 'nil' Force-unwrap using '!' to abort execution if the optional value contains 'nil'

Turnt Ducky
  • 23
  • 1
  • 7

1 Answers1

2

Try this:

if let scene = SCNScene(named: "Models.scnassets/CDMTEST1"){
    sceneView.scene = scene
}

you need to pass a value not a optional since SCNScene(named: "Models.scnassets/CDMTEST1") is optional since may return nil since it fails.

Yoel Jimenez del valle
  • 1,270
  • 1
  • 9
  • 20
  • This context is better suited to force unwrapping, or conditional wrapping and then `fatalError`ing with a custom message. Not finding an asset file is a critical and unrecoverable error, the application shouldn't continue without it. It's best to make it obvious with a crash, then have silent failure – Alexander Aug 02 '19 at 14:17