Using a variable in place of an instance

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Using a variable in place of an instance

Boyd Crow
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?

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

Re: Using a variable in place of an instance

Ken Ferry
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

On Nov 5, 2015, at 5:40 PM, Boyd Crow <[hidden email]> wrote:

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?

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/B2C20033-0F82-476D-A8D3-5D856221C6F3%40gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Using a variable in place of an instance

Jens Alfke
In reply to this post by Boyd Crow

On Nov 5, 2015, at 5:40 PM, Boyd Crow <[hidden email]> 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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/08F1374C-4FD3-4B33-8E53-53F4C58A8D7E%40mooseyard.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Using a variable in place of an instance

Brent Royal-Gordon
In reply to this post by Boyd Crow
> 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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/0F6A7E5D-EE57-40A3-9EE9-A414BED16C24%40architechies.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Using a variable in place of an instance

Boyd Crow
In reply to this post by Boyd Crow
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

On Thursday, November 5, 2015 at 6:33:58 PM UTC-8, Boyd Crow wrote:
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?

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/cd7f9d62-64e4-4570-ae64-ed54633bc30a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Using a variable in place of an instance

Jens Alfke

On Nov 6, 2015, at 9:47 AM, Boyd Crow <[hidden email]> 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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/23A1ED5E-4C44-4D91-B402-DC6BCC410D9B%40mooseyard.com.
For more options, visit https://groups.google.com/d/optout.