Xcode创建ARKit项目遇致命错误:解包可选值时意外发现nil
iOS AR项目默认配置运行时触发解包nil致命错误
使用Xcode默认设置创建iOS AR项目,采用默认配置运行时触发致命错误,错误提示为Unexpectedly found nil while unwrapping an Optional value,指向ViewController.swift第26行。已确认art.scnassets/ship.scn文件存在且路径名称正确。
ViewController代码:
// // ViewController.swift // test // // import UIKit import SceneKit import ARKit class ViewController: UIViewController, ARSCNViewDelegate { @IBOutlet var sceneView: ARSCNView! override func viewDidLoad() { super.viewDidLoad() // Set the view's delegate sceneView.delegate = self // Show statistics such as fps and timing information sceneView.showsStatistics = true // Create a new scene let scene = SCNScene(named: "art.scnassets/ship.scn")! // Set the scene to the view sceneView.scene = scene } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) // Create a session configuration let configuration = ARWorldTrackingConfiguration() // Run the view's session sceneView.session.run(configuration) } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) // Pause the view's session sceneView.session.pause() } // MARK: - ARSCNViewDelegate /* // Override to create and configure nodes for anchors added to the view's session. func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? { let node = SCNNode() return node } */ func session(_ session: ARSession, didFailWithError error: Error) { // Present an error message to the user } func sessionWasInterrupted(_ session: ARSession) { // Inform the user that the session has been interrupted, for example, by presenting an overlay } func sessionInterruptionEnded(_ session: ARSession) { // Reset tracking and/or remove existing anchors if consistent tracking is required } }
调试日志:
2023-04-16 02:12:05.471666+0300 test[13308:8472291] [SceneConfiguration] Info.plist contained no UIScene configuration dictionary (looking for configuration named "(no name)") 2023-04-16 02:12:05.471706+0300 test[13308:8472291] [SceneConfiguration] Info.plist contained no UIScene configuration dictionary (looking for configuration named "(no name)") 2023-04-16 02:12:05.475942+0300 test[13308:8472291] Metal GPU Frame Capture Enabled 2023-04-16 02:12:05.476043+0300 test[13308:8472291] Metal API Validation Enabled test/ViewController.swift:26: Fatal error: Unexpectedly found nil while unwrapping an Optional value 2023-04-16 02:12:05.549337+0300 test[13308:8472291] test/ViewController.swift:26: Fatal error: Unexpectedly found nil while unwrapping an Optional value (lldb)
问题分析与解决方案
错误指向第26行的SCNScene(named: "art.scnassets/ship.scn")!强制解包,说明SCNScene(named:)返回了nil。即使文件存在,仍可能有以下原因:
文件未添加到项目Target
- 选中
ship.scn文件,打开右侧Inspector面板,在Target Membership区域勾选你的项目Target - 或进入项目设置的
Build Phases->Copy Bundle Resources,确认ship.scn在列表中,若缺失则点击+按钮添加该文件
- 选中
文件名/路径大小写不匹配
- iOS设备的文件系统区分大小写,而模拟器不区分。确保代码中的路径
art.scnassets/ship.scn与实际文件的名称、大小写完全一致(例如Ship.scn和ship.scn会被视为不同文件)
- iOS设备的文件系统区分大小写,而模拟器不区分。确保代码中的路径
Xcode缓存问题
- 按下
Command + Shift + Option + K清理构建文件夹 - 重启Xcode后重新构建运行项目
- 按下
验证资源文件夹类型
- 确保
art.scnassets是Xcode中的Group(蓝色文件夹图标),而非Folder Reference(黄色文件夹图标)。默认AR项目使用Group,若误设为Folder Reference会导致路径解析异常
- 确保
内容的提问来源于stack exchange,提问作者Mr.Schtelbe
相关产品推荐
相关产品推荐

