5

Can anyone explain in detail about the technical difference between ! & ? literals that we are using in swift language. Basically in apple developer document they mentioned it as the nil validation but i didn't understand clearly about the logic behind it. Can you one explain with some real time logic for the same.

Thanks in advance for the user.

Naveen
  • 121
  • 1
  • 8

1 Answers1

9

Optional Chaining as an Alternative to Forced Unwrapping

You specify optional chaining by placing a question mark (?) after the optional value on which you wish to call a property, method or subscript if the optional is non-nil. This is very similar to placing an exclamation mark (!) after an optional value to force the unwrapping of its value. The main difference is that optional chaining fails gracefully when the optional is nil, whereas forced unwrapping triggers a runtime error when the optional is nil.

The Swift Programming Language (emphasis added)

Nobody's going to be able to explain it more simply than that. What don't you understand?

Tommy
  • 99,986
  • 12
  • 185
  • 204