swift - 删除场景视图节点后应用程序崩溃
问题描述
我在我的 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))
}
我做错了什么?
解决方案
在这里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))
}
推荐阅读
- python - 使用 js_on_change 在 Bokeh 中动态更新文本
- prediction - 多项式NB的维度不匹配
- javascript - asp.net 验证 - 如果一行中的一个控件具有值,则该行中剩余的 2 个控件应该具有值
- python - Dask:自动重新分块和 block_size_limit
- php - PHP:合并两个数组时是否有一个空安全运算符来防止“未定义的数组键”警告?
- expo - 更新 React Native Expo 应用程序,OTA 与构建
- c++ - Premake5:GLFW 项目无法在项目类型“ConsoleApp”下编译
- python - 如何在 Windows 上从 .tar 文件安装 python
- angular - 如果规范在循环内,量角器测试总是通过
- python - 如何使用 Scrapy 的 crawlspider 从一个页面和每个单击子页面中抓取所有网站文本