首页 > 解决方案 > 删除场景视图节点后应用程序崩溃

问题描述

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

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

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

com.apple.scenekit.scnview-renderer (14):致命错误:在展开可选值时意外发现 nil

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

    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))

}

我做错了什么?

标签: swiftxcodearkitsceneview

解决方案


在这里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))
}

推荐阅读