it is sometimes useful to be able to store associated values of other types alongside these case values. This enables you to store additional custom information along with the case value, and permits this information to vary each time you use that case in your code.

In Swift, an enumeration to define product barcodes of either type might look like this:

  1. enum Barcode {
  2. case upc(Int, Int, Int, Int)
  3. case qrCode(String)
  4. }

This can be read as:

“Define an enumeration type called Barcode, which can take either a value of upc with an associated value of type (IntIntIntInt), or a value of qrCode with an associated value of type String.”

This definition does not provide any actual Int or String values—it just defines the type of associated values that Barcode constants and variables can store when they are equal to Barcode.upc or Barcode.qrCode.

New barcodes can then be created using either type:

  1. var productBarcode = Barcode.upc(8, 85909, 51226, 3)

This example creates a new variable called productBarcode and assigns it a value of Barcode.upc with an associated tuple value of (8, 85909, 51226, 3).

The same product can be assigned a different type of barcode:

  1. productBarcode = .qrCode("ABCDEFGHIJKLMNOP")

At this point, the original Barcode.upc and its integer values are replaced by the new Barcode.qrCode and its string value. Constants and variables of type Barcode can store either a .upc or a .qrCode (together with their associated values), but they can only store one of them at any given time.

The different barcode types can be checked using a switch statement, as before. This time, however, the associated values can be extracted as part of the switch statement. You extract each associated value as a constant (with the let prefix) or a variable (with the var prefix) for use within the switch case’s body:

  1. switch productBarcode {
  2. case .upc(let numberSystem, let manufacturer, let product, let check):
  3. print("UPC: \(numberSystem), \(manufacturer), \(product), \(check).")
  4. case .qrCode(let productCode):
  5. print("QR code: \(productCode).")
  6. }
  7. // Prints "QR code: ABCDEFGHIJKLMNOP."

If all of the associated values for an enumeration case are extracted as constants, or if all are extracted as variables, you can place a single var or let annotation before the case name, for brevity:

  1. switch productBarcode {
  2. case let .upc(numberSystem, manufacturer, product, check):
  3. print("UPC : \(numberSystem), \(manufacturer), \(product), \(check).")
  4. case let .qrCode(productCode):
  5. print("QR code: \(productCode).")
  6. }
  7. // Prints "QR code: ABCDEFGHIJKLMNOP."

最新文章

  1. dubbo demo实现
  2. JAVA类访问URL
  3. WPF中使用MVVM模式进行简单的数据绑定
  4. MVC视图引擎
  5. Java基础知识强化86:BigInteger类之BigInteger概述和构造方法
  6. 左右 Java 于 finally 深度分析语句块
  7. 自己实现的sax XML解析,可能会有误
  8. win10 uwp 隐私声明
  9. Android面试题目总结
  10. clam简单使用
  11. 转换流InputStreamReader & OutputStreamWriter
  12. 【手记】VSTO部署中的坑
  13. cmd打开E盘文件
  14. jquery.validate使用详解
  15. 与servlet相关的接口
  16. Console命令,让js调试更简单
  17. c++ 栈(顺序表)
  18. 第七章移动互联网与移动IP
  19. ACM比赛辅导--授课内容
  20. Python-内置函数3

热门文章

  1. php第六讲
  2. 如何解决windows docker共享目录不支持符号链接(do not support symlinks)?
  3. [NOIP模拟赛]b
  4. 解决windows文件在linux系统中显示乱码的问题
  5. 25.partial update内置乐观锁并发控制
  6. python爬虫06 | 你的第一个爬虫,爬取当当网 Top 500 本五星好评书籍
  7. 《团队名称》第八次团队作业:Alpha冲刺
  8. oc10--练习
  9. 2017-3-8 leetcode 380 381 532
  10. Java-Spring MVC:JAVA之常用的一些Spring MVC的路由写法以及参数传递方式