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"
}
}
}
实际行动:
> let foo = Foo.Bing
foo: Foo = Bing
> println ("String for 'foo' is \(foo)"
String for 'foo' is Bing
更新 : 对于 Swift > = 2.0,将 Printable替换为 CustomStringConvertible
enum Audience: String, Printable {
case Public = "Public"
case Friends = "Friends"
case Private = "Private"
var description: String {
return self.rawValue
}
}
然后我们可以做:
let audience = Audience.Public.description // audience = "Public"
或者
println("The value of Public is \(Audience.Public)")
// Prints "The value of Public is Public"
在 Swift 2和最新的 Xcode 7(不需要指定枚举类型,或者。RawValue、描述符等等。.)
更新为 Swift 3和 Xcode 8:
enum Audience {
case Public
case Friends
case Private
}
let audience: Audience = .Public // or, let audience = Audience.Public
print(audience) // "Public"