2

I am new to SpriteKit and see the code like let skView = view as! SKView so what is use of this? Also I am confused about the difference between view and skView, scene and GameScene?

class GameViewController: UIViewController {

  override func viewDidLoad() {
    super.viewDidLoad()
    let scene = GameScene(size: view.bounds.size)
    let skView = view as! SKView
    skView.showsFPS = true
    skView.showsNodeCount = true
    skView.ignoresSiblingOrder = true
    scene.scaleMode = .resizeFill
    skView.presentScene(scene)
  }

  override var prefersStatusBarHidden: Bool {
    return true
  }

} 
EmilioPelaez
  • 18,758
  • 6
  • 46
  • 50
Alvin123
  • 53
  • 1
  • 7
  • What is "!" after as? – Alvin123 Nov 29 '16 at 18:14
  • 4
    The things you are confused about are very basic (casting, variable names, classes). It seems to me like you are getting ahead of yourself and should start learning Swift by the basics. – EmilioPelaez Nov 29 '16 at 18:15
  • I know its the casting view to SKView but what is use of "!" ? – Alvin123 Nov 29 '16 at 18:20
  • The use of `?` and `!` in swift is foundational. Not understanding Optionals in Swift is like not understanding `&&` and `||` in C. If you don't know what `?` and `!` are, stop and read a book on Swift. Apple's Swift iBook would be a good place to start. – Duncan C Nov 29 '16 at 18:39

2 Answers2

0

as! means "force cast". The value of view will be casted as SKView. If this fails, because view is nil or of a different type, the code will crash. That's why you should avoid using as! when you are not 100% sure of what type the value is.


Related:

Community
  • 1
  • 1
FelixSFD
  • 6,052
  • 10
  • 43
  • 117
  • Thank you for the answer! That's perfect. what is relation between view and SKView? – Alvin123 Nov 29 '16 at 18:25
  • Usually, the `view` property of an `UIViewController` is of type `UIView`. To edit some properties like `showsFPS` (which is only available in `SKView`, a subclass of `UIView`), you need to tell the compiler that in that case, `view` will be of the type `SKView` – FelixSFD Nov 29 '16 at 18:28
  • Thanks. where can i know all these relations ? any book or place to talk about these? – Alvin123 Nov 29 '16 at 18:45
  • You can find this information in the official [API Reference](https://developer.apple.com/reference/). For example: [SKView](https://developer.apple.com/reference/spritekit/skview) /// You can also see the full API Reference in Xcode: Window -> Documentation and API Reference – FelixSFD Nov 29 '16 at 18:48
  • Thank you . That's exactly what i am looking for. – Alvin123 Nov 29 '16 at 18:53
  • Another question is that when i looked at he API is , what is relation between SKView ,SKScene,SKNode? – Alvin123 Nov 29 '16 at 19:26
  • Sorry, but that's too much for a comment and I don't know much about SpriteKit. I would recommend you to read tutorials like [this](https://www.raywenderlich.com/145318/spritekit-swift-3-tutorial-beginners) about Sprite kit. – FelixSFD Nov 29 '16 at 19:28
0

SKView is a subclass of UIView that has additional capabilities for SpriteKit functionality. The as! operator is downcasting the view of UIView to a SKView, to enable using SKView specific functionality in viewDidLoad().

Generally, the as? operator is recommended because it will gracefully fail to nil if it doesn't succeed, whereas as! will crash the app if the casting is unsuccessful. If you are 100% certain that you will always have the view is of type SKView in this case, then using as! can be an appropriate and less verbose choice.

For more information, I recommend reading this Stackoverflow post that compares the use of as, as?, and as!.

Community
  • 1
  • 1
  • Can we allocate the object of SKView class instead of casting ? Maybe like let skView = SKView () – Alvin123 Nov 29 '16 at 19:29
  • Yes we can instantiate skView = SKView(). But, the Storyboard hands us a view already. That view was not created programmatically by us so we just downcast it to utilize functionality from SKView class. Were we to instantiate our own SKView() object as you suggest then we would have to add it as a subview to the main view already given to us. There is is no way I know of to "convince" the storyboard to give us an SKView in the first place. I have tried editing the XML (right click storyboard file -> open as source) for fun. But there is no tag available. – DMS Dec 13 '20 at 02:21