删除场景视图节点后应用程序崩溃

App crashes after i delete sceneview nodes

我在我的 SceneView 中添加了一个按钮来删除放置在墙上的图像:

@IBAction func reset(_ sender: Any) {
sceneView.scene.rootNode.enumerateChildNodes { (node, _) in node.removeFromParentNode()
    }
}

它工作正常,但几秒钟后应用程序崩溃并出现以下警告:

com.apple.scenekit.scnview-renderer (14): Fatal error: Unexpectedly found nil while unwrapping an Optional value

在我的这部分代码中出现错误:

    func update(anchor: ARPlaneAnchor) {

    planeGeometry.width = CGFloat(anchor.extent.x);

    planeGeometry.height = CGFloat(anchor.extent.z);

    position = SCNVector3Make(anchor.center.x, 0, anchor.center.z);



    let planeNode = self.childNodes.first!

    planeNode.physicsBody = SCNPhysicsBody(type: .static, shape: SCNPhysicsShape(geometry: self.planeGeometry, options: nil))

}

我做错了什么?

这里 let planeNode = self.childNodes.first! 你强制展开值。从 childNodes 数组中删除所有节点后,它将变为空。使用 if let

if let planeNode = self.childNodes.first{
 planeNode.physicsBody = SCNPhysicsBody(type: .static, shape: SCNPhysicsShape(geometry: self.planeGeometry, options: nil))
}