0
votes

I know there are many questions regarding introspection in Swift, but I couldn't find a clear answer for mine:

Is there a way to know if a variable of type Any (a function parameter for instance) is an enum?

I tried myVar is enum, myVar is enum.self, myVar is enum.Type, ... I didn't worked obviously (I didn't had much hope anyway).

1
This question needs clarification. Using enumerations is a great way to make new types but an enumeration is not a type itself. If you have created a new enumeration type, is will work to test if your var is of that type. If you think you have to know whether a type was created as an enumeration, I imagine you are thinking about your problem incorrectly. Perhaps ask your question again after taking a step up the abstraction ladder. - Asa

1 Answers

0
votes

You can check if it is a case of a specific enumeration:

let suit = Suit.Spades
var item:Any
item = suit
item is Suit // true
item = "string"
item is Suit // false

You cannot check if it is a case of any enumeration.