Using a variable in place of an instance

20 views
Skip to first unread message

Boyd Crow

unread,
Nov 5, 2015, 9:33:58 PM11/5/15
to Swift Language
There are many syntax elements in Swift of the order of 

enumRank.Eight

which in this case refers to enum enumRank Case "Eight" 

In actual use, a user would enter something as a variable, for instance enumCase.

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?

Ken Ferry

unread,
Nov 5, 2015, 9:44:16 PM11/5/15
to Boyd Crow, Swift Language
There probably isn’t as general a way to do this as you’re thinking, but you can use raw values if your enum is like a C enum.

enum Bar : Int {
    case Zero = 0
    case One
    case Two
    case Three
    case Eight = 8
}

let num = 8
let bar = Bar.init(rawValue: num)

-ken

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swift-languag...@googlegroups.com.
To post to this group, send email to swift-l...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/cdea13c4-c54a-496a-bfed-d9cb602dc09c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jens Alfke

unread,
Nov 5, 2015, 11:51:00 PM11/5/15
to Boyd Crow, Swift Language
On Nov 5, 2015, at 5:40 PM, Boyd Crow <misterc...@gmail.com> wrote:

In actual use, a user would enter something as a variable, for instance enumCase.

How do I use the variable to describe the case?

I don’t understand the question. The values of the enum are named EnumRank.Eight, etc.

What do you mean by “a user would enter something” … what would they enter it in? And what is ‘enumCase’?

—Jens

Brent Royal-Gordon

unread,
Nov 6, 2015, 4:01:09 AM11/6/15
to Boyd Crow, Swift Language
> 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

Boyd Crow

unread,
Nov 6, 2015, 12:47:31 PM11/6/15
to Swift Language
I have been using rawValue in this particular usage but I'm not really interested in a lesson on enums, which I am studying intensely.

I'm talking about the general case where a method has several instances available to it, but I do not know which instance I will want to use in advance.

At some point I get user input or some other means to assign a variable which will contain the TEXT of the instance name.

How do I substitute the instance name by that variable?

If it can't be done, it can't be done but it seems like something I've been able to do in other languages.

Boyd Crow

Jens Alfke

unread,
Nov 6, 2015, 1:04:41 PM11/6/15
to Boyd Crow, Swift Language
On Nov 6, 2015, at 9:47 AM, Boyd Crow <misterc...@gmail.com> wrote:

At some point I get user input or some other means to assign a variable which will contain the TEXT of the instance name.
How do I substitute the instance name by that variable?

You build your own dictionary that maps names to enum values. As has already been said, Swift does not support this type of introspection.

(Even if the language let you do this, it would probably be a bad idea to expose the internal enum names in your program’s user interface. For one thing, it’s not localizable. For another, Swift identifiers don’t allow niceties like whitespace, so you’d have to show dorky names like “TenOfSpades” or “speed_up” in your UI :-p )

If it can't be done, it can't be done but it seems like something I've been able to do in other languages.

Scripting languages tend to support that type of stuff, since they’re very dynamic. It’s not something you’ll find in C or C++, though.

—Jens
Reply all
Reply to author
Forward
0 new messages