Swift: Convert enum value to String?

匿名 (未验证) 提交于 2019-12-03 01:52:01

问题:

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 

回答1:

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" 

Source.



回答2:

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



回答3:

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" 


回答4:

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" 


回答5:

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!" 


回答6:

In swift 3, you can use this

var enumValue = Customer.Physics var str = String(describing: enumValue) 

from Swift how to use enum to get string value



回答7:

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" 


回答8:

There are multiple ways to do this. Either you could de

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!