swift - Swift 中枚举的反射(reflect)

标签 swift

<分区>

假设我有一个这样的枚举:

enum Direction {
  case Left
  case Right
}

有没有一种方法可以自动导出枚举的字符串描述而无需自己定义?像这样:

let direction: Direction = .Left
let description: String = direction.description // should be "Left"

我尝试了 reflect(direction)MirrorType 上的属性,但没有用。

最佳答案

目前没有。为此,您需要手动实现一个description 函数。 Swift 的反射非常有限。

关于swift - Swift 中枚举的反射(reflect),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29708463/

相关文章:

ios - 如何使用 SpriteKit 检测节点是否离开屏幕

swift - 更新字典的所有值

ios - TableView Cell 内的多个 View

ios - 覆盖 prepareForSegue 不起作用

swift - 使用 Vapor 创建和返回数据库记录

ios - 解析 json 时出现结束错误时出现 Alamofire 垃圾

swift - Google Maps iOS SDK map 不会出现

ios - 在 Double 中获得奇怪的值(value)

swift - tableview 到其他 View autoresize 问题 : swift

ios - swift 4 : Value of tuple type '(assignment: Assignment, tasks: [Task])' has no member 'subscript'