Could not cast value of type 'UIView' (0x1

2019-01-18 19:49发布

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

4条回答
欢心
2楼-- · 2019-01-18 19:53

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
    ...
  }
}
查看更多
仙女界的扛把子
3楼-- · 2019-01-18 19:55

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
查看更多
戒情不戒烟
4楼-- · 2019-01-18 19:57

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

查看更多
倾城 Initia
5楼-- · 2019-01-18 20:02

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!.

查看更多
登录 后发表回答