0

I have the following code in GameViewController.swift:

override func viewDidLoad() {
    super.viewDidLoad()

    let scene: GameScene = GameScene()

    let currentView = self.view as! SKView
    currentView.showsFPS = true
    currentView.showsNodeCount = true

    currentView.presentScene(scene)
}

It crashes at let currentView = self.view as! SKView. with this message:

Could not cast value of type 'UIView' (0x1079b7e88) to 'SKView' (0x106b79718)

There was a similar question posed, but their solution was in the storyboard. I have no storyboards, just code.

Pang
  • 9,564
  • 146
  • 81
  • 122
Fine Man
  • 455
  • 4
  • 17

2 Answers2

3

You are getting a crash because your UIView's (i.e. your self.view) actual type is NOT an SKView. So you can't downcast to an SKView because it is not an SKView. So wherever you create your ViewController's view you must be assigning it a UIView. You said you are not using storyboards so I assume you are creating the ViewController programmatically. If this is the case, you need to manually assign your ViewController's view an SKView. This is typically done in the loadView method of the ViewController. You can see an example of this here

Community
  • 1
  • 1
Epic Byte
  • 33,840
  • 12
  • 45
  • 93
  • Thanks a ton! The link was also quite helpful. Where'd you find the Apple documentation you referred to in your answer on the other post? – Fine Man Jul 13 '15 at 18:13
  • I've surpassed the crash, but now the scene doesn't seem to show up. I added currentView.backgroundColor = UIColor.blueColor(), but the screen doesn't turn blue. – Fine Man Jul 13 '15 at 18:55
0

just write SKView in the storyboard of your ViewController file under custom class of the main UIView as shown in the fig. enter image description here This will simply solve all your problem.