This is because you are creating the colours variable as an optional type. If you do like this:
var colours: Colours colours = .Red
you do not have to disclose this value
If we look at what an optional type is, we will see that this enum is like:
enum Optional<T> { case Some(T) case None }
And it can be Some Int type, for example, or None , in which case it is nil.
When you do this:
var colours: Colours!
directly indicates to you ! that it is not a type of colours , but it is an enum ImplicitlyUnwrappedOptional<Colours> type enum ImplicitlyUnwrappedOptional<Colours> . At the time of creation there will be Some<Colours> if it is equal, but with this ! you have it enum ImplicitlyUnwrappedOptional<Colours> , and the next moment it will be None . That is why you should use ! in switch :
Your colours value is of type ImplicitlyUnwrappedOptional<Colours> and can be colours or nil , and you need to directly indicate that it is colours enter `switch``.
Alexey Pichukov Nov 12 '15 at 12:08 2015-11-12 12:08
source share