SCNScene - 调用 SCNScene(named:) 的致命错误:线程 1:EXC_BAD_INSTRUCTION
SCNScene - fatal error calling SCNScene(named:): Thread 1: EXC_BAD_INSTRUCTION
我正在使用一个共享项目,并花了很多时间将其更新为 Swift 2,所以我想让它正常工作,但不幸的是我有一个运行时错误需要处理。情节提要板上没有任何内容,因此一切都是以编程方式完成的。看到它是以编程方式完成的,我不确定为什么会收到此运行时错误。
请告诉我这段代码中的 "glasses1.dae" 和 "glasses2" 是什么。这些是什么标识符?
function signature specialization of Swift.(_fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> ()).(closure #2)
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
Thread 1: EXC_BAD_INSTRUCTION ...
class ViewController: UIViewController {
private let notificationCenter : NSNotificationCenter = NSNotificationCenter.defaultCenter()
private let screenWidth : CGFloat = 320
private let scaleX : CGFloat = (320 / 750)
private let scaleY : CGFloat = (568 / 1334)
private let eyeRectL : UILabel = UILabel()
private let eyeRectR : UILabel = UILabel()
private var scnView : SCNView!
private var glasses : SCNNode!
override func viewDidLoad() {
super.viewDidLoad()
// Scene
let scene = SCNScene(named: "glasses1.dae")! // fatal error: unexpectedly found nil while unwrapping an Optional value
// Camera
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3(x: 0, y: 0, z: 0)
scene.rootNode.addChildNode(cameraNode)
// Light
let lightNode = SCNNode()
lightNode.light = SCNLight()
lightNode.light!.type = SCNLightTypeOmni
lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
scene.rootNode.addChildNode(lightNode)
// Ambient Light
let ambientLightNode = SCNNode()
ambientLightNode.light = SCNLight()
ambientLightNode.light!.type = SCNLightTypeAmbient
ambientLightNode.light!.color = UIColor.darkGrayColor()
scene.rootNode.addChildNode(ambientLightNode)
glasses = scene.rootNode.childNodeWithName("glasses2", recursively: true)! // what is "glasses2"?
scnView = SCNView()
scnView.scene = scene
scnView.backgroundColor = UIColor.clearColor()
scnView.frame = self.view.bounds
let moohaha = setupMoohaha()
let cameraView = visage.moohahaCameraView
self.view.addSubview(cameraView)
self.view.addSubview(scnView)
self.view.addSubview(eyeRectL)
self.view.addSubview(eyeRectR)
moohaha.beginFaceDetection()
}
.dae
文件包含场景信息,用于加载外部创建的场景(不是在代码中)。 "glasses1.dae" 字符串是应该属于您的项目的文件的名称。据推测,childWithNodeName(_:recursively:)
方法调用用于查找名称为 "glasses2" 的子节点,该子节点创建为用于创建场景的 .dea
文件中定义的场景的一部分。
你指出的两行末尾的!
运算符用于强制解包一个可选值,如果该值包含nil
,它将导致程序崩溃。因此,我假设您的场景没有被创建,因为 "glasses.dea" 文件实际上不是您项目的一部分并且 SceneKit 无法加载它并返回一个 nil
场景。
我正在使用一个共享项目,并花了很多时间将其更新为 Swift 2,所以我想让它正常工作,但不幸的是我有一个运行时错误需要处理。情节提要板上没有任何内容,因此一切都是以编程方式完成的。看到它是以编程方式完成的,我不确定为什么会收到此运行时错误。
请告诉我这段代码中的 "glasses1.dae" 和 "glasses2" 是什么。这些是什么标识符?
function signature specialization of Swift.(_fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> ()).(closure #2)
fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)
Thread 1: EXC_BAD_INSTRUCTION ...
class ViewController: UIViewController {
private let notificationCenter : NSNotificationCenter = NSNotificationCenter.defaultCenter()
private let screenWidth : CGFloat = 320
private let scaleX : CGFloat = (320 / 750)
private let scaleY : CGFloat = (568 / 1334)
private let eyeRectL : UILabel = UILabel()
private let eyeRectR : UILabel = UILabel()
private var scnView : SCNView!
private var glasses : SCNNode!
override func viewDidLoad() {
super.viewDidLoad()
// Scene
let scene = SCNScene(named: "glasses1.dae")! // fatal error: unexpectedly found nil while unwrapping an Optional value
// Camera
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3(x: 0, y: 0, z: 0)
scene.rootNode.addChildNode(cameraNode)
// Light
let lightNode = SCNNode()
lightNode.light = SCNLight()
lightNode.light!.type = SCNLightTypeOmni
lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
scene.rootNode.addChildNode(lightNode)
// Ambient Light
let ambientLightNode = SCNNode()
ambientLightNode.light = SCNLight()
ambientLightNode.light!.type = SCNLightTypeAmbient
ambientLightNode.light!.color = UIColor.darkGrayColor()
scene.rootNode.addChildNode(ambientLightNode)
glasses = scene.rootNode.childNodeWithName("glasses2", recursively: true)! // what is "glasses2"?
scnView = SCNView()
scnView.scene = scene
scnView.backgroundColor = UIColor.clearColor()
scnView.frame = self.view.bounds
let moohaha = setupMoohaha()
let cameraView = visage.moohahaCameraView
self.view.addSubview(cameraView)
self.view.addSubview(scnView)
self.view.addSubview(eyeRectL)
self.view.addSubview(eyeRectR)
moohaha.beginFaceDetection()
}
.dae
文件包含场景信息,用于加载外部创建的场景(不是在代码中)。 "glasses1.dae" 字符串是应该属于您的项目的文件的名称。据推测,childWithNodeName(_:recursively:)
方法调用用于查找名称为 "glasses2" 的子节点,该子节点创建为用于创建场景的 .dea
文件中定义的场景的一部分。
你指出的两行末尾的!
运算符用于强制解包一个可选值,如果该值包含nil
,它将导致程序崩溃。因此,我假设您的场景没有被创建,因为 "glasses.dea" 文件实际上不是您项目的一部分并且 SceneKit 无法加载它并返回一个 nil
场景。