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”
先上代码:
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”
相关文章推荐
- 445利用方法
- easyui-windows组件
- 华为,为什么让全世界都感到害怕?
- 击中击不中变换的作用理解
- log4j详解与实战
- 【高级数据结构】[SPOJ QTREE]树链剖分/动态树各一模板
- 项目受源代码管理。向源代码管理注册此项目时出错。建议不要对此项目进行任何更改
- TensorFlow实战— —K-Means聚类
- Apache安装与配置
- Android ListView中嵌入其实组件后如checkbox,button后ItemClick事件失效的问题
- 欢迎使用CSDN-markdown编辑器
- android-Media Playback
- 充电五分钟通话两小时 VOOC闪充助OPPO
- CSRF与Cookie
- MPAndroidChart的K线图上添加均线
- MPAndroidChart的K线图上添加均线
- Android-Media and Camera
- 什么是P问题,NP问题和NPC问题?
- 可扩展机器学习——Spark分布式处理
- oracle和tomcat的开机自启动