Clear an optional variable in Swift
var myImage: UIImage?
Is basically short hand for making a UIImage
point to nil
automatically.
So to reset it back to the original value say:
myImage = nil
Set its value back to nil like this
myImage = nil
Optional
is an enum
type in Swift; it has two cases:
enum Optional<T> : NilLiteralConvertible {
case None
case Some(T)
...
}
By assigning an image to your Optional<UIImage>
, you have implicitly specified .Some(image)
. To clear it, you can use .None
. But since Optional
also conforms to NilLiteralConvertible
, you can use the simpler and clearer nil
.