Files
the-swift-programming-langu…/chapter2/08_Enumerations.html
2014-06-09 22:19:55 +08:00

781 lines
37 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE HTML>
<html lang="en-US" manifest="../manifest.appcache">
<head prefix="og: http://ogp.me/ns# book: http://ogp.me/ns/book#">
<meta charset="UTF-8">
<title>枚举 | Swift 编程语言</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="robots" content="index, follow">
<meta name="author" content="">
<meta name="description" content="Swift 是苹果在 WWDC 2014 上发布的一款全新的编程语言,本书译自苹果官方的 Swift 教程《The Swift Programming Language》。">
<meta name="keywords" content="gitbook,github" >
<meta name="generator" content="www.gitbook.io">
<link rel="next" href="../chapter2/09_Classes_and_Structures.html" />
<link rel="prev" href="../chapter2/07_Closures.html" />
<meta property="og:title" content="枚举 | Swift 编程语言">
<meta property="og:site_name" content="Swift 编程语言">
<meta property="og:type" content="book">
<meta property="og:locale" content="en_US">
<meta property="book:author" content="https://github.com/">
<meta property="book:tag" content="GitBook">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
</head>
<body>
<link rel="stylesheet" href="../gitbook/style.css">
<div class="book" data-level="2.8" data-basepath=".." data-revision="1402323563628">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
<a href="https://github.com/null" target="_blank" class="btn pull-left home-bookmark" aria-label="GitHub home"><i class="fa fa-bookmark-o"></i></a>
<a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
<span id="font-settings-wrapper">
<a href="#" class="btn pull-left toggle-font-settings" aria-label="Toggle font settings"><i class="fa fa-font"></i>
</a>
<div class="dropdown-menu font-settings">
<div class="dropdown-caret">
<span class="caret-outer"></span>
<span class="caret-inner"></span>
</div>
<div class="btn-group btn-block">
<button id="reduce-font-size" class="btn btn-default">A</button>
<button id="enlarge-font-size" class="btn btn-default">A</button>
</div>
<ul class="list-group font-family-list">
<li class="list-group-item" data-font="0">Serif</li>
<li class="list-group-item" data-font="1">Sans</li>
</ul>
<div class="btn-group btn-group-xs btn-block color-theme-list">
<button type="button" class="btn btn-default" id="color-theme-preview-0" data-theme="0">White</button>
<button type="button" class="btn btn-default" id="color-theme-preview-1" data-theme="1">Sepia</button>
<button type="button" class="btn btn-default" id="color-theme-preview-2" data-theme="2">Night</button>
</div>
</div>
</span>
<!-- Actions Right -->
<a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Share on Google Plus"><i class="fa fa-google-plus"></i></a>
<a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Share on Facebook"><i class="fa fa-facebook"></i></a>
<a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Share on Twitter"><i class="fa fa-twitter"></i></a>
<!-- Title -->
<h1>
<i class="fa fa-spinner fa-spin"></i>
<a href="../" >Swift 编程语言</a>
</h1>
</div>
<div class="book-summary">
<div class="book-search">
<input type="text" placeholder="Search" class="form-control" />
</div>
<ul class="summary">
<li data-level="0" data-path="index.html">
<a href="../"><i class="fa fa-check"></i> Introduction</a>
</li>
<li class="chapter " data-level="1" data-path="chapter1/chapter1.html">
<a href="../chapter1/chapter1.html">
<i class="fa fa-check"></i> <b>1.</b> 欢迎使用 Swift
</a>
<ul class="articles">
<li class="chapter " data-level="1.1" data-path="chapter1/01_swift.html">
<a href="../chapter1/01_swift.html">
<i class="fa fa-check"></i> <b>1.1.</b> 关于 Swift
</a>
</li>
<li class="chapter " data-level="1.2" data-path="chapter1/02_a_swift_tour.html">
<a href="../chapter1/02_a_swift_tour.html">
<i class="fa fa-check"></i> <b>1.2.</b> Swift 初见
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="2" data-path="chapter2/chapter2.html">
<a href="../chapter2/chapter2.html">
<i class="fa fa-check"></i> <b>2.</b> Swift 教程
</a>
<ul class="articles">
<li class="chapter " data-level="2.1" data-path="chapter2/01_The_Basics.html">
<a href="../chapter2/01_The_Basics.html">
<i class="fa fa-check"></i> <b>2.1.</b> 基础部分
</a>
</li>
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
<a href="../chapter2/02_Basic_Operators.html">
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
</a>
</li>
<li class="chapter " data-level="2.3" data-path="chapter2/03_Strings_and_Characters.html">
<a href="../chapter2/03_Strings_and_Characters.html">
<i class="fa fa-check"></i> <b>2.3.</b> 字符串和字符
</a>
</li>
<li class="chapter " data-level="2.4" data-path="chapter2/04_Collection_Types.html">
<a href="../chapter2/04_Collection_Types.html">
<i class="fa fa-check"></i> <b>2.4.</b> 集合类型
</a>
</li>
<li class="chapter " data-level="2.5" data-path="chapter2/05_Control_Flow.html">
<a href="../chapter2/05_Control_Flow.html">
<i class="fa fa-check"></i> <b>2.5.</b> 控制流
</a>
</li>
<li class="chapter " data-level="2.6" data-path="chapter2/06_Functions.html">
<a href="../chapter2/06_Functions.html">
<i class="fa fa-check"></i> <b>2.6.</b> 函数
</a>
</li>
<li class="chapter " data-level="2.7" data-path="chapter2/07_Closures.html">
<a href="../chapter2/07_Closures.html">
<i class="fa fa-check"></i> <b>2.7.</b> 闭包
</a>
</li>
<li class="chapter " data-level="2.8" data-path="chapter2/08_Enumerations.html">
<a href="../chapter2/08_Enumerations.html">
<i class="fa fa-check"></i> <b>2.8.</b> 枚举
</a>
</li>
<li class="chapter " data-level="2.9" data-path="chapter2/09_Classes_and_Structures.html">
<a href="../chapter2/09_Classes_and_Structures.html">
<i class="fa fa-check"></i> <b>2.9.</b> 类和结构体
</a>
</li>
<li class="chapter " data-level="2.10" data-path="chapter2/10_Properties.html">
<a href="../chapter2/10_Properties.html">
<i class="fa fa-check"></i> <b>2.10.</b> 属性
</a>
</li>
<li class="chapter " data-level="2.11" data-path="chapter2/11_Methods.html">
<a href="../chapter2/11_Methods.html">
<i class="fa fa-check"></i> <b>2.11.</b> 方法
</a>
</li>
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
<a href="../chapter2/12_Subscripts.html">
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
</a>
</li>
<li class="chapter " data-level="2.13" data-path="chapter2/13_Inheritance.html">
<a href="../chapter2/13_Inheritance.html">
<i class="fa fa-check"></i> <b>2.13.</b> 继承
</a>
</li>
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
<a href="../chapter2/14_Initialization.html">
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
</a>
</li>
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
<a href="../chapter2/15_Deinitialization.html">
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
</a>
</li>
<li class="chapter " data-level="2.16" data-path="chapter2/16_Automatic_Reference_Counting.html">
<a href="../chapter2/16_Automatic_Reference_Counting.html">
<i class="fa fa-check"></i> <b>2.16.</b> 自动引用计数
</a>
</li>
<li class="chapter " data-level="2.17" data-path="chapter2/17_Optional_Chaining.html">
<a href="../chapter2/17_Optional_Chaining.html">
<i class="fa fa-check"></i> <b>2.17.</b> 可选链
</a>
</li>
<li class="chapter " data-level="2.18" data-path="chapter2/18_Type_Casting.html">
<a href="../chapter2/18_Type_Casting.html">
<i class="fa fa-check"></i> <b>2.18.</b> 类型检查
</a>
</li>
<li class="chapter " data-level="2.19" data-path="chapter2/19_Nested_Types.html">
<a href="../chapter2/19_Nested_Types.html">
<i class="fa fa-check"></i> <b>2.19.</b> 嵌套类型
</a>
</li>
<li class="chapter " data-level="2.20" data-path="chapter2/20_Extensions.html">
<a href="../chapter2/20_Extensions.html">
<i class="fa fa-check"></i> <b>2.20.</b> 扩展
</a>
</li>
<li class="chapter " data-level="2.21" data-path="chapter2/21_Protocols.html">
<a href="../chapter2/21_Protocols.html">
<i class="fa fa-check"></i> <b>2.21.</b> 协议
</a>
</li>
<li class="chapter " data-level="2.22" data-path="chapter2/22_Generics.html">
<a href="../chapter2/22_Generics.html">
<i class="fa fa-check"></i> <b>2.22.</b> 泛型
</a>
</li>
<li class="chapter " data-level="2.23" data-path="chapter2/23_Advanced_Operators.html">
<a href="../chapter2/23_Advanced_Operators.html">
<i class="fa fa-check"></i> <b>2.23.</b> 高级操作符
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3" data-path="chapter3/chapter3.html">
<a href="../chapter3/chapter3.html">
<i class="fa fa-check"></i> <b>3.</b> 语言参考
</a>
<ul class="articles">
<li class="chapter " data-level="3.1" data-path="chapter3/01_About_the_Language_Reference.html">
<a href="../chapter3/01_About_the_Language_Reference.html">
<i class="fa fa-check"></i> <b>3.1.</b> 关于语言参考
</a>
</li>
<li class="chapter " data-level="3.2" data-path="chapter3/02_Lexical_Structure.html">
<a href="../chapter3/02_Lexical_Structure.html">
<i class="fa fa-check"></i> <b>3.2.</b> 词法结构
</a>
</li>
<li class="chapter " data-level="3.3" data-path="chapter3/03_Types.html">
<a href="../chapter3/03_Types.html">
<i class="fa fa-check"></i> <b>3.3.</b> 类型
</a>
</li>
<li class="chapter " data-level="3.4" data-path="chapter3/04_Expressions.html">
<a href="../chapter3/04_Expressions.html">
<i class="fa fa-check"></i> <b>3.4.</b> 表达式
</a>
</li>
<li class="chapter " data-level="3.5" data-path="chapter3/10_Statements.html">
<a href="../chapter3/10_Statements.html">
<i class="fa fa-check"></i> <b>3.5.</b> 语句
</a>
</li>
<li class="chapter " data-level="3.6" data-path="chapter3/05_Declarations.html">
<a href="../chapter3/05_Declarations.html">
<i class="fa fa-check"></i> <b>3.6.</b> 声明
</a>
</li>
<li class="chapter " data-level="3.7" data-path="chapter3/06_Attributes.html">
<a href="../chapter3/06_Attributes.html">
<i class="fa fa-check"></i> <b>3.7.</b> 特性
</a>
</li>
<li class="chapter " data-level="3.8" data-path="chapter3/07_Patterns.html">
<a href="../chapter3/07_Patterns.html">
<i class="fa fa-check"></i> <b>3.8.</b> 模式
</a>
</li>
<li class="chapter " data-level="3.9" data-path="chapter3/08_Generic_Parameters_and_Arguments.html">
<a href="../chapter3/08_Generic_Parameters_and_Arguments.html">
<i class="fa fa-check"></i> <b>3.9.</b> 泛型参数
</a>
</li>
<li class="chapter " data-level="3.10" data-path="chapter3/09_Summary_of_the_Grammar.html">
<a href="../chapter3/09_Summary_of_the_Grammar.html">
<i class="fa fa-check"></i> <b>3.10.</b> 语法总结
</a>
</li>
</ul>
</li>
<li class="divider"></li>
<li>
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
</li>
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
</ul>
</div>
<div class="book-body">
<div class="body-inner">
<div class="page-wrapper" tabindex="-1">
<div class="book-progress">
<div class="bar">
<div class="inner" style="width: 68.42105263157895%;min-width: 65.78947368421052%;"></div>
</div>
<div class="chapters">
<a href="../index.html" title="Introduction" class="chapter done new-chapter" data-progress="0" style="left: 0%;"></a>
<a href="../chapter1/chapter1.html" title="欢迎使用 Swift" class="chapter done new-chapter" data-progress="1" style="left: 2.6315789473684212%;"></a>
<a href="../chapter1/01_swift.html" title="关于 Swift" class="chapter done " data-progress="1.1" style="left: 5.2631578947368425%;"></a>
<a href="../chapter1/02_a_swift_tour.html" title="Swift 初见" class="chapter done " data-progress="1.2" style="left: 7.894736842105263%;"></a>
<a href="../chapter2/chapter2.html" title="Swift 教程" class="chapter done new-chapter" data-progress="2" style="left: 10.526315789473685%;"></a>
<a href="../chapter2/01_The_Basics.html" title="基础部分" class="chapter done " data-progress="2.1" style="left: 13.157894736842104%;"></a>
<a href="../chapter2/10_Properties.html" title="属性" class="chapter done " data-progress="2.10" style="left: 15.789473684210526%;"></a>
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 26.31578947368421%;"></a>
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 28.94736842105263%;"></a>
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 31.57894736842105%;"></a>
<a href="../chapter2/17_Optional_Chaining.html" title="可选链" class="chapter done " data-progress="2.17" style="left: 34.21052631578947%;"></a>
<a href="../chapter2/18_Type_Casting.html" title="类型检查" class="chapter done " data-progress="2.18" style="left: 36.8421052631579%;"></a>
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
<a href="../chapter2/21_Protocols.html" title="协议" class="chapter done " data-progress="2.21" style="left: 47.36842105263158%;"></a>
<a href="../chapter2/22_Generics.html" title="泛型" class="chapter done " data-progress="2.22" style="left: 50%;"></a>
<a href="../chapter2/23_Advanced_Operators.html" title="高级操作符" class="chapter done " data-progress="2.23" style="left: 52.63157894736842%;"></a>
<a href="../chapter2/03_Strings_and_Characters.html" title="字符串和字符" class="chapter done " data-progress="2.3" style="left: 55.26315789473684%;"></a>
<a href="../chapter2/04_Collection_Types.html" title="集合类型" class="chapter done " data-progress="2.4" style="left: 57.89473684210526%;"></a>
<a href="../chapter2/05_Control_Flow.html" title="控制流" class="chapter done " data-progress="2.5" style="left: 60.526315789473685%;"></a>
<a href="../chapter2/06_Functions.html" title="函数" class="chapter done " data-progress="2.6" style="left: 63.1578947368421%;"></a>
<a href="../chapter2/07_Closures.html" title="闭包" class="chapter done " data-progress="2.7" style="left: 65.78947368421052%;"></a>
<a href="../chapter2/08_Enumerations.html" title="枚举" class="chapter done " data-progress="2.8" style="left: 68.42105263157895%;"></a>
<a href="../chapter2/09_Classes_and_Structures.html" title="类和结构体" class="chapter " data-progress="2.9" style="left: 71.05263157894737%;"></a>
<a href="../chapter3/chapter3.html" title="语言参考" class="chapter new-chapter" data-progress="3" style="left: 73.6842105263158%;"></a>
<a href="../chapter3/01_About_the_Language_Reference.html" title="关于语言参考" class="chapter " data-progress="3.1" style="left: 76.3157894736842%;"></a>
<a href="../chapter3/09_Summary_of_the_Grammar.html" title="语法总结" class="chapter " data-progress="3.10" style="left: 78.94736842105263%;"></a>
<a href="../chapter3/02_Lexical_Structure.html" title="词法结构" class="chapter " data-progress="3.2" style="left: 81.57894736842105%;"></a>
<a href="../chapter3/03_Types.html" title="类型" class="chapter " data-progress="3.3" style="left: 84.21052631578948%;"></a>
<a href="../chapter3/04_Expressions.html" title="表达式" class="chapter " data-progress="3.4" style="left: 86.84210526315789%;"></a>
<a href="../chapter3/10_Statements.html" title="语句" class="chapter " data-progress="3.5" style="left: 89.47368421052632%;"></a>
<a href="../chapter3/05_Declarations.html" title="声明" class="chapter " data-progress="3.6" style="left: 92.10526315789474%;"></a>
<a href="../chapter3/06_Attributes.html" title="特性" class="chapter " data-progress="3.7" style="left: 94.73684210526316%;"></a>
<a href="../chapter3/07_Patterns.html" title="模式" class="chapter " data-progress="3.8" style="left: 97.36842105263158%;"></a>
<a href="../chapter3/08_Generic_Parameters_and_Arguments.html" title="泛型参数" class="chapter " data-progress="3.9" style="left: 100%;"></a>
</div>
</div>
<div class="page-inner">
<section class="normal" id="section-gitbook_306">
<h1 id="-">枚举</h1>
<p>本页内容包含:</p>
<ul>
<li>枚举语法</li>
<li>匹配枚举值与<code>Swith</code>语句</li>
<li>关联值associated values</li>
<li>原始值raw values</li>
</ul>
<p>枚举enumeration定义了一个通用类型的一组相关的值使你可以在你的代码中以一个安全的方式来使用这些值。</p>
<p>如果你熟悉C语言你就会知道在C语言中枚举指定相关名称为一组整型值。Swift中的枚举更加灵活不必给每一个枚举成员enumeration member提供一个值。如果一个值被认为是“原始”值被提供给每个枚举成员则该值可以是一个字符串一个字符或是一个整型值或浮点值。</p>
<p>此外枚举成员可以指定任何类型的关联值存储到枚举成员值中就像其他语言中的联合体unions和变体variants。你可以定义一组通用的相关成员作为枚举的一部分每一组都有不同的一组与它相关的适当类型的数值。</p>
<p>在Swift中枚举类型是一等first-class类型。它们采用了很多传统上只被类class)所支持的特征例如计算型属性computed properties)用于提供关于枚举当前值的附加信息实例方法instance methods用于提供和枚举所代表的值相关联的功能。枚举也可以定义构造函数initializers来提供一个初始成员值可以在原始的实现基础上扩展它们的功能可以遵守协议protocols来提供标准的功能。</p>
<p>欲了解更多相关功能请参见属性Properties方法Methods构造过程Initialization扩展Extensions和协议Protocols</p>
<h2 id="-">枚举语法</h2>
<p>使用<code>enum</code>关键词并且把它们的整个定义放在一对大括号内:</p>
<pre><code>enum SomeEumeration {
// enumeration definition goes here
}
</code></pre><p>以下是指南针四个方向的一个例子:</p>
<pre><code>enum CompassPoint {
case North
case South
case East
case West
}
</code></pre><p>一个枚举中被定义的值(例如 <code>North</code><code>South</code><code>East</code><code>West</code>)是枚举的<strong><em>成员值</em></strong>(或者<strong><em>成员</em></strong>)。<code>case</code>关键词表明新的一行成员值将被定义。</p>
<blockquote>
<p>注意:
不像C和Objective-C一样Swift的枚举成员在被创建时不会被赋予一个默认的整数值。在上面的<code>CompassPoints</code>例子中,<code>North</code><code>South</code><code>East</code><code>West</code>不是隐式得等于<code>0</code><code>1</code><code>2</code><code>3</code>。相反的,这些不同的枚举成员在<code>CompassPoint</code>的一种显示定义中拥有各自不同的值。</p>
</blockquote>
<p>多个成员值可以出现在同一行上,用逗号隔开:</p>
<pre><code>enum Planet {
case Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Nepturn
}
</code></pre><p>每个枚举定义了一个全新的类型。像Swift中其他类型一样它们的名字例如<code>CompassPoint</code><code>Planet</code>)必须以一个大写字母开头。给枚举类型起一个单数名字而不是复数名字,以便于读起来更加容易理解:</p>
<pre><code>var directionToHead = CompassPoint.West
</code></pre><p><code>directionToHead</code>的类型被推断当它被<code>CompassPoint</code>的一个可能值初始化。一旦<code>directionToHead</code>被声明为一个<code>CompassPoint</code>,你可以使用更短的点(.)语法将其设置为另一个<code>CompassPoint</code>的值:</p>
<pre><code>directionToHead = .East
</code></pre><p><code>directionToHead</code>的类型已知时,当设定它的值时,你可以不再写类型名。使用显示类型的枚举值可以让代码具有更好的可读性。</p>
<h2 id="-switch-">匹配枚举值和<code>Switch</code>语句</h2>
<p>你可以匹配单个枚举值和<code>switch</code>语句:</p>
<pre><code>directionToHead = .South
switch directionToHead {
case .North:
println(&quot;Lots of planets have a north&quot;)
case .South:
println(&quot;Watch out for penguins&quot;)
case .East:
println(&quot;Where the sun rises&quot;)
case .West:
println(&quot;Where the skies are blue&quot;)
}
// prints &quot;Watch out for penguins”
</code></pre><p>你可以如此理解这段代码:</p>
<p>“考虑<code>directionToHead</code>的值。当它等于<code>.North</code>,打印<code>“Lots of planets have a north”</code>。当它等于<code>.South</code>,打印<code>“Watch out for penguins”</code>。”</p>
<p>等等依次类推。</p>
<p>正如在控制流Control Flow中介绍当考虑一个枚举的成员们时一个<code>switch</code>语句必须全面。如果忽略了<code>.West</code>这种情况,上面那段代码将无法通过编译,因为它没有考虑到<code>CompassPoint</code>的全部成员。全面性的要求确保了枚举成员不会被意外遗漏。</p>
<p>当不需要匹配每个枚举成员的时候,你可以提供一个默认<code>default</code>分支来涵盖所有未明确被提出的任何成员:</p>
<pre><code>let somePlanet = Planet.Earth
switch somePlanet {
case .Earth:
println(&quot;Mostly harmless&quot;)
default:
println(&quot;Not a safe place for humans&quot;)
}
// prints &quot;Mostly harmless”
</code></pre><h2 id="-associated-values-">关联值Associated Values</h2>
<p>上一小节的例子演示了一个枚举的成员是如何被定义(分类)的。你可以为<code>Planet.Earth</code>设置一个常量或则变量,并且在之后查看这个值。然而,有时候会很有用如果能够把其他类型的关联值和成员值一起存储起来。这能让你随着成员值存储额外的自定义信息,并且当每次你在代码中利用该成员时允许这个信息产生变化。</p>
<p>你可以定义Swift的枚举存储任何类型的关联值如果需要的话每个成员的数据类型可以是各不相同的。枚举的这种特性跟其他语言中的可辨识联合discriminated unions标签联合tagged unions或者变体variants相似。</p>
<p>例如假设一个库存跟踪系统需要利用两种不同类型的条形码来跟踪商品。有些商品上标有UPC-A格式的一维码它使用数字0到9.每一个条形码都有一个代表“数字系统”的数字该数字后接10个代表“标识符”的数字。最后一个数字是“检查”位用来验证代码是否被正确扫描</p>
<p><img width="252" height="120" a"" src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/barcode_UPC_2x.png"></p>
<p>其他商品上标有QR码格式的二维码它可以使用任何ISO8859-1字符并且可以编码一个最多拥有2,953字符的字符串:</p>
<p><img width="169" height="169" alt="" src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/barcode_QR_2x.png"></p>
<p>对于库存跟踪系统来说能够把UPC-A码作为三个整型值的元组和把QR码作为一个任何长度的字符串存储起来是方便的。</p>
<p>在Swift中用来定义两种商品条码的枚举是这样子的</p>
<pre><code>enum Barcode {
case UPCA(Int, Int, Int)
case QRCode(String)
}
</code></pre><p>以上代码可以这么理解:</p>
<p>“定义一个名为<code>Barcode</code>的枚举类型,它可以是<code>UPCA</code>的一个关联值(<code>Int</code><code>Int</code><code>Int</code>),或者<code>QRCode</code>的一个字符串类型(<code>String</code>)关联值。”</p>
<p>这个定义不提供任何<code>Int</code><code>String</code>的实际值,它只是定义了,当<code>Barcode</code>常量和变量等于<code>Barcode.UPCA</code><code>Barcode.QRCode</code>时,关联值的类型。</p>
<p>然后可以使用任何一种条码类型创建新的条码,如:</p>
<pre><code>var productBarcode = Barcode.UPCA(8, 85909_51226, 3)
</code></pre><p>以上例子创建了一个名为<code>productBarcode</code>的新变量,并且赋给它一个<code>Barcode.UPCA</code>的关联元组值<code>(8, 8590951226, 3)</code>。提供的“标识符”值在整数字中有一个下划线,使其便于阅读条形码。</p>
<p>同一个商品可以被分配给一个不同类型的条形码,如:</p>
<pre><code>productBarcode = .QRCode(&quot;ABCDEFGHIJKLMNOP&quot;)
</code></pre><p>这时,原始的<code>Barcode.UPCA</code>和其整数值被新的<code>Barcode.QRCode</code>和其字符串值所替代。条形码的常量和变量可以存储一个<code>.UPCA</code>或者一个<code>.QRCode</code>(连同它的关联值),但是在任何指定时间只能存储其中之一。</p>
<p>像以前那样不同的条形码类型可以使用一个switch语句来检查然而这次关联值可以被提取作为switch语句的一部分。你可以在<code>switch</code>的case分支代码中提取每个关联值作为一个常量<code>let</code>前缀)或者作为一个变量(用<code>var</code>前缀)来使用:</p>
<pre><code>switch productBarcode {
case .UPCA(let numberSystem, let identifier, let check):
println(&quot;UPC-A with value of \(numberSystem), \(identifier), \(check).&quot;)
case .QRCode(let productCode):
println(&quot;QR code with value of \(productCode).&quot;)
}
// prints &quot;QR code with value of ABCDEFGHIJKLMNOP.”
</code></pre><p>如果一个枚举成员的所有关联值被提取为常量,或者它们全部被提取为变量,为了简洁,你可以只放置一个<code>var</code>或者<code>let</code>标注在成员名称前:</p>
<pre><code>switch productBarcode {
case let .UPCA(numberSystem, identifier, check):
println(&quot;UPC-A with value of \(numberSystem), \(identifier), \(check).&quot;)
case let .QRCode(productCode):
println(&quot;QR code with value of \(productCode).&quot;)
}
// prints &quot;QR code with value of ABCDEFGHIJKLMNOP.&quot;
</code></pre><h2 id="-raw-values-">原始值Raw Values</h2>
<p>在关联值小节的条形码例子中演示了一个枚举的成员如何声明它们存储不同类型的关联值。作为关联值的替代,枚举成员可以被默认值(称为原始值)预先填充,其中这些原始值具有相同的类型。</p>
<p>这里是一个枚举成员存储原始ASCII值的例子</p>
<pre><code>enum ASCIIControlCharacter: Character {
case Tab = &quot;\t&quot;
case LineFeed = &quot;\n&quot;
case CarriageReturn = &quot;\r&quot;
}
</code></pre><p>在这里,称为<code>ASCIIControlCharacter</code>的枚举的原始值类型被定义为字符型<code>Character</code>并被设置了一些比较常见的ASCII控制字符。字符值的描述请详见字符串和字符<code>Strings and Characters</code>部分。</p>
<p>注意原始值和关联值是不相同的。当你开始在你的代码中定义枚举的时候原始值是被预先填充的值像上述三个ASCII码。对于一个特定的枚举成员它的原始值始终是相同的。关联值是当你在创建一个基于枚举成员的新常量或变量时才会被设置并且每次当你这么做得时候它的值可以是不同的。</p>
<p>原始值可以是字符串,字符,或者任何整型值或浮点型值。每个原始值在它的枚举声明中必须是唯一的。当整型值被用于原始值,如果其他枚举成员没有值时,它们会自动递增。</p>
<p>下面的枚举是对之前<code>Planet</code>这个枚举的一个细化利用原始整型值来表示每个planet在太阳系中的顺序</p>
<pre><code>enum Planet: Int {
case Mercury = 1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune
}
</code></pre><p>自动递增意味着<code>Planet.Venus</code>的原始值是<code>2</code>,依次类推。</p>
<p>使用枚举成员的<code>toRaw</code>方法可以访问该枚举成员的原始值:</p>
<pre><code>let earthsOrder = Planet.Earth.toRaw()
// earthsOrder is 3
</code></pre><p>使用枚举的<code>fromRaw</code>方法来试图找到具有特定原始值的枚举成员。这个例子通过原始值<code>7</code>识别<code>Uranus</code></p>
<pre><code>let possiblePlanet = Planet.fromRaw(7)
// possiblePlanet is of type Planet? and equals Planet.Uranus
</code></pre><p>然而,并非所有可能的<code>Int</code>值都可以找到一个匹配的行星。正因为如此,<code>fromRaw</code>方法可以返回一个<strong><em>可选</em></strong>的枚举成员。在上面的例子中,<code>possiblePlanet</code><code>Planet?</code>类型,或“可选的<code>Planet</code>”。</p>
<p>如果你试图寻找一个位置为9的行星通过<code>fromRaw</code>返回的可选<code>Planet</code>值将是<code>nil</code></p>
<pre><code>let positionToFind = 9
if let somePlanet = Planet.fromRaw(positionToFind) {
switch somePlanet {
case .Earth:
println(&quot;Mostly harmless&quot;)
default:
println(&quot;Not a safe place for humans&quot;)
}
} else {
println(&quot;There isn&#39;t a planet at position \(positionToFind)&quot;)
}
// prints &quot;There isn&#39;t a planet at position 9
</code></pre><p>这个范例使用可选绑定optional binding通过原始值<code>9</code>试图访问一个行星。<code>if let somePlanet = Planet.fromRaw(9)</code>语句获得一个可选<code>Planet</code>,如果可选<code>Planet</code>可以被获得,把<code>somePlanet</code>设置成该可选<code>Planet</code>的内容。在这个范例中,无法检索到位置为<code>9</code>的行星,所以<code>else</code>分支被执行。</p>
</section>
</div>
</div>
</div>
<a href="../chapter2/07_Closures.html" class="navigation navigation-prev " aria-label="Previous page: 闭包"><i class="fa fa-angle-left"></i></a>
<a href="../chapter2/09_Classes_and_Structures.html" class="navigation navigation-next " aria-label="Next page: 类和结构体"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
<script src="../gitbook/app.js"></script>
<script src="../gitbook/plugins/gitbook-plugin-mixpanel/plugin.js"></script>
<script src="http://cdn.mathjax.org/mathjax/2.0-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script src="../gitbook/plugins/gitbook-plugin-mathjax/plugin.js"></script>
<script>
require(["gitbook"], function(gitbook) {
var config = {};
gitbook.start(config);
});
</script>
</body>
</html>