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 
6

17 Answers

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

Note: Using CustomStringConvertible allows Foo to adopt a different raw type. For example enum Foo : Int, CustomStringConvertible { ... } is possible. This freedom can be useful.

8

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.

7

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

4

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

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

A swift 3 and above example if using Ints in Enum

public enum ECategory : Int{ case Attraction=0, FP, Food, Restroom, Popcorn, Shop, Service, None; var description: String { return String(describing: self) } } let category = ECategory.Attraction let categoryName = category.description //string Attraction 
2

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

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

For anyone reading the example in "A Swift Tour" chapter of "The Swift Programming Language" and looking for a way to simplify the simpleDescription() method, converting the enum itself to String by doing String(self) will do it:

enum Rank: Int { case Ace = 1 //required otherwise Ace will be 0 case Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten case Jack, Queen, King func simpleDescription() -> String { switch self { case .Ace, .Jack, .Queen, .King: return String(self).lowercaseString default: return String(self.rawValue) } } } 
1

After try few different ways, i found that if you don't want to use:

let audience = Audience.Public.toRaw() 

You can still archive it using a struct

struct Audience { static let Public = "Public" static let Friends = "Friends" static let Private = "Private" } 

then your code:

let audience = Audience.Public 

will work as expected. It isn't pretty and there are some downsides because you not using a "enum", you can't use the shortcut only adding .Private neither will work with switch cases.

3

There are multiple ways to do this. Either you could define a function in the enum which returns the string based on the value of enum type:

enum Audience{ ... func toString()->String{ var a:String switch self{ case .Public: a="Public" case .Friends: a="Friends" ... } return a } 

Or you could can try this:

enum Audience:String{ case Public="Public" case Friends="Friends" case Private="Private" } 

And to use it:

var a:Audience=Audience.Public println(a.toRaw()) 

Starting from Swift 3.0 you can

var str = String(describing: Audience.friends) 
1

Use Ruby way

var public: String = "\(Audience.Public)" 

One more way

public enum HTTP{ case get case put case delete case patch var value: String? { return String(describing: self) } 

Friendly by guides if you need to use static strings as enum values:

class EncyclopediaOfCats { struct Constants { static var playHideAndSeek: String { "Play hide-and-seek" } static var eat: String { "Eats" } static var sleep: String { "Sleep" } static var beCute: String { "Be cute" } } } enum CatLife { case playHideAndSeek case eat case sleep case beCute typealias RawValue = String var rawValue: String { switch self { case .playHideAndSeek: return EncyclopediaOfCats.Constants.playHideAndSeek case .eat: return EncyclopediaOfCats.Constants.eat case .sleep: return EncyclopediaOfCats.Constants.sleep case .beCute: return EncyclopediaOfCats.Constants.beCute } } init?(rawValue: CatLife.RawValue) { switch rawValue { case EncyclopediaOfCats.Constants.playHideAndSeek: self = .playHideAndSeek case EncyclopediaOfCats.Constants.eat: self = .eat case EncyclopediaOfCats.Constants.sleep: self = .sleep case EncyclopediaOfCats.Constants.beCute: self = .beCute default: self = .playHideAndSeek } } } 
1

You can also use "\(enumVal)" Here is an example :

enum WeekDays{ case Sat, Sun, Mon, Tue, Wed, The, Fri } let dayOfWeek: String = "\(WeekDays.Mon)" 

Tried and tested in Swift 5

I agree with all the above answers but in your enum private and the public cases can't be defined since those are default keywords. I'm including CaseIterable in my answer, it may help you to get all cases if you required to loop over.

enum Audience: String, CaseIterable { case publicAudience case friends case privateAudience var description: String { switch self { case .publicAudience: return "Public" case .friends: return "Friends" case .privateAudience: return "Private" } } static var allAudience: [String] { return Audience { $0.rawValue } } 

}

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy