You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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。即使文件存在,仍可能有以下原因:

  1. 文件未添加到项目Target

    • 选中ship.scn文件,打开右侧Inspector面板,在Target Membership区域勾选你的项目Target
    • 或进入项目设置的Build Phases -> Copy Bundle Resources,确认ship.scn在列表中,若缺失则点击+按钮添加该文件
  2. 文件名/路径大小写不匹配

    • iOS设备的文件系统区分大小写,而模拟器不区分。确保代码中的路径art.scnassets/ship.scn与实际文件的名称、大小写完全一致(例如Ship.scn和ship.scn会被视为不同文件)
  3. Xcode缓存问题

    • 按下Command + Shift + Option + K清理构建文件夹
    • 重启Xcode后重新构建运行项目
  4. 验证资源文件夹类型

    • 确保art.scnassets是Xcode中的Group(蓝色文件夹图标),而非Folder Reference(黄色文件夹图标)。默认AR项目使用Group,若误设为Folder Reference会导致路径解析异常

内容的提问来源于stack exchange,提问作者Mr.Schtelbe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 21:37:53