I have an implicitly unwrapped optional with value nil
.
Why does it print nil
and not crash?
var x : Int!
print(x) // nil
I have an implicitly unwrapped optional with value nil
.
Why does it print nil
and not crash?
var x : Int!
print(x) // nil
That does not crash because print
accepts Any
as the first parameter. Are implicitly unwrapped optionals a kind of Any
? Yes they are! Anything is Any
. There is no need to unwrap the optional. An implicitly unwrapped optional can be used in a place where Any
is expected without unwrapping the optional.
That could potentially be confusing because now you have something with type Any
, which doesn't look like it's optional, but it is an optional under the hood. To avoid this, Swift will output a warning telling you that you are implicitly coercing whatever optional type to Any
.
You need to use !
to force unwrap it here:
print(x!)
The reason why it's not crashing is because you are not force-unwrapping it in print(x)
. It will only crash one you try to force-unwrap it like print(x!)
IUO(Implicitly Unwrapped Optionals) are just special type of Optionals which will be automatically unwrapped one you use it inside chaining (say optional chaining), otherwise they remain Optionals.
Consider an example:
var implicitlyUO: Int!
implictlyUO = 4
print(implicitlyUO) // prints Optional(4)
print(implicitlyUO.advanced(by: 3)) // prints 7
implicitlyUO = nil
print(implicitlyUO) // prints nil
print(implicitlyUO.advanced(by: 3)) // Here it will crash as now it will be unwrapped and found it nil
var someNilValue:Int?
someNilValue = 4
print(someNilValue) // prints Optional(4)
print(someNilValue?.advanced(by: 3)) // prints Optional(7)
someNilValue = nil
print(someNilValue) // prints nil
print(someNilValue?.advanced(by: 3)) // prints nil
print(someNilValue!.advanced(by: 3)) /* Will crash here as we are force unwrapping nil which is the same case as implicitlyUO */
The only advantage you get with IUO is that you don't need !
(or explicitly unwrapping) in your code unnecessarily but you need to make sure that it is not nil.