Almeida Cavalcante Almeida Cavalcante - 2 months ago 7
Swift Question

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

I'm following a tutorial to create a "tetris" game in Swift with XCode 7. I followed every single step in this tutorial, but I'm getting a runtime error:

Could not cast value of type 'SCNView' (0x106c19778) to 'SKView' (0x1068fcad0).

My GameViewController.swift is as follows:

import UIKit
import SceneKit
import SpriteKit

class GameViewController: UIViewController {

var scene: GameScene!
var swiftris:Swiftris!

override func viewDidLoad() {
super.viewDidLoad()

//Configure the view
let skView = view as! SKView

skView.multipleTouchEnabled = false
//Create and configure the scene
scene = GameScene(size: skView.bounds.size)
scene.scaleMode = .AspectFill
scene.tick = didTick
swiftris = Swiftris()
swiftris.beginGame()

//Presente the scene.
skView.presentScene(scene)

scene.addPreviewShpaeToScene(swiftris.nextShape!){
self.swiftris.nextShape?.moveTo(StartingColumn, row: StartingRow)
self.scene.movePreviewShape(self.swiftris.nextShape!){
let nextShapes = self.swiftris.newShape()
self.scene.startTicking()
self.scene.addPreviewShpaeToScene(nextShapes.nextShape!) {}
}
}
}

override func prefersStatusBarHidden() -> Bool {
return true
}

func didTick(){
swiftris.fallingShape?.lowerShapeByOneRow()
scene.redrawShape(swiftris.fallingShape!, completion: {})
}
}


I already search for it on google and here and I didn`t find anything related to SCNView and SKView.

Thank you in advance.

Answer

I did find the problem with my code. I instantiate the SKView as the view inside GameViewController:

self.view = SKView()    
let skView = view as! SKView

Before I do that, I tried to change the class of the view to SKView in the storyboard, but it was not possible.

I'd like to thank you for the revisions.