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 ...
随机推荐
- BZOJ 3514 GERALD07加强版 (LCT+主席树)
题目大意:给定n个点m条边无向图,每次询问求当图中有编号为[L,R]的边时,整个图的联通块个数,强制在线 神题!(发现好久以前的题解没有写完诶) 我们要求图中联通块的个数,似乎不可搞啊. 联通块个数= ...
- linux系统中给mysql配置环境变量
安装过程就不写了,记得安装的路径就行,接下来要用到. 修改配置文件 vim /etc/profile 设置环境变量 写一个MYSQL_HOME,值为“mysql的安装路径” 在PATH后面加上$MYS ...
- 继续聊WPF——Expander控件(1)
这个控件最实用的地方,就是做导航栏. <StackPanel Margin="20,20" Width="100" Height="460&qu ...
- 快速上手Linux 玩转典型应用_慕课网笔记
1.没有exe安装程序 2.区分大小写 3.一切皆文件 4.文件后缀不是那么重要,只是为了好识别 -------------------------------------------------- ...
- java反射,简单demo
直接上码 //获取方法.属性.构造函数时加 Declared 表示获取本类全部的,不分修饰符:不加 Declared 表示获取从父类继承的和本类公共的 //获取 First 类的无参构造函数 Cons ...
- asp怎么实现二级联动下拉菜单
rs为一级栏目的记录集 rs2为二级栏目的记录集 分别替换成你自己的记录集名称就好了 <script language="JavaScript"> var onecou ...
- Python:利用 selenium 库抓取动态网页示例
前言 在抓取常规的静态网页时,我们直接请求对应的 url 就可以获取到完整的 HTML 页面,但是对于动态页面,网页显示的内容往往是通过 ajax 动态去生成的,所以如果是用 urllib.reque ...
- 状态压缩dp poj 3254 hdu5045
近来感觉状态压缩dp的强大性(灵活利用了二进制运算非常关键). . . 于是做了俩提来看看..毕竟队友是专业的dp.我仅仅是管中窥豹下而已.. 日后有机会再与之玩耍玩耍...ps:假设上天再给我一次机 ...
- chrome 插件开发2
登录 | 注册 基础文档 综述 调试 Manifest 文件 代码例子 模式匹配 分类索引 改变浏览器外观 Browser Actions 右键菜单 桌面通知 Omnibox 选项页 覆写特定页 ...
- POJ 1442 Black Box(优先队列)
题目地址:POJ 1442 这题是用了两个优先队列,当中一个是较大优先.还有一个是较小优先. 让较大优先的队列保持k个.每次输出较大优先队列的队头. 每次取出一个数之后,都要先进行推断,假设这个数比較 ...