无法将’UIView’类型的值转换为’SCNView’

应用程序正在构建正常,直到我开始向故事板添加按钮然后我得到上述错误。 它停在我的GameViewController内的这行代码中。

let scnView = self.view as! SCNView 

故事板本身的自定义类设置为GameViewController(SCNView没有选项)。 GameViewController本身inheritance自UIViewController。 我正在使用Xcode7.2。

确保还在文件顶部导入SceneKit。

 import SceneKit 

打开“Main.storyboard”文件。 选择ViewController – > View转到“Identity Inspector”并将Class字段更改为SCNView。 SceneKit帮助

如果您在没有故事板的情况下以编程方式设置所有这些内容,请确保执行以下步骤:

  1. 常规 – >部署信息 – >清除主界面部分,留空。
  2. 转到AppDelegate.swift并分配窗口的rootViewController
 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { window = UIWindow() let gameViewController = GameViewController() window?.rootViewController = gameViewController window?.makeKeyAndVisible() return true }