如何在Swift中获取枚举值的名称? [英] How to get the name of enumeration value in Swift?
问题描述
如果我使用原始的 Integer
值进行枚举:
If I have an enumeration with raw Integer
values:
enum City: Int {
case Melbourne = 1, Chelyabinsk, Bursa
}
let city = City.Melbourne
如何将 city
值转换为字符串 Melbourne
?这种语言类型的自省可以使用吗?
How can I convert a city
value to a string Melbourne
? Is this kind of a type name introspection available in the language?
类似的东西(此代码将不起作用):
Something like (this code will not work):
println("Your city is \(city.magicFunction)")
> Your city is Melbourne
推荐答案
从Xcode 7 beta 5(Swift版本2)开始,您现在可以使用 print(_ :)默认打印类型名称和枚举大小写
,或使用 String
的 init(_:)
初始值设定项或字符串插值语法转换为 String
.因此,对于您的示例:
As of Xcode 7 beta 5 (Swift version 2) you can now print type names and enum cases by default using print(_:)
, or convert to String
using String
's init(_:)
initializer or string interpolation syntax. So for your example:
enum City: Int {
case Melbourne = 1, Chelyabinsk, Bursa
}
let city = City.Melbourne
print(city)
// prints "Melbourne"
let cityName = "\(city)" // or `let cityName = String(city)`
// cityName contains "Melbourne"
因此不再需要定义&维护一个便利函数,该函数在每种情况下均会打开以返回字符串文字.此外,即使未指定原始值类型,此方法也可以自动用于任何枚举.
So there is no longer a need to define & maintain a convenience function that switches on each case to return a string literal. In addition, this works automatically for any enum, even if no raw-value type is specified.
debugPrint(_:)
& String(reflecting:)
可以用作标准名称:
debugPrint(_:)
& String(reflecting:)
can be used for a fully-qualified name:
debugPrint(city)
// prints "App.City.Melbourne" (or similar, depending on the full scope)
let cityDebugName = String(reflecting: city)
// cityDebugName contains "App.City.Melbourne"
请注意,您可以自定义以下每种情况下的打印内容:
Note that you can customise what is printed in each of these scenarios:
extension City: CustomStringConvertible {
var description: String {
return "City \(rawValue)"
}
}
print(city)
// prints "City 1"
extension City: CustomDebugStringConvertible {
var debugDescription: String {
return "City (rawValue: \(rawValue))"
}
}
debugPrint(city)
// prints "City (rawValue: 1)"
(我还没有找到一种方法来调用此默认"值,例如,在不求助于switch语句的情况下,打印城市是墨尔本".使用 \(self)
在 description
/ debugDescription
的实现中会导致无限递归.)
(I haven't found a way to call into this "default" value, for example, to print "The city is Melbourne" without resorting back to a switch statement. Using \(self)
in the implementation of description
/debugDescription
causes an infinite recursion.)
String
的 init(_:)
&上方的注释 init(reflecting:)
初始值设定项准确描述要打印的内容,具体取决于所反映的类型符合以下条件:
The comments above String
's init(_:)
& init(reflecting:)
initializers describe exactly what is printed, depending on what the reflected type conforms to:
extension String {
/// Initialize `self` with the textual representation of `instance`.
///
/// * If `T` conforms to `Streamable`, the result is obtained by
/// calling `instance.writeTo(s)` on an empty string s.
/// * Otherwise, if `T` conforms to `CustomStringConvertible`, the
/// result is `instance`'s `description`
/// * Otherwise, if `T` conforms to `CustomDebugStringConvertible`,
/// the result is `instance`'s `debugDescription`
/// * Otherwise, an unspecified result is supplied automatically by
/// the Swift standard library.
///
/// - SeeAlso: `String.init<T>(reflecting: T)`
public init<T>(_ instance: T)
/// Initialize `self` with a detailed textual representation of
/// `subject`, suitable for debugging.
///
/// * If `T` conforms to `CustomDebugStringConvertible`, the result
/// is `subject`'s `debugDescription`.
///
/// * Otherwise, if `T` conforms to `CustomStringConvertible`, the result
/// is `subject`'s `description`.
///
/// * Otherwise, if `T` conforms to `Streamable`, the result is
/// obtained by calling `subject.writeTo(s)` on an empty string s.
///
/// * Otherwise, an unspecified result is supplied automatically by
/// the Swift standard library.
///
/// - SeeAlso: `String.init<T>(T)`
public init<T>(reflecting subject: T)
}
请参见发行说明以获取有关此更改的信息.
See the release notes for info about this change.
这篇关于如何在Swift中获取枚举值的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!