如果我有一个原始值为Integer的枚举:

enum City: Int {
  case Melbourne = 1, Chelyabinsk, Bursa
}

let city = City.Melbourne

如何将city值转换为字符串Melbourne?这种类型名内省在语言中可用吗?

类似(此代码不起作用):

println("Your city is \(city.magicFunction)")
> Your city is Melbourne

推荐答案

从Xcode 7 beta 5(Swift版本2)开始,您现在可以默认使用print(_:)打印类型名称和枚举大小写,或者使用Stringinit(_:)初始值设定项或字符串插值语法转换为String.以你为例:

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"

因此,不再需要定义&;维护一个方便的函数,打开每个大小写以返回字符串文本.此外,这会自动适用于任何枚举,即使未指定原始值类型.

debugPrint(_:)&;String(reflecting:)可用于完全限定名称:

debugPrint(city)
// prints "App.City.Melbourne" (or similar, depending on the full scope)

let cityDebugName = String(reflecting: city)
// cityDebugName contains "App.City.Melbourne"

请注意,您可以自定义这些场景中打印的内容:

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

(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 100 in the implementation of 101/102 causes an infinite recursion.)


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)
}


Swift相关问答推荐

如何在 Swift 中返回 Task 中定义的变量

如何从 LLDB 调用带有断点的 Swift 函数?

在视图中设置所有变量

如何自己快速编写 Hashable 协议

Xcode 13.3 将函数调用更改为属性访问

如何防止 UITableViewCell 移动(Swift 5)

'requestReview()' 在 iOS 14.0 中已弃用

xcrun:错误:无法找到实用程序“xctest”,不是开发人员工具或 PATH

SwiftUI - 呈现工作表后导航栏按钮不可点击

Xcode 在上传时卡在“将包上传到 App Store”阶段

Swift 5.1 编译器无法导入使用 Swift 5.0.1 编译的模块

SwiftUI 中的比例高度(或宽度)

切换 swiftUI toggle() 时如何触发操作?

在 SwiftUI 中,如何在 UIView 内或作为 UIView 使用 UIHostingController?

SwiftUI 使用 ForEach 遍历字典

在 SwiftUI 中显示行/分隔符视图

在 SwiftUI 中水平居中视图

在现有的 UIKit 应用程序中包含 SwiftUI 视图

用于字符串资源格式的自定义 Swift 编码器/解码器

支持的 Swift 字符串格式说明符有哪些?