1

I have just built a SpriteKit demo app. I don't under the code below

if let view = self.view as! SKView? {
        ...
    }

What does (self.view as! SKView?) meaning? Why don't they just write code like this:

if let view = self.view as? SKView {
        ...
    }

What's the difference? Thanks a lot!

Rufus
  • 640
  • 1
  • 7
  • 14
  • The first syntax (*forced unwrapping* an optional to an optional) is a contradiction in terms and shouldn't be used at all. I wish the compiler was throwing an error when being used in an optional binding. – vadian Dec 16 '16 at 09:40
  • Possible duplicate of [Casting to generic optional in Swift](http://stackoverflow.com/questions/32080420/casting-to-generic-optional-in-swift) – Federico Malagoni Dec 16 '16 at 09:51
  • @Eric Has your question been answered? – Alexander Jan 20 '17 at 08:52
  • I get these codes in SpriteKit default demo created by Xcode 8.2. I think the developer use ( as! ) because he want to express that (self.view)is already set to a SKView instance and will never be nil. In the meanwhile, he wants to use ( if let ) syntax, so he need a optional type (SKView?). Maybe it is just a habit and not necessary at all. – Rufus Jan 29 '17 at 12:59

2 Answers2

1

Case 1

self.view is force cast to SKView? (a.k.a. Optional<SKView>). If this cast fails (when self.view isn't actually an SKView?), then the program will crash to prevent going into an inconsistent state.

The resulting SKView? is then safely unwrapped and assigned to view if it's not nil, otherwise the else case is executed, if there is one.

Case 2

self.view is safely cast to SKView. The result of this expression is a SKView. It will have a valid value (if the cast was possible), or nil if the cast wasn't possible. Unlike the first case, it won't crash.

The resulting SKView? is then safely unwrapped and assigned to view if it's not nil, otherwise the else case is executed, if there is one.

Alexander
  • 59,041
  • 12
  • 98
  • 151
  • It will. http://swiftlang.ng.bluemix.net/#/repl/5853c53e6749731cc79801b0 @AppzYourLife – Alexander Dec 16 '16 at 10:43
  • Thanks,Alexander. Though I still not quite understand the meaning of code in the demo. Maybe it is just a syntax and do not need pay much attention. – Rufus Jan 29 '17 at 12:28
  • @Eric I mean, I gave explanations exactly addressing the particular example in mind. Perhaps try asking me follow up questions, and I'll try to address the confusion. It's not just syntax, the two options are semantically different. It's definitely important to know the difference – Alexander Jan 29 '17 at 13:22
0
if let view = self.view as? SKView { ... }

This means that if the view can be cast to SKView, then do it, otherwise don't do it. This is safe as it won't crash the program and is recommended. That's what the question mark means. Can it be cast as an SKView?

if let view = self.view as! SKView? { ... }

This really shouldn't be used at all. You're saying that the view should be cast by force to an optional SKView that will work if SKView can be unwrapped. Like another user stated, it's repetitive and shouldn't be used even if it won't give you an error.

Hedylove
  • 1,724
  • 16
  • 26