I was very confused about forced unwrapping and implicit unwrapping at first. Now, the following understanding comes from my self-study:
There is no action available for implicit unwrapping, but there is something called implicitly unwrapped Optionals. Implicitly unwrapped Optionals and normal Optionals are both Optionals, the difference being when accessing an implicitly unwrapped Optional, you confidently know that there is a valid value under the hood and ready for use. Normal Optionals need if let
binding or a forced unwrapping (!
) action to access the possible values behind the optional variables.
Summary:
Forced unwrapping is an action done on the normal Optionals.
Implicitly unwrapped Optionals are Optionals, usually used for class initialization and will pass values without exclamation mark when used.
Question:
Am I right? If my understanding is not precise, I would appreciate it if you correct me.
Thanks
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.
It's only after the view controller is initialized that it loads its view. This also means that any outlets declared in the view controller class don't have a value immediately after the view controller's initialization. That's why an outlet is always declared as an (implicitly unwrapped) optional.
We can use the nil coalescing operator right in the String with String interpolation: print (\(myString ?? ""))
An Optional value is a container of some type (Int
, String
, UIColor
, ...), it could contain the value (1
, "Hello world"
, .greenColor()
, ...) or nil
.
let anOptionalInt: Int? = 1 let anotherOptionalInt: Int? = nil
When in Swift we see an Optional value we think:
Ok this could contain the actual value or
nil
It's the action of extracting the value contained inside an Optional
. This operation is dangerous because you are telling the compiler: I am sure this Optional value does contain a real value, extract it!
let anOptionalInt: Int? = 1 let anInt: Int = anOptionalInt!
Now anInt
contains the value 1.
If we perform a force unwrapping on an Optional value that happens to contain nil
we get a fatalError
, the app does crash and there is no way to recover it.
let anotherOptionalInt: Int? = nil let anotherInt = anotherOptionalInt! fatal error: unexpectedly found nil while unwrapping an Optional value
When we define an Implicitly unwrapped optional, we define a container that will automatically perform a force unwrap each time we read it.
var text: String! = "Hello"
If now we read text
let name = text
we don't get an Optional String
but a plain String
because text
automatically unwrapped it's content.
However text is still an optional so we can put a nil
value inside it
text = nil
But as soon as we read it (and it contains nil
) we get a fatal error because we are unwrapping an optional containing nil
let anotherName = text fatal error: unexpectedly found nil while unwrapping an Optional value
An implicitly unwrapped optional is a normal optional behind the scenes, but can also be used like a non optional value, so yes you are correct.
But if you declare a value as implicitly unwrapped, it is equivalent to force unwrapping it at every use.
For Implicitly unwrapped Optionals there are 4 main reasons to do that.
1: A Constant That Cannot Be Defined During Initialization
2: Interacting with an Objective-C API
3: When Your App Cannot Recover From a Variable Being nil
4: NSObject
Initializers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With