我有以下Swift枚举:

enum ScreenName: String {
    case start = "Start Screen"
    case dashboard = "My Dashboard Screen"
}

这使我可以拥有一组类型化的常量,并像下面这样使用它们:
func trackView(screen: ScreenName) {
     print("viewed \(screen.rawValue)")
}

trackView(screen: .start) // -> "viewed Start Screen"

在Kotlin中这相当于什么?

最佳答案

就像是:

enum class ScreenName(val displayName : String) {
  START("Start Screen"),
  DASHBOARD("My Dashboard Screen")
}

fun trackView(screenName : ScreenName) {
  print("viewed ${screenName.displayName}")
}

关于swift - Kotlin相当于Swift字符串枚举(字符串常量的类型化集合),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51526201/

10-09 20:15