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 ...
随机推荐
- 【JavaScript游戏开发】使用HTML5+Canvas+JavaScript 封装的一个超级马里奥游戏(包含源码)
这个游戏基本上是建立在JavaScript模块化的开发基础上进行封装的,对游戏里面需要使用到的游戏场景进行了封装,分别实现了Game,Sprite,enemy,player, base,Animati ...
- UVA133 - The Dole Queue【紫书例题4.3】
题意: n个人围成个圆,从1到n,一个人从1数到k就让第k个人离场,了另一个人从n开始数,数到m就让第m个人下去,直到剩下最后一个人,并依次输出离场人的序号. 水题,直接上标程了 #include&l ...
- 0719show engine innodb status解读
转自 http://www.cnblogs.com/zengkefu/p/5678100.html 注:以下内容为根据<高性能mysql第三版>和<mysql技术内幕innodb存储 ...
- 使用厂商MIB库查找设备OID值并实施监控的方法
https://wenku.baidu.com/view/8f4b389e0029bd64783e2cd0.html
- Sublime Text 3常用插件—Emmet
原文链接:http://www.cnblogs.com/easy-blue/p/6617852.html 摘要: 安装请看上一篇Sublime Text—安装,和sublime自带快捷键一起用,写ht ...
- HDU 5046
同样是二分+DLX即可. #include <iostream> #include <cstdio> #include <cstring> #include < ...
- CSDN日报20170416 ——《为什么程序猿话少钱多死得早?》
[程序人生]为什么程序猿话少钱多死得早? 作者:文奇 我在想,程序猿都是话少吗?不一定吧.像我和我的同学.都是话非常多啊. 可是经历过非常多事的如今.再想想,发现事实的确如此.程序猿确实话少. 我是一 ...
- HDU 3579 Hello Kiki 中国剩余定理(合并方程
题意: 给定方程 res % 14 = 5 res % 57 = 56 求res 中国剩余定理裸题 #include<stdio.h> #include<string.h> # ...
- HDU 3104 Combination Lock(数学题)
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=3104 Problem Description A combination lock consists ...
- 通过UrlRewriter配置MVC4伪静态
有些项目须要设置静态.这样能够被站点收录了,提高站点的排名.内容. 假设地址后面有www.a.com/xx.html?id=1是不行,还是不能达到一些需求.怎么才干实现www.a.com/1/xx.h ...