如果我有一个原始整型值的枚举:
enum City: Int {
case Melbourne = 1, Chelyabinsk, Bursa
}
let city = City.Melbourne
如何将城市值转换为字符串墨尔本?这种类型名称内省在语言中可用吗?
类似于(这段代码将不起作用):
println("Your city is \(city.magicFunction)")
> Your city is Melbourne
如果我有一个原始整型值的枚举:
enum City: Int {
case Melbourne = 1, Chelyabinsk, Bursa
}
let city = City.Melbourne
如何将城市值转换为字符串墨尔本?这种类型名称内省在语言中可用吗?
类似于(这段代码将不起作用):
println("Your city is \(city.magicFunction)")
> Your city is Melbourne
当前回答
在Swift 2.2中对枚举的String(…)(CustomStringConvertible)支持的基础上,对它们也有一些破碎的反射支持。对于有关联值的枚举案例,可以使用反射获得枚举案例的标签:
enum City {
case Melbourne(String)
case Chelyabinsk
case Bursa
var label:String? {
let mirror = Mirror(reflecting: self)
return mirror.children.first?.label
}
}
print(City.Melbourne("Foobar").label) // prints out "Melbourne"
然而,我的意思是,对于“简单”枚举,上面基于反射的标签计算属性只返回nil (boo-hoo)。
print(City.Chelyabinsk.label) // prints out nil
显然,在《Swift 3》之后,反思的情况应该会有所好转。现在的解决方案是String(…),正如在其他答案之一中建议的那样:
print(String(City.Chelyabinsk)) // prints out Cheylabinsk
其他回答
简单但有效…
enum ViewType : Int {
case Title
case Buttons
case View
}
func printEnumValue(enum: ViewType) {
switch enum {
case .Title: println("ViewType.Title")
case .Buttons: println("ViewType.Buttons")
case .View: println("ViewType.View")
}
}
这太令人失望了。
当你需要这些名字的时候(编译器完全知道这些名字的准确拼写,但拒绝访问)——谢谢Swift团队!!——)但不想或不能让String作为枚举的基,一个冗长、麻烦的替代方法如下:
enum ViewType : Int, Printable {
case Title
case Buttons
case View
static let all = [Title, Buttons, View]
static let strings = ["Title", "Buttons", "View"]
func string() -> String {
return ViewType.strings[self.rawValue]
}
var description:String {
get {
return string()
}
}
}
您可以使用上述如下:
let elementType = ViewType.Title
let column = Column.Collections
let row = 0
println("fetching element \(elementType), column: \(column.string()), row: \(row)")
您将得到预期的结果(列的代码类似,但没有显示)
fetching element Title, column: Collections, row: 0
在上面,我已经使description属性引用回string方法,但这是一个品味问题。还要注意,所谓的静态变量需要通过其封闭类型的名称来限定作用域,因为编译器太健忘了,无法自行召回上下文…
斯威夫特团队必须得到真正的指挥。他们创建了枚举,你不能枚举,你可以使用枚举是“序列”,但不是enum!
在Swift-3(用Xcode 8.1测试)中,你可以在你的枚举中添加以下方法:
/**
* The name of the enumeration (as written in case).
*/
var name: String {
get { return String(describing: self) }
}
/**
* The full name of the enumeration
* (the name of the enum plus dot plus the name as written in case).
*/
var description: String {
get { return String(reflecting: self) }
}
然后,您可以将其用作枚举实例上的常规方法调用。 它可能也适用于以前的Swift版本,但我还没有测试过。
在你的例子中:
enum City: Int {
case Melbourne = 1, Chelyabinsk, Bursa
var name: String {
get { return String(describing: self) }
}
var description: String {
get { return String(reflecting: self) }
}
}
let city = City.Melbourne
print(city.name)
// prints "Melbourne"
print(city.description)
// prints "City.Melbourne"
如果你想为你所有的枚举提供这个功能,你可以让它成为一个扩展:
/**
* Extend all enums with a simple method to derive their names.
*/
extension RawRepresentable where RawValue: Any {
/**
* The name of the enumeration (as written in case).
*/
var name: String {
get { return String(describing: self) }
}
/**
* The full name of the enumeration
* (the name of the enum plus dot plus the name as written in case).
*/
var description: String {
get { return String(reflecting: self) }
}
}
这只适用于Swift枚举。
对于Objective-C枚举来说,目前唯一的方法似乎是,例如,用CustomStringConvertible扩展枚举,最终得到如下结果:
extension UIDeviceBatteryState: CustomStringConvertible {
public var description: String {
switch self {
case .Unknown:
return "Unknown"
case .Unplugged:
return "Unplugged"
case .Charging:
return "Charging"
case .Full:
return "Full"
}
}
}
然后将枚举类型转换为String:
String(UIDevice.currentDevice().batteryState)
String(description:)初始化式可以用来返回case标签名称,即使是对于非String rawValues的枚举:
enum Numbers: Int {
case one = 1
case two = 2
}
let one = String(describing: Numbers.one) // "one"
let two = String(describing: Numbers.two) // "two"
注意,如果枚举使用了@objc修饰符,这将不起作用:
在Swift中获取Objective-C @objc enum值的字符串名称? 为什么Enum返回“EnumName”而不是“caseLabel”的字符串(描述:)?
为Objective-C类型生成的Swift接口有时不包括@objc修饰符。这些枚举是在Objective-C中定义的,因此不像上面那样工作。