Variable declaration with ! at end Swift 3

Asked

Viewed 370 times

3

Why in some variable statements in Swift3 an exclamation mark is added (!)?

For example:

var _nome: String!
var _idade: Double!

What does this entail of differences? When to use?

1 answer

5


There are data that are packaged in one type Optional where you may have a value or you may not have one. If you do not have a runtime error. So you need to check first if it is possible to use that value. This is done with the operator !. To use a value like this always use the exclamation right after it.

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

This serves as documentation for you to avoid codes that override this variable. But if you inadvertently override, the application will break if you don’t do a check.

This statement is called Implicitly Unwrapped Optionals.

They had the knife and the cheese in hand to make it right and avoid the call 1 billion dollar mistake, but did not.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.