JeremyRaven
JeremyRaven

Reputation: 119

Could not cast value of type 'UIView' to 'SCNView'

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.

Upvotes: 3

Views: 3877

Answers (4)

Evan
Evan

Reputation: 2040

If you're doing this programmatically, add the following code:

    override func loadView() {
        //sort of like changes type of self.view from UIView to SCNView
        self.view = SCNView()
    }

Upvotes: 1

Robert Belongia
Robert Belongia

Reputation: 1

Same thing happened to me when I deleted a UIButton, which also deleted the UIView. So add a UIView and change class to SCNView

Upvotes: 0

codethemall
codethemall

Reputation: 144

If you are setting all these stuff programmatically without storyboard, make sure to do these steps:

  1. General -> Deployment Info -> Clear the Main Interface section, leave empty.
  2. 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
}

  1. 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)

Upvotes: 6

Shehzad Ali
Shehzad Ali

Reputation: 1845

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

Upvotes: 8

Related Questions