您的位置:首页 > 其它

Checklists学习日志之枚举的概念

2015-12-01 13:17 197 查看
枚举为一系相关联的值定义了一个公共的组类型.同时能够让你在编程的时候在类型安全的情况下去使用这些值。看swift教程熟悉一下枚举的概念。

先上代码:

enum CompassPoint{
case North
case South
case East
case West
}
var directionToHead = CompassPoint.West
print("\(directionToHead)")

这是枚举的定义,输出为West。
当directionToHead在初始化过程中被赋值成CompassPoint中的某一个可能的值的时候,它的类型就可以被推测出来来了。一旦directionToHead被声明成是CompassPoint类型,那么你就可以简短的使用逗号表达式来给它赋值成其他的CompassPoint当中的值了

directionToHead = .East
directionToHead = .South

枚举还可以与Switch连用:
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 blue")
}
// 输出"Watch out for penguins”
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: