问题
App was building fine until I started adding buttons to the storyboard then I got the above error. It stops at this line of code inside my GameViewController.
let scnView = self.view as! SCNView
The storyboard itself has its custom class set to GameViewController (theres no option for SCNView). GameViewController itself inherits from UIViewController. Im using Xcode7.2.
回答1:
Make sure to also import SceneKit at the top of the file.
import SceneKit
Open the “Main.storyboard” File. Select ViewController -> View go to “Identity Inspector” and change the Class field to SCNView. SceneKit Help
回答2:
If you are setting all these stuff programmatically without storyboard, make sure to do these steps:
- General -> Deployment Info -> Clear the Main Interface section, leave empty.
- Go to AppDelegate.swift and assign rootViewController of window
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow()
let gameViewController = GameViewController()
window?.rootViewController = gameViewController
window?.makeKeyAndVisible()
return true
}
- Go to GameViewController and here don't cast GameViewController's view to SCNView, instead create SCNView instance and add like subView:
let scnView = SCNView(frame: view.frame)
view.addSubview(scnView)
回答3:
Same thing happened to me when I deleted a UIButton, which also deleted the UIView. So add a UIView and change class to SCNView
来源:https://stackoverflow.com/questions/35323147/could-not-cast-value-of-type-uiview-to-scnview