Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the ! in Swift called an 'implicitly' rather than 'explicitly' unwrapped optional?

Tags:

swift

The name of the ! always confuses me: it's called an 'implicitly unwrapped optional'. However, what is implicit about it? Implicit means "implied though not plainly expressed." However, does not adding a ! plainly express its purpose? Does not adding a ! make it explicit what we are trying to accomplish?

like image 888
Snowman Avatar asked Jul 22 '14 00:07

Snowman


People also ask

What is implicitly unwrapped optional in Swift?

Checking an optionals value is called “unwrapping”, because we're looking inside the optional box to see what it contains. Implicitly unwrapping that optional means that it's still optional and might be nil, but Swift eliminates the need for unwrapping.

What is implicit and explicit in Swift?

Explicit captures are always by-value captures, whereas implicit captures will be by-reference if the captured variable is a var .

Why use implicitly unwrapped optional?

For the most part, Implicitly Unwrapped Optionals should be avoided because if used mistakenly, your entire app will crash when it is accessed while nil . If you are ever not sure about whether a variable can be nil, always default to using a normal Optional.

What is implicit in Swift?

Implicit returns are optional and solely used for shortening the code within a function body. They are not mandatory and are up to the developer's stylistic preference. Note: Both explicit and implicit returns are valid but keep in mind that implicit returns are only supported in Swift version 5.1 or later.


2 Answers

In Swift, trailing exclamation marks (!) are used in two different ways. One is called Forced Unwrapping. This is when you have a variable defined as an Optional and you want to basically assert that the value is not nil so that you can use it as if it were not an optional:

var optionalName: String? = "World"
if optionalName != nil {
    sayHelloTo(optionalString!)
}

An alternate way you could describe "Forced Unwrapping" is "Explicit Unwrapping", but forced adds the extra hint that the whole program will crash if you try to "Force Unwrap" an optional that is nil.

The second is when you are actually declaring the type of a variable and it is called an Implicitly Unwrapped Optional. This is for when you want to declare a variable that is technically optional, but that you will treat as if it is not optional. Every time you use this Implicitly Unwrapped Optional, it is in reality, doing the "Force Unwrapping" I described above:

var name: String! = "World"
if name != nil {
    sayHelloTo(name)
}

You can still test an Implicitly Unwrapped Optional for nil, but you can also use it directly as if it were not optional. That is why it is considered implicit. Every time you use it, it is automatically, implicitly unwrapped for you.

Like "Forced Unwrapping" an "Implicitly Unwrapped Optional" will still crash the entire program if you try to use it when it is nil

like image 186
drewag Avatar answered Jun 17 '23 21:06

drewag


Normal Optional

var foo : Foo? = Foo()
foo!.doSomething() // explicitly unwrap it and call the method

Implicitly Unwrapped Optional

var foo : Foo! = Foo()
foo.doSomething() // implicitly unwrap it and call the method just like it is not optional

In both case, you need something to declare an optional (? or !). Implicitly Unwrapped Optional does not add more. But you can omit unwrap operator when use it.

like image 39
Bryan Chen Avatar answered Jun 17 '23 20:06

Bryan Chen