29

Could not cast value of type 'UIView' (0x112484eb0) to 'SKView' (0x111646718). I keep on getting this error. Can anyone help me because the line of code is there by default. I converted the SK game into the latest swift syntax using the xcode 7 beta. The compiler was missing a lot of things, that is why i came back to xcode 6. I have no red errors; but, the game crashes and says thread 1: signal SIGARBT. Any tips on the changes between swift 2 and the latest version of swift 1 that could have caused problems in the conversion to the latest swift syntax. Perhaps something that was converted, that I should change in xcode 6.3.2. I will delete the beta once this crash issue is resolved. There is probably an error in the code, because i got the same error when i copied the code onto a brand new xcode project. Thanks in advance!

let skView = self.view as! SKView
Aryaman Goel
  • 538
  • 1
  • 4
  • 15
  • What is `self` And how is its view property set?. The message is pretty clear - self.view is a UIview, not an SKView. – Paulw11 Jun 11 '15 at 22:02
  • the line is pre written. how do i fix the problem? – Aryaman Goel Jun 11 '15 at 22:23
  • 1
    It isn't the line that is the problem. It is the configuration of the scene in the storyboard. The scene for this object specifies a UIView for the view outlet, not an SKView – Paulw11 Jun 11 '15 at 22:25
  • i don't have any lines of code with sk view except for the defaults! – Aryaman Goel Jun 11 '15 at 22:32
  • 1
    It isn't code. It is the storyboard scene – Paulw11 Jun 11 '15 at 22:33
  • what should i do with the storyboard? – Aryaman Goel Jun 11 '15 at 22:35
  • 1
    As I said above, the view outlet for the class is an instance of a UIView, not an SKView - check the scene, what is the outer most view? What is its class? – Paulw11 Jun 11 '15 at 22:36
  • the class is gameViewController as stated in main.storyboard. Sorry I started this recently. Where do i find the outer most view? – Aryaman Goel Jun 11 '15 at 22:38
  • 1
    On your Storyboard, check your GameViewController( *UIViewController) and then click on the empty view. If you go to Identity Inspector (right menu, third icon), you should see Class SKView, but you might have UIView and that is causing the error. Change this class to SKView. - Added an answer with images – aramusss Jun 12 '15 at 11:27
  • Old question but, *"There is probably an error in the code"* is not helpful **at all**. In what class is this code? What is the type of your controller's view in your storyboard? Are you using a container view? You should make these clear, so that we can reproduce the problem. Also *the line of code is there by default*, by default how? Are you using a template, or sample code, or did you just create a UI element and this was there? Question, hence also your answer, is not clear. That is why there are answers unrelated to what you intended, but are valid because they fix the issue in the title – MuhsinFatih May 02 '18 at 01:58
  • 1
    I believe you should accept either ArinW. 's or aramusss 's answer, and if you want to, ask the question in more clear terms and post your answer to help others with the same issue, that is if the issue is still reproducable (as the swift language and probably the framework has evolved after this post). E.g: with title "Could not cast value of type 'UIView' to 'SKView' when " and with reproducable steps. – MuhsinFatih May 02 '18 at 02:16
  • Possible duplicate of [Could not cast value of type 'UIView' to 'SCNView'](https://stackoverflow.com/questions/35323147/could-not-cast-value-of-type-uiview-to-scnview) – MuhsinFatih May 02 '18 at 02:19

4 Answers4

64

Go to your Storyboard, select your UIViewController that contains the SpriteKit game, and select the view from left menu:

enter image description here

Now go to Identity Inspector and make sure Class is SKView and not UIView:

enter image description here

You should now be able to compile this part of code from your UIViewController:

// Configure the view.
SKView * skView = (SKView *)self.view;

Or in Swift:

let skView = self.view as! SKView
aramusss
  • 2,391
  • 20
  • 30
  • what else could it be? – Aryaman Goel Jun 12 '15 at 12:04
  • i made a new xcode project and copied all the code. Still the same error. – Aryaman Goel Jun 12 '15 at 12:07
  • If you create a new Xcode project, select Game, with Swift and (without modifying any code) run the project, do you get the same error? If so, i recommend you to re-install Xcode from scratch (without BETAS, stable version from Apple Store) – aramusss Jun 12 '15 at 13:24
  • it is stable otherwise. I am using xcode 6.3.2 stabled. – Aryaman Goel Jun 12 '15 at 13:29
  • @Aryaman , did you find a solution? Post it here and accept the answer! :) – aramusss Jun 13 '15 at 13:31
  • No i couldn't still waiting for it to be resolved. I deleted xcode 7 beta, because it had far more error messages than xcode 6.3.2. I'll have to go through all my code. – Aryaman Goel Jun 13 '15 at 14:41
  • Try uninstalling completely Xcode and reinstall then Xcode 6 from App Store using this command: `sudo /Developer/Library/uninstall-devtools --mode=all` – aramusss Jun 14 '15 at 09:25
  • No the problem is still there! Something in the code! The code looks all fine to me! – Aryaman Goel Jun 17 '15 at 00:59
  • @Aryaman, after trying this command did you create a new project or run the existing one? Create an existing project, you can't have any error – aramusss Jun 17 '15 at 06:43
  • made new project. when i add the code, the problem comes again. Nothing to do with skview and uiview just something else. any changes the 'change to latest swift syntax' could have done for a spritekit game? – Aryaman Goel Jun 17 '15 at 15:54
  • 2
    thanks for the answer. It was helpful for me. But I also have to UIView object itself to ViewController :/ – Andrei Konstantinov Jun 21 '15 at 14:26
19

For those who are not wanting to use a Storyboard, you can simple create the view as an SKView in the ViewController's loadView function.

class ViewController: UIViewController {
  override func loadView() {
    self.view = SKView()
  }

  override func viewDidLoad() {
    let skView = view as! SKView
    ...
  }
}
marcel
  • 555
  • 6
  • 12
2

Simple fix. You just have to change the View in which every view controller your using from UIView to SKView.

Arin
  • 155
  • 1
  • 7
-2

I finally fixed it! Instead of putting the functions in the GameViewController and calling them from game scene, I had to put the functions in gameScene and replace view with self.view!.

Aryaman Goel
  • 538
  • 1
  • 4
  • 15