Given the following enum:
鉴于以下枚举:
enum Audience {
case Public
case Friends
case Private
}
How do I get the string "Public"
from the audience
constant below?
如何从下面的观众常数中获得字符串“Public”?
let audience = Audience.Public
11 个解决方案
#1
89
Not sure in which Swift version this feature was added, but right now (Swift 2.1) you only need this code:
不确定在哪个Swift版本中添加了这个特性,但是现在(Swift 2.1)您只需要这段代码:
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.
当字符串用于原始值时,每个case的隐式值都是该case名称的文本。
[...]
[…]
enum CompassPoint: String { case North, South, East, West }
In the example above, CompassPoint.South has an implicit raw value of "South", and so on.
在上面的示例中,是围绕spoint。南方有一个隐含的“南方”的原始价值,等等。
You access the raw value of an enumeration case with its rawValue property:
使用其rawValue属性访问枚举案例的原始值:
let sunsetDirection = CompassPoint.West.rawValue // sunsetDirection is "West"
源。
#2
144
The idiomatic interface for 'getting a String' is to use the CustomStringConvertible
interface and access the description
getter. Define your enum
as:
用于“获取字符串”的惯用接口是使用customconvertible接口并访问description getter。你的枚举定义为:
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
更新:对于Swift >= 2.0,用可定制的stringconvertible替换可打印
#3
28
For now, I'll redefine the enum as:
现在,我将把enum重新定义为:
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:
但是,新的enum定义不是多余的吗?我可以保留初始的enum定义吗?
audience.toString() // "Public"
#4
20
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
14
In swift 3, you can use this
在swift 3中,您可以使用这个
var enumValue = Customer.Physics
var str = String(describing: enumValue)
from Swift how to use enum to get string value
从如何使用enum获取字符串值
#6
12
Updated for the release of Xcode 7 GM. It works as one would hope now--thanks Apple!
为Xcode 7 GM的发布而更新。它现在可以像人们希望的那样工作了——谢谢苹果!
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!"
#7
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...)
在Swift 2和最新的Xcode 7中(不需要指定enum类型,或.rawValue、descriptors等等),这一点再简单不过了。
Updated for Swift 3 and Xcode 8:
Swift 3和Xcode 8更新:
enum Audience {
case Public
case Friends
case Private
}
let audience: Audience = .Public // or, let audience = Audience.Public
print(audience) // "Public"
#8
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中定义一个函数,该函数根据enum类型的值返回字符串:
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())
#9
3
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:
对于任何在“Swift编程语言”章节中阅读示例的人,并寻找简化simpleDescription()方法的方法,将枚举本身转换为字符串(self)将会这样做:
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)
}
}
}
#10
2
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对其进行存档
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.
将按预期工作。它并不漂亮,而且也有一些缺点,因为您没有使用“enum”,您不能使用仅添加. private的快捷方式,也不能使用switch case。
#11
2
A swift 3 and above example if using Ints in Enum
如果在Enum中使用Ints,可以使用swift 3和以上示例
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
#1
89
Not sure in which Swift version this feature was added, but right now (Swift 2.1) you only need this code:
不确定在哪个Swift版本中添加了这个特性,但是现在(Swift 2.1)您只需要这段代码:
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.
当字符串用于原始值时,每个case的隐式值都是该case名称的文本。
[...]
[…]
enum CompassPoint: String { case North, South, East, West }
In the example above, CompassPoint.South has an implicit raw value of "South", and so on.
在上面的示例中,是围绕spoint。南方有一个隐含的“南方”的原始价值,等等。
You access the raw value of an enumeration case with its rawValue property:
使用其rawValue属性访问枚举案例的原始值:
let sunsetDirection = CompassPoint.West.rawValue // sunsetDirection is "West"
源。
#2
144
The idiomatic interface for 'getting a String' is to use the CustomStringConvertible
interface and access the description
getter. Define your enum
as:
用于“获取字符串”的惯用接口是使用customconvertible接口并访问description getter。你的枚举定义为:
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
更新:对于Swift >= 2.0,用可定制的stringconvertible替换可打印
#3
28
For now, I'll redefine the enum as:
现在,我将把enum重新定义为:
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:
但是,新的enum定义不是多余的吗?我可以保留初始的enum定义吗?
audience.toString() // "Public"
#4
20
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
14
In swift 3, you can use this
在swift 3中,您可以使用这个
var enumValue = Customer.Physics
var str = String(describing: enumValue)
from Swift how to use enum to get string value
从如何使用enum获取字符串值
#6
12
Updated for the release of Xcode 7 GM. It works as one would hope now--thanks Apple!
为Xcode 7 GM的发布而更新。它现在可以像人们希望的那样工作了——谢谢苹果!
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!"
#7
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...)
在Swift 2和最新的Xcode 7中(不需要指定enum类型,或.rawValue、descriptors等等),这一点再简单不过了。
Updated for Swift 3 and Xcode 8:
Swift 3和Xcode 8更新:
enum Audience {
case Public
case Friends
case Private
}
let audience: Audience = .Public // or, let audience = Audience.Public
print(audience) // "Public"
#8
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中定义一个函数,该函数根据enum类型的值返回字符串:
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())
#9
3
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:
对于任何在“Swift编程语言”章节中阅读示例的人,并寻找简化simpleDescription()方法的方法,将枚举本身转换为字符串(self)将会这样做:
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)
}
}
}
#10
2
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对其进行存档
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.
将按预期工作。它并不漂亮,而且也有一些缺点,因为您没有使用“enum”,您不能使用仅添加. private的快捷方式,也不能使用switch case。
#11
2
A swift 3 and above example if using Ints in Enum
如果在Enum中使用Ints,可以使用swift 3和以上示例
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