如何解决 Swift Playgrounds 中的“运行 此页面出现问题”消息?

How do I address the “There was a problem running this page” message in Swift Playgrounds?

我正在尝试使用 SceneKit 为 WWDC 学生挑战赛制作一些东西,这要求我的项目在 Swift 操场上。我不断收到这样的消息:“此页面 运行 有问题。”它不提供错误消息。它只是建议检查我的代码或重新开始。我曾尝试单独删除代码片段,但我无法找到此问题的根源。我还在 man Xcode playground 中尝试了 运行 它,它提供了警告,我引用,“the playground could not continue 运行 because the playground source did”。我卡住了。我的代码有什么问题。

import UIKit
import SceneKit
import QuartzCore 
import PlaygroundSupport

class GameScene: UIViewController, SCNSceneRendererDelegate {
    var primaryView: SCNView!
    var primaryScene: SCNScene!
    var cameraNode: SCNNode!
    
    override func viewDidLoad() {
        sceneAndViewInit()
        cameraInit()
        createGround()
        moonInit()
    }
    
    func createGround() {
        var ground = SCNBox(width: 200, height: 1, length: 200, chamferRadius: 0)
        var groundPhysicsShape = SCNPhysicsShape(geometry: ground, options: nil) 
        var groundNode = SCNNode(geometry: ground)
        ground.firstMaterial?.diffuse.contents = UIColor.orange
        ground.firstMaterial?.specular.contents = UIColor.white
        groundNode.position = SCNVector3(0, -6, 0)
        groundNode.physicsBody = SCNPhysicsBody(type: .static, shape: groundPhysicsShape)
        primaryScene.rootNode.addChildNode(groundNode)
    }
    
    func sceneAndViewInit() {
        primaryView = SCNView(frame: CGRect(x: 0, y: 0, width: 500, height: 300))
        primaryView.allowsCameraControl = true
        primaryView.autoenablesDefaultLighting = true
        
        primaryScene = SCNScene()
        
        primaryView.scene = primaryScene
        primaryView.isPlaying = true
    }
    
    func cameraInit() {
        var cameraNode = SCNNode()
        cameraNode.camera = SCNCamera()
        cameraNode.position = SCNVector3(x: 0, y: 0, z: 2)
        cameraNode.camera?.fieldOfView = 65
        cameraNode.camera?.wantsHDR = false
        primaryScene.rootNode.addChildNode(cameraNode)
    }
    
    func moonInit() {
        let moonScene = SCNScene(named: "Moon.scn")
        var moonNode = moonScene?.rootNode.childNode(withName: "Sphere", recursively: true)
        var moonPhysicsShape = SCNPhysicsShape(node: moonNode!, options: nil)
        moonNode?.position = SCNVector3(0, 0, 0)
        moonNode?.scale = SCNVector3(1, 1, 1)
        moonNode?.name = "Moon"
        moonNode?.physicsBody = SCNPhysicsBody(type: .static, shape: moonPhysicsShape)
        primaryScene.rootNode.addChildNode(moonNode!)
    }
    
    func renderer(_ aRenderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
        print(time)
    }
}

let gameScene = GameScene()
PlaygroundPage.current.liveView = gameScene

(“Moon.scn”是我在 resources/files 部分中的一个文件及其纹理,但没有任何与月亮相关的代码,我仍然有这个问题。)

我找到了解决这个问题的方法。但是,我只能为您提供一个关于问题是什么以及如何解决它的全面想法。
问题似乎出在 class.

顶部的变量声明
var primaryView: SCNView!
var primaryScene: SCNScene!
var cameraNode: SCNNode!

类型后面的感叹号表示变量是一个 implicitly-unwrapped optional(我引用 this Stack Overflow response 更深入地解释了这是什么),但或多或​​少的变量每当使用时都会自动强制解包。我对 Swift 的工作知识多于技术知识,所以我的猜测是,当创建 GameScene 对象时,合成初始化程序会尝试设置这些变量,出于某种原因,它,需要读取变量,获取 nil,无法继续。

解决方案似乎是将变量的值放在它们的初始声明中(可能有更好的解决方案,但这至少在我的测试中有效。),因此它们不是 nil初始化程序。现在我不能在这里完全解释这个问题的原因是这似乎只对 SCNScene 变量是必要的。我真的不知道为什么会这样。

代码(***标记重要评论):

import UIKit
import SceneKit
import QuartzCore 
import PlaygroundSupport

class GameScene: UIViewController, SCNSceneRendererDelegate {
    var primaryView: SCNView!// = SCNView(frame: CGRect(x: 0, y: 0, width: 500, height: 300)) //***This seems to work for reasons beyond me with the implicitly-unwrapped optional***
    var primaryScene = SCNScene() //***The implicitly unwrapped optional is removed here***
    var cameraNode = SCNNode() //***The implicitly unwrapped optional is removed here (I am not sure if that is necessary but it probably good practice not to use it anywhere intros code.  I use it earlier to demonstrate its weird complexities.)***
    
    override func viewDidLoad() {
        sceneAndViewInit()
        cameraInit()
        createGround()
        //moonInit() ***I removed this for simplicity in the answer***
    }
    
    func createGround() {
        var ground = SCNBox(width: 200, height: 1, length: 200, chamferRadius: 0)
        var groundPhysicsShape = SCNPhysicsShape(geometry: ground, options: nil) 
        var groundNode = SCNNode(geometry: ground)
        ground.firstMaterial?.diffuse.contents = UIColor.orange
        ground.firstMaterial?.specular.contents = UIColor.white
        groundNode.position = SCNVector3(0, -6, 0)
        groundNode.physicsBody = SCNPhysicsBody(type: .static, shape: groundPhysicsShape)
        primaryScene.rootNode.addChildNode(groundNode)
    }
    
    func sceneAndViewInit() {
        primaryView = SCNView(frame: CGRect(x: 0, y: 0, width: 500, height: 300))
        primaryView.allowsCameraControl = true
        primaryView.autoenablesDefaultLighting = true
        
        //primaryScene = SCNScene() ***This is not necessary when it is declared above***
        
        self.primaryView.scene = primaryScene
        //primaryView.isPlaying = true
        
        self.view = primaryView //***I realized later you need this bit of code or the program will only display a blank screen***
    }
    
    func cameraInit() {
        //var cameraNode = SCNNode()
        cameraNode.camera = SCNCamera()
        cameraNode.position = SCNVector3(x: 0, y: 0, z: 2)
        cameraNode.camera?.fieldOfView = 65
        cameraNode.camera?.wantsHDR = false
        primaryScene.rootNode.addChildNode(cameraNode)
    }
    
    func moonInit() {
        let moonScene = SCNScene(named: "Moon.scn")
        var moonNode = moonScene?.rootNode.childNode(withName: "Sphere", recursively: true)
        var moonPhysicsShape = SCNPhysicsShape(node: moonNode!, options: nil)
        moonNode?.position = SCNVector3(0, 0, 0)
        moonNode?.scale = SCNVector3(1, 1, 1)
        moonNode?.name = "Moon"
        moonNode?.physicsBody = SCNPhysicsBody(type: .static, shape: moonPhysicsShape)
        primaryScene.rootNode.addChildNode(moonNode!)
    }
    
    func renderer(_ aRenderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
        print(time)
    }
}

let gameScene = GameScene()
PlaygroundPage.current.liveView = gameScene

最后一点: self.view = primaryView 代码非常重要。我在完整代码中做了注释,但我发现这种方式很难在互联网上找到。我认为这与 UIViewController 有关(我对 UIKit 了解不多。),但是如果您 SceneKitUIKit 只是显示一个空白屏幕现在确实有那么一点代码。如果你不知道这些是不同的问题,它们真的很难破译。

要查看完整的错误消息,请转到“设置”应用 -> Playgrounds -> 打开“创作调试模式”。这并不能直接解决问题,但对定位错误更有帮助。