之前记录了Swift Enumeration(1),这篇算是它的延续吧,继续说下Enumeration,看以下定义

enum TrainStatus {

case OnTime

case Delay(Int) //带关联值

}

var myTrainStatus = TrainStatus.Delay(10)

用Switch 匹配对象

switch myTrainStatus{

case .OnTime:

println("Train is ontime")

case .Delay(let minutes):

println("Train delays \(minutes) minutes")

}

匹配方式是有先后顺序的,上面中先匹配到Delay然后再给里面的minutes赋值,好下面我将演示区间匹配。

switch myTrainStatus{

case .OnTime://匹配准时

println("Train is ontime")

case .Delay(1...5)://匹配1到5

println("Train delays less than five minutes")

case .Delay(6...10)://匹配6到10

println("Train delays between six and ten minutes")

case .Delay(_): //匹配其它,里面是下划线

println("Train delays more than ten minutes")

}

关联值的另一个例子,

enum Barcode{
case UPCA(Int,Int,Int,Int)
case QRCode(String)
} var productCodeA:Barcode = Barcode.UPCA(10, 20, 30, 40)
var productCodeB:Barcode = Barcode.QRCode("This is an infomation") switch productCodeB{
case .UPCA(let a,let b,let c,let d):
print("\(a)-\(b)-\(c)-\(d)")
case .QRCode(let code):
print("\(code)")
}

SWIFT Enumeration(2)的更多相关文章

  1. SWIFT Enumeration(1)

    Swift中定义Enumeration跟其它语言挺类似的,看如下定义一个星期的Enumeration enum Day:Int{ case Monday = 1, Tuesday,Wednesday, ...

  2. 【Swift学习笔记00】——enumeration枚举类型遵循协议protocol

    Apple官方文档:The Swift Programming LanguageProtocols and Extensions一节的小节练习,要求自行定义一个enumeration枚举类型,并且遵循 ...

  3. Swift—下标脚本(Subscripts)

    下标脚本可以定义在类(Class).结构体(Struct).枚举(enumeration)这些目标中,可以认为是访问集合,列表或序列的快捷方式,使用下标脚本的索引设置和获取值,不需要再调用实例的特定的 ...

  4. 【Swift学习】Swift编程之旅---枚举(十二)

    枚举为一组相关的值定义一个共同的类型,并允许您在代码中的以类型安全的方式中使用这些值,在 Swift 中,枚举类型是一等(first-class)类型.它们采用了很多传统上只被类所支持的特征,例如计算 ...

  5. 使用swift 中的注意,不断完善中

    1. 应该充分利用swfit的新特性 比如如果按照oc里的习惯,调用一个delegate中都optional函数应该这样写 if delegate != nil && delegate ...

  6. Swift声明参考

    一条声明可以在你的程序里引入新的名字和构造.举例来说,你可以使用声明来引入函数和方法,变量和常量,或者来定义 新的命名好的枚举,结构,类和协议类型.你也可以使用一条声明来延长一个已经存在的命名好的类型 ...

  7. 集合类(Objective-C & Swift)

    内容提要: 本文前两部分讲了Cocoa的集合类和Swift的集合类,其中Cocoa提供的集合类包括NSArray.NSMutableArray.NSDictionary.NSMutableDictio ...

  8. iOS - Swift Swift 语言新特性

    1.Swift 2.0 带来哪些新变化 常规变化: 1.OS X 10.11.iOS 9 和 watchOS 2 SDK 采纳了一些 Objective-C 的特性用来提高 Swift 的编程体验, ...

  9. iOS - Swift Subscript 下标脚本

    1.Subscript 下标脚本允许你通过在实例后面的方括号中传入一个或者多个的索引值来对实例进行访问和赋值.语法类似于实例方法和计算型属性的混合.与定义实例方法类似,定义下标脚本使用 subscri ...

随机推荐

  1. python ros 重新设置机器人的位置

    #!/usr/bin/env python import rospy import math from tf import transformations from geometry_msgs.msg ...

  2. python 集合元素添加

    #A new empty set color_set = set() color_set.add("Red") print(color_set) #Add multiple ite ...

  3. Ubuntu 16.04 kinetic 编译指定包

    编译指定包 catkin_make -DCATKIN_WHITELIST_PACKAGES=baoming 使用上述命令后catkin_make会一直编译上面那个包,想要编译全部包,使用 catkin ...

  4. java 使用正则判断是不是一个数字

    public class Numeric { public static void main(String[] args) { String string = "-1234.15" ...

  5. bash Shell 中如何实现条件判断之if判断

    http://blog.51cto.com/lovelace/1211353 bash中如何实现条件判断?条件测试类型:    整数测试    字符测试    文件测试 一.条件测试的表达式:     ...

  6. [原][库][c++]tinyxml使用小结

    参考:http://blog.csdn.net/L_Andy/article/details/40615517 tinyxml官网: http://www.grinninglizard.com/tin ...

  7. Linux环境下 RabbitMQ 的下载与安装

    0 环境 CentOS7 RabbitMQ 3.6.5 erlang 18.3 socat rabbitmq是使用erlang语言编写的,所以需要先安装erlang,其次rabbitmq安装依赖于so ...

  8. 《剑指offer》第一题(重载赋值运算符)

    //重载赋值运算符 #include <iostream> #include <cstring> using namespace std; class CMystring { ...

  9. OpenGL入门程序二:绘制简单的圆

    学习 绘制一个圆: ; const float Pi = 3.1415926536f; const float R = 0.5f; //绘制一个圆 void DrawCircle() { //绘制一个 ...

  10. 3-19(晚) require_relative 和 require. === operator的解释。

    kernel#require_relative Ruby tries to load the library named string relative to the requiring file's ...