Variable declaration with! at the end Swift 3

3

Why do some variable declarations in Swift3 add an exclamation point (!)?

For example:

var _nome: String!
var _idade: Double!

What does this entail? When should it be used?

    
asked by anonymous 17.02.2017 / 19:33

1 answer

5

There is data that is packaged in a type Optional where it can have a value or may not have value. If it does not it causes a runtime error. So you need to first check if you can use that value. This is done with the ! operator. To use such a value you always have to use the exclamation right after it.

In some variables you know that it is forever to have a value, so declare it already with the exclamation. This way you no longer have to use the exclamation mark in every use of that variable.

This serves as documentation for you to avoid codes that override this variable. But if you inadvertently undo, the application will break if you do not do a scan.

This statement is called Implicitly Unwrapped Optionals .

They had the knife and cheese on hand to do right and avoid calling 1 billion dollar bug , but did not do it.

    
17.02.2017 / 20:04