1.枚舉定義了一個通用類型的一組相關值,使你能夠在你的代碼中以一種安全的方式來使用這些值安全
2.語法 spa
enum 名稱 {orm
case 常量字符串
case 常量it
....io
}或者寫在一行,用逗號隔開enum 名稱 { case 常量1,常量2,常量3 ...}ast
enum Direction{語法
case eastmargin
case westtop
case south
case north
}
print(Direction.east)
enum e {
case up,down,left,right
}
print(e.down)
3.enum和switch聯合使用
enum Direction { case east,west,north,south }
var d = Direction.south
switch d{
case Direction.east:
print("east")
case Direction.west:
print("west")
case Direction.north:
print("north")
case Direction.south:
print("south")
}
4.關聯值
枚舉裏面能夠是整型,字符串,元組類型
enum state{
case status(Int,String)
}
var s = state.status(404, "page not found")
5.原始值
enum numbers: Int{ case a,b,c}
print(numbers.b.rawValue)//默認以0開始
enum Direction: String{
case east,west,north,south
}
print(Direction.east.rawValue)//默認是他自己