SLN SLN - 1 year ago 35
Swift Question

Why there is no optional(nil) but only nil?

Given the follwoing code example:

var ResponseCode: Int? = 404
print(ResponseCode) // --> Optional(404)
ResponseCode = nil
print(ResponseCode) // --> nil (why not Optional(nil)?)

Question: why when I printed a nil-valued option it is just a
. Why not also showing the

Thanks for your time and help.


This is rather simple to explain. Optional is an enum that has two values: .Some(T) and .None.

As you see, only one of them has a parameter.

The string description of .Some(T) is "Optional(description of T)". .None has no parameters so the description is just nil.