[swift-evolution] Working with enums by name
Charlie Monroe
charlie at charliemonroe.net
Wed Jun 1 05:10:10 CDT 2016
This is, however, kind of a hack IMHO that relies on the compiler behavior that isn't well documented.
For example, this:
enum Planet {
case Earth
case Mars
}
"\(Planet.Mars)" // This is "Mars"
Works as well. You don't need to have the represented value to be String.
Note that this:
- works both when you have a plain enum, or enum Planet: Int, or whatever raw value kind
- does not work (!) when declared as @objc - then the result is "Planet".
> On Jun 1, 2016, at 9:52 AM, Patrick Smith via swift-evolution <swift-evolution at swift.org> wrote:
>
> I had no idea you could do this!!
>
>> On 1 Jun 2016, at 12:32 PM, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org> wrote:
>>
>> Who said anything about repeating the name?
>>
>> Welcome to Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.30). Type :help for assistance.
>> 1> enum Planet: String { case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune }
>> 2> Planet.mercury.rawValue
>> $R0: String = "mercury"
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
More information about the swift-evolution
mailing list