SLN SLN - 5 months ago 7
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
nil
. Why not also showing the
Optional(nil)


Thanks for your time and help.

Answer

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.