When a var
is marked as an optional Swift wraps it and when actual value is needed unwrapping is performed.
var anOptional : String? = "wrapping"
print("\(anOptional!) unwrapping")
What actually happens during wrapping and unwrapping of an optional?
An Optional is an enum with two possible cases,
.None
and.Some
. The.Some
case has an associated value which is the wrapped value. To "unwrap" the optional is to return that associated value. It is as if you did this: