28

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

4 回答 4

63

转到 Storyboard,选择包含 SpriteKit 游戏的 UIViewController,然后从左侧菜单中选择视图:

在此处输入图像描述

现在去 Identity Inspector 并确保 Class isSKView和 not UIView

在此处输入图像描述

您现在应该能够从以下代码编译这部分代码UIViewController

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

或者在 Swift 中:

let skView = self.view as! SKView
于 2015-06-12T11:31:13.907 回答
16

对于那些不想使用 a 的人Storyboard,您可以SKViewViewController'sloadView函数中简单地将视图创建为 a 。

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

  override func viewDidLoad() {
    let skView = view as! SKView
    ...
  }
}
于 2017-06-22T07:47:41.723 回答
2

简单的修复。您只需将您使用的每个视图控制器的视图从 UIView 更改为 SKView。

于 2018-03-16T18:56:35.040 回答
-2

我终于修好了!我不必将函数放在 GameViewController 中并从游戏场景中调用它们,而是必须将函数放在 gameScene 中并用 self.view 替换视图!

于 2015-06-20T15:16:09.663 回答