小编xia*_*ian的帖子

Kotlin:当枚举名称明确时省略枚举名称

使用Swift 枚举,您可以省略名称enum,在只能使用该类型的值的情况下,。

所以当给出枚举(Swift/Kotlin)

enum (class) CompassPoint {
  case north
  case south
  case east
  case west
}
Run Code Online (Sandbox Code Playgroud)

Swift 在创建新变量时只需要枚举名称:

// type unclear, enum name needed
var directionToHead = CompassPoint.west

// type clear, enum name can be dropped
directionToHead = .east

// type clear, enum name can be dropped
switch directionToHead {
case .north:
  print("Lots of planets have a north")
case .south:
  print("Watch out for penguins")
case .east:
  print("Where the sun rises")
case .west:
  print("Where the skies are …
Run Code Online (Sandbox Code Playgroud)

enums kotlin kotlin-when

2
推荐指数
1
解决办法
284
查看次数

标签 统计

enums ×1

kotlin ×1

kotlin-when ×1