Associated Values & enum
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:
enum Barcode {case upc(Int, Int, Int, Int)case qrCode(String)}
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 (Int, Int, Int, Int), 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:
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:
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:
switch productBarcode {case .upc(let numberSystem, let manufacturer, let product, let check):print("UPC: \(numberSystem), \(manufacturer), \(product), \(check).")case .qrCode(let productCode):print("QR code: \(productCode).")}// 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:
switch productBarcode {case let .upc(numberSystem, manufacturer, product, check):print("UPC : \(numberSystem), \(manufacturer), \(product), \(check).")case let .qrCode(productCode):print("QR code: \(productCode).")}// Prints "QR code: ABCDEFGHIJKLMNOP."
Associated Values & enum的更多相关文章
- 用枚举enum替代int常量
枚举的好处: 1. 类型安全性 2.使用方便性 public class EnumDemo { enum Color{ RED(3),BLUE(5),BLACK(8),YELLOW(13),GREEN ...
- 枚举类型与位域枚举Enum
一.概述 定义一个值类型,其中包含固定值集合.枚举类型变量可以是此集合中的任意一个或多个值.枚举使用enum关键字来声明,与类同级.枚举本身可以有修饰符,但枚举的成员始终是公共的,不能有访问修饰符.枚 ...
- [小问题笔记(四)] Enum枚举类型转换为DataTable( C# )
枚举: public enum ProductType { 小产品=, 大产品, 超大产品 } 转换方法: /// <summary> /// 枚举类型转化为DataTable /// & ...
- c# enum 解析
解析定义的枚举 public enum OrderPaymentStatus { /// <summary> /// 未支付 /// </summary> [Descripti ...
- C#的扩展方法解析
在使用面向对象的语言进行项目开发的过程中,较多的会使用到“继承”的特性,但是并非所有的场景都适合使用“继承”特性,在设计模式的一些基本原则中也有较多的提到. 继承的有关特性的使用所带来的问题:对象的继 ...
- Blender 之 Splash 代码分析
注:以下内容基于 Blender 2.7x 版本工程,其它低版本可能有改动. Blender启动完成时,会出现一个画面,英文叫Splash.默认是打开的,可以在设置里关闭.在文件菜单里点击用户首选项( ...
- [译]MVC网站教程(三):动态布局和站点管理
目录 1. 介绍 2. 软件环境 3. 在运行示例代码之前(源代码 + 示例登陆帐号) 4. 自定义操作结果和控制器扩展 1) OpenFileResult 2) ImageR ...
- c# 枚举
命名空间: System程序集: mscorlib(mscorlib.dll 中) 定义一个枚举类型 public enum Week { [Description("星期一" ...
- Java Script 编码规范【转】
Java Script 编码规范 以下文档大多来自: Google JavaScript 编码规范指南 Idiomatic 风格 参考规范 ECMAScript 5.1 注解版 EcmaScript ...
随机推荐
- 多叉树结构的数据,parent表示法转成children表示法
最近碰到的问题,有个数组,数组元素是对象,该对象的结构就如树的parent表示法的节点一样.形象点讲就是该数组存放了树的所有“叶子节点”,并且叶子节点内存有父节点,一直到根节点为止,就如存了一条从叶子 ...
- asp.net--ado.net5大对象代码示例
连接数据库 string conn_string ="Data Source=localhost;Initial Catalog=SQLtest;Integrated Security=Tr ...
- NEFU116 GCD
水 #include <iostream> #include <cstdio> #include <algorithm> using namespace std; ...
- 用css3和canvas实现的蜂窝动画效果
近期工作时研究了一下css3动画和js动画.主要是工作中为了增强页面的趣味性,大家都有意无意的加入了非常多动画效果.当然大部分都是css3动画效果.能够gpu加速,这会降低移动端的性能需求. 今天主要 ...
- C++研究之在开发中你可能没有考虑到的两个性能优化
1:多余的存储引用导致性能减少. 2:利用局部性提高程序性能: 先来说说引用是怎么减少程序性能.个人觉得减少程序性能主要有两个原因,一是数据结构选择不合理,二是多层嵌套循环导致部分代码被多余反复 ...
- 第14章3节《MonkeyRunner源代码剖析》 HierarchyViewer实现原理-HierarchyViewer实例化
既然要使用HierarchyViewer来获取控件信息,那么首先我们看下在脚本中.我们是怎么获得HierarchyViewer的,看以下一段脚本代码: 1 device = MonkeyRunner. ...
- ArcGIS Python实现Modis NDVI批量化月最大合成
最大合成法(MVC)能够在Envi中的Band Math中进行,式子是B1>B2,可是无法批量化.本文实如今ArcGIS中利用Python代码批量进行,例如以下: 用到的Modis NDVI数据 ...
- Codeforces Round #349 (Div. 2) D. World Tour 暴力最短路
D. World Tour A famous sculptor Cicasso goes to a world tour! Well, it is not actually a world-wid ...
- graphviz.js划线操作
digraph A{ graph[color=red bgcolor="cadetblue" label="海阔天空",fontname="FangS ...
- 【POJ 3744】 Scout YYF I
[题目链接] http://poj.org/problem?id=3744 [算法] 概率DP + 矩阵乘法 [代码] #include <algorithm> #include < ...