> How do I use the variable to describe the case?
>
> I've tried
>
> enumRank.\(EnumCase)
>
> but, of course, that didn't work.
>
> There must be some way to convert variables "fill in" elements in the dotted syntax elements.
>
> Anybody know?
There’s no built-in way to look up an enum case by name; that sort of behavior is usually reserved for much more dynamic languages than Swift.
You do have a couple options to get it for a specific enum, however. One is to build a dictionary mapping the names to the cases:
enum Suit {
case Hearts, Spades, Diamonds, Clubs
static var byName = [ “Hearts”: Hearts, “Spades”: Spades, “Diamonds”: Diamonds, “Clubs”: Clubs ]
}
The other is to take advantage of a shortcut which I believe was introduced in Swift 2: an enum with a raw value of type String will, by default, give each case a raw value corresponding to its name.
enum Suit: String {
case Hearts, Spades, Diamonds, Clubs
}
Suit(rawValue: “Hearts”) // Optional.Some(Suit.Hearts)
--
Brent Royal-Gordon
Architechies