Given the following enum:
enum Audience { case Public case Friends case Private } How do I get the string "Public" from the audience constant below?
let audience = Audience.Public Given the following enum:
enum Audience { case Public case Friends case Private } How do I get the string "Public" from the audience constant below?
let audience = Audience.Public Not sure in which Swift version this feature was added, but right now (Swift 2.1) you only need this code:
enum Audience: String { case Public case Friends case Private } let audience = Audience.Public.rawValue // "Public" When strings are used for raw values, the implicit value for each case is the text of that case’s name.
[...]
enum CompassPoint: String { case North, South, East, West }In the example above, CompassPoint.South has an implicit raw value of "South", and so on.
You access the raw value of an enumeration case with its rawValue property:
let sunsetDirection = CompassPoint.West.rawValue // sunsetDirection is "West"
The idiomatic interface for 'getting a String' is to use the CustomStringConvertible interface and access the description getter. Define your enum as:
enum Foo : CustomStringConvertible { case Bing case Bang case Boom var description : String { switch self { // Use Internationalization, as appropriate. case .Bing: return "Bing" case .Bang: return "Bang" case .Boom: return "Boom" } } } In action:
> let foo = Foo.Bing foo: Foo = Bing > println ("String for 'foo' is \(foo)" String for 'foo' is Bing Updated: For Swift >= 2.0, replaced Printable with CustomStringConvertible
For now, I'll redefine the enum as:
enum Audience: String { case Public = "Public" case Friends = "Friends" case Private = "Private" } so that I can do:
audience.toRaw() // "Public" But, isn't this new enum definition redundant? Can I keep the initial enum definition and do something like:
audience.toString() // "Public" I like to use Printable with Raw Values.
enum Audience: String, Printable { case Public = "Public" case Friends = "Friends" case Private = "Private" var description: String { return self.rawValue } } Then we can do:
let audience = Audience.Public.description // audience = "Public" or
println("The value of Public is \(Audience.Public)") // Prints "The value of Public is Public" Updated for the release of Xcode 7 GM. It works as one would hope now--thanks Apple!
enum Rank:Int { case Ace = 1, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King } let r = Rank.Ace print(r) // prints "Ace" print("Rank: \(r)!") // prints "Rank: Ace!" In swift 3, you can use this
var enumValue = Customer.Physics var str = String(describing: enumValue) It couldn't get simpler than this in Swift 2 and the latest Xcode 7 (no need to specify enum type, or .rawValue, descriptors etc...)
Updated for Swift 3 and Xcode 8:
enum Audience { case Public case Friends case Private } let audience: Audience = .Public // or, let audience = Audience.Public print(audience) // "Public" There are multiple ways to do this. Either you could de