Swift中的枚举与C、OC中的枚举有很大的区别,增加了原来类中都有的特性到枚举中。
定义枚举的语法:
定义一个指南针的四个方向的枚举:
-
- enum CompassPoint {
- case North
- case South
- case East
- case West
- }
// 使用枚举
- var direction = CompassPoint.West
// 再次修改时,可以不指定枚举,而可以只使用.引用
下面是引用 BY 史薇芙特 · 2014年6月5日
枚举定义了一个常用的具有相关性的一组数据,并在你的代码中以一个安全的方式使用它们。
如果你熟悉C语言,你就会知道,C语言中的枚举指定相关名称为一组整数值。在Swift中枚举更为灵活,不必为枚举的每个成员提供一个值。如果一个值(被称为“原始”的值)被提供给每个枚举成员,则该值可以是一个字符串,一个字符,或者任何整数或浮点类型的值。
另外,枚举成员可以指定任何类型,每个成员都可以存储的不同的相关值,就像其他语言中使用集合或变体。你还可以定义一组通用的相关成员为一个枚举,每一种都有不同的一组与它相关的适当类型的值的一部分。
在Swift中枚举类型是最重要的类型。它采用了很多以前只有类才具有的特性,如计算性能,以提供有关枚举的当前值的更多信息,方法和实例方法提供的功能相关的枚举表示的值传统上支持的许多功能。枚举也可以定义初始化,以提供一个初始成员值;可以在原有基础上扩展扩大它们的功能;并使用协议来提供标准功能。
欲了解更多有关这些功能,请参见Properties, Methods, Initialization, Extensions, Protocols
1、枚举语法
使用枚举enum关键词并把他们的整个定义在一对大括号内:
<span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">1</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">2</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">3</span> | <span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">enum</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">SomeEnumeration</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">{</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#666666;margin:0px; padding:0px; border:0px;font-size:undefined; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><em><span class="com" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">// enumeration definition goes here</span></em></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">}</span></span>
|
下面是一个指南针的四个点一个例子:
<span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">1</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">2</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">3</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">4</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">5</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">6</span> | <span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">enum</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">CompassPoint</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">{</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">case</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">North</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">case</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">South</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">case</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">East</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">case</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">West</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">}</span></span>
|
在枚举中定义的值(如North,South,East和West)是枚举的成员值(或成员)。这个例子里case关键字表示成员值一条新的分支将被定义。
Note
不像C和Objective-C,Swift枚举成员在创建时不分配默认整数值。在上面的例子CompassPoints中North,South,Eath,West不等于隐含0,1,2和3,而是一种与CompassPoint明确被定义的类型却各不相同的值。
多个成员的值可以出现在一行上,用逗号分隔:
<span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">1</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">2</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">3</span> | <span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">enum</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Planet</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">{</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">case</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Mercury</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">,</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Venus</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">,</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Earth</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">,</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Mars</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">,</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Jupiter</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">,</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Saturn</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">,</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Uranus</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">,</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Neptune</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">}</span></span>
|
每个枚举定义中定义了一个全新的类型。像其他Swift的类型,它们的名称(如CompassPoint和Planet)应为大写字母。给枚举类型单数而不是复数的名字,这样理解起来更加容易如:
<span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">1</span> | <span style="font-family:inherit;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline"><strong><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">var</span></strong></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> directionToHead </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">=</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">CompassPoint</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">.</span></span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">West</span>
|
使用directionToHead的类型时,用CompassPoint的一个可能值初始化的推断。一旦directionToHead被声明为一个CompassPoint,您可以将其设置为使用更短的.语法而不用再书写枚举CompassPoint值本身:
<span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">1</span> | <span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">directionToHead </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">=</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">.</span></span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">East</span>
|
directionToHead的类型是已知的,所以你可以在设定它的值时,不写该类型。使用类型明确的枚举值可以让代码具有更好的可读性。
2、匹配枚举值与switch语句
你可以使用单个枚举值匹配switch语句:
<span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">1</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">2</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">3</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">4</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">5</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">6</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">7</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">8</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">9</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">10</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">11</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">12</span> | <span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">directionToHead </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">=</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">.</span></span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">South</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">switch</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> directionToHead </span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">{</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">case</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">.</span></span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">North</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">:</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
println</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">(</span></span><span style="font-family:inherit;color:#00ff;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">"Lots of planets have a north"</span></span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">)</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">case</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">.</span></span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">South</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">:</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
println</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">(</span></span><span style="font-family:inherit;color:#00ff;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">"Watch out for penguins"</span></span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">)</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">case</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">.</span></span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">East</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">:</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
println</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">(</span></span><span style="font-family:inherit;color:#00ff;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">"Where the sun rises"</span></span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">)</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">case</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">.</span></span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">West</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">:</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
println</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">(</span></span><span style="font-family:inherit;color:#00ff;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">"Where the skies are blue"</span></span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">)</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">}</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#666666;margin:0px; padding:0px; border:0px;font-size:undefined; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><em><span class="com" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">// prints "Watch out for penguins"</span></em></span>
|
你可以理解这段代码:
“考虑directionToHead的价值。当它等于North,打印“Lots of planets have a north”。当它等于South,打印“Watch out for penguins”等等。
正如控制流所描述,Switch语句考虑枚举的成员,如果省略了West时,这段代码无法编译,因为它没有考虑CompassPoint成员的完整性。Switch语句要求全面性确保枚举成员,避免不小心漏掉情况发生。
当它不需要为每一个枚举成员都匹配的情况下,你可以提供一个默认default分支来涵盖未明确提到的任何成员:
<span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">1</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">2</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">3</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">4</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">5</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">6</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">7</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">8</span> | <span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">let somePlanet </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">=</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Planet</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">.</span></span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Earth</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">switch</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> somePlanet </span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">{</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">case</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">.</span></span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Earth</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">:</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
println</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">(</span></span><span style="font-family:inherit;color:#00ff;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">"Mostly harmless"</span></span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">)</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">default</span></span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">:</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
println</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">(</span></span><span style="font-family:inherit;color:#00ff;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">"Not a safe place for humans"</span></span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">)</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">}</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#666666;margin:0px; padding:0px; border:0px;font-size:undefined; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><em><span class="com" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">// prints "Mostly harmless"</span></em></span>
|
3、关联值
在上一节中的示例延时了一个枚举的成员是如何被定义(分类)的。你可以为Planet.Earth设置一个常量或变量,然后在代码中检查这个值。但是,它有时是有用的才能存储其它类型的关联值除了这些成员的值。这让你随着成员值存储额外的自定义信息,并允许在你的代码中来使用该信息。
Swift的枚举类型可以由一些数据类型相关的组成,如果需要的话,这些数据类型可以是各不相同的。枚举的这种特性跟其它语言中的奇异集合,标签集合或者变体相似
例如,假设一个库存跟踪系统需要由两种不同类型的条形码来跟踪产品。有些产品上标有UPC-A代码格式,它使用数字0到9的一维条码,每一个条码都有一个“数字系统”的数字,后跟十“标识符”的数字。最后一位是“检查”位,以验证代码已被正确扫描:
其他产品都贴有二维条码QR码格式,它可以使用任何的ISO8859-1字符,并可以编码字符串,最多2,953个字符:
这将是方便的库存跟踪系统能够存储UPC-A条码作为三个整数的元组,和QR代码的条形码的任何长度的字符串。
在Swift中可以使用一个枚举来定义两种类型的产品条形码,结构可以是这样的:
<span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">1</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">2</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">3</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">4</span> | <span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">enum</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Barcode</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">{</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">case</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> UPCA</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">(</span></span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Int</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">,</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Int</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">,</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Int</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">)</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">case</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">QRCode</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">(</span></span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">String</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">)</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">}</span></span>
|
这可以被理解为:
“定义一个名为条形码枚举类型,它可以是UPC-A的任一值类型的关联值(Int,Int,Int),或QRCode的一个类型为String的关联值。”
这个定义不提供任何实际的Int或String值,它只是定义了条形码常量和变量当等于Barcode.UPCA或Barcode.QRCode关联值的类型的时候的存储形式。
然后可以使用任何一种类型来创建新的条码:
<span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">1</span> | <span style="font-family:inherit;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline"><strong><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">var</span></strong></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> productBarcode </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">=</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Barcode</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">.</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">UPCA</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">(</span></span><span style="font-family:inherit;color:#cc66cc;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">8</span></span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">,</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#cc66cc;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">85909</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">_51226</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">,</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#cc66cc;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">3</span></span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">)</span></span>
|
此示例创建一个名为productBarcode新的变量,并与相关联的元组值赋给它Barcode.UPCA的值(8,8590951226,3)。提供的“标识符”值都有整数加下划线的文字,85909_51226,使其更易于阅读的条形码。
同一产品可以分配不同类型的条形码:
<span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">1</span> | <span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">productBarcode </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">=</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">.</span></span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">QRCode</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">(</span></span><span style="font-family:inherit;color:#00ff;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">"ABCDEFGHIJKLMNOP"</span></span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">)</span></span>
|
在这一点上,原来Barcode.UPCA和其整数值被新的Barcode.QRCode及其字符串值代替。_条形码的常量和变量可以存储任何一个_UPCA或QRCode的(连同其关联值),但它们只能存储其中之一在任何指定时间。
不同的条码类型像以前一样可以使用一个switch语句来检查,但是这一次相关的值可以被提取作为switch语句的一部分。您提取每个相关值作为常数(let前缀)或变量(var前缀)不同的情况下,在switch语句的case代码内使用:
<span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">1</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">2</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">3</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">4</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">5</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">6</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">7</span> | <span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">switch</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> productBarcode </span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">{</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">case</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">.</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">UPCA</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">(</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">let numberSystem</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">,</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> let identifier</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">,</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> let check</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">)</span></span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">:</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
println</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">(</span></span><span style="font-family:inherit;color:#00ff;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">"UPC-A with value of \(numberSystem), \(identifier), \(check)."</span></span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">)</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">case</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">.</span></span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">QRCode</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">(</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">let productCode</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">)</span></span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">:</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
println</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">(</span></span><span style="font-family:inherit;color:#00ff;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">"QR code with value of \(productCode)."</span></span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">)</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">}</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#666666;margin:0px; padding:0px; border:0px;font-size:undefined; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><em><span class="com" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">// prints "QR code with value of ABCDEFGHIJKLMNOP."</span></em></span>
|
如果所有的枚举成员的关联值的提取为常数,或者当所有被提取为变量,为了简洁起见,可以放置一个var,或let标注在成员名称前:
<span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">1</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">2</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">3</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">4</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">5</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">6</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">7</span> | <span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">switch</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> productBarcode </span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">{</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">case</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> let </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">.</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">UPCA</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">(</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">numberSystem</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">,</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> identifier</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">,</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> check</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">)</span></span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">:</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
println</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">(</span></span><span style="font-family:inherit;color:#00ff;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">"UPC-A with value of \(numberSystem), \(identifier), \(check)."</span></span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">)</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">case</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> let </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">.</span></span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">QRCode</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">(</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">productCode</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">)</span></span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">:</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
println</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">(</span></span><span style="font-family:inherit;color:#00ff;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">"QR code with value of \(productCode)."</span></span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">)</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">}</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#666666;margin:0px; padding:0px; border:0px;font-size:undefined; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><em><span class="com" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">// prints "QR code with value of ABCDEFGHIJKLMNOP."</span></em></span>
|
4、原始值
在关联值的条形码的例子演示了一个枚举的成员如何能声明它们存储不同类型的关联值。作为替代关联值,枚举成员可以拿出预先填入缺省值(称为原始值),从而具有相同的类型。
这里是一个存储原始的ASCII值命名枚举成员的一个例子:
<span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">1</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">2</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">3</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">4</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">5</span> | <span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">enum</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">ASCIIControlCharacter</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">:</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Character</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">{</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">case</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Tab</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">=</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#00ff;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">"</span><span style="font-family:inherit;color:#0099;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><strong><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">\t</span></strong></span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">"</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">case</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">LineFeed</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">=</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#00ff;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">"</span><span style="font-family:inherit;color:#0099;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><strong><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">\n</span></strong></span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">"</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">case</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">CarriageReturn</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">=</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#00ff;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">"</span><span style="font-family:inherit;color:#0099;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><strong><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline;">\r</span></strong></span><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">"</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">}</span></span>
|
在这里,原始值被定义为字符类型的枚举叫做ASCIIControlCharacter,并设置了一些比较常见的ASCII控制字符。字符值的字符串和字符的描述。
注意,原始值是不相同关联值。原始值设置为预填充的值时,应先在你的代码中定义枚举,像上述三个ASCII码。对于一个特定的枚举成员的原始值始终是相同的。当你创建一个基于枚举的常量或变量的新成员的关联值设置,每次当你这样做的时候可以是不同的。
原始值可以是字符串,字符,或任何整数或浮点数类型。每个原始值必须在它的枚举中唯一声明。当整数被用于原始值,如果其他枚举成员没有值时,它们自动递增。
下面列举的是一个细化的早期Planet枚举,使用原始整数值来表示每个Planet的太阳系的顺序:
<span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">1</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">2</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">3</span> | <span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">enum</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Planet</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">:</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Int</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">{</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">case</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Mercury</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">=</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#cc66cc;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">1</span></span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">,</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Venus</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">,</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Earth</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">,</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Mars</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">,</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Jupiter</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">,</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Saturn</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">,</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Uranus</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">,</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Neptune</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">}</span></span>
|
自动递增意味着Planet.Venus具有2的原始值,依此类推。
访问其toRaw方法枚举成员的原始值:
<span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">1</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">2</span> | <span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">let earthsOrder </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">=</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Planet</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">.</span></span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Earth</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">.</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">toRaw</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">(</span></span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">)</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#666666;margin:0px; padding:0px; border:0px;font-size:undefined; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><em><span class="com" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">// earthsOrder is 3</span></em></span>
|
使用枚举的fromRaw方法来试图找到一个特定的原始值枚举成员。这个例子识别Uranus的位置通过原始值为7:
<span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">1</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">2</span> | <span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">let possiblePlanet </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">=</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Planet</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">.</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">fromRaw</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">(</span></span><span style="font-family:inherit;color:#cc66cc;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">7</span></span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">)</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#666666;margin:0px; padding:0px; border:0px;font-size:undefined; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><em><span class="com" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">// possiblePlanet is of type Planet? and equals Planet.Uranus</span></em></span>
|
然而,并非所有可能的Int值都会找到一个匹配的星球。正因如此,该fromRaw方法返回一个可选的枚举成员。在上面的例子中,是possiblePlanet类型Planet?或“可选的Planet”。
如果你试图找到一个Planet为9的位置,通过fromRaw返回可选的Planet值将是无:
<span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">1</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">2</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">3</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">4</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">5</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">6</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">7</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">8</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">9</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">10</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">11</span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">12</span> | <span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">let positionToFind </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">=</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#cc66cc;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="lit" style="font-family:inherit;color:#195f91;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">9</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">if</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> let somePlanet </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">=</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Planet</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">.</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">fromRaw</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">(</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">positionToFind</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">)</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">{</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">switch</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> somePlanet </span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">{</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">case</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">.</span></span><span class="typ" style="font-family:inherit;color:teal;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">Earth</span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">:</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
println</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">(</span></span><span style="font-family:inherit;color:#00ff;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">"Mostly harmless"</span></span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">)</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">default</span></span><span style="font-family:inherit;color:#339933;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">:</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
println</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">(</span></span><span style="font-family:inherit;color:#00ff;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">"Not a safe place for humans"</span></span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">)</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">}</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">}</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#b1b10;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="kwd" style="font-family:inherit;color:#1e347b;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">else</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"> </span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">{</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
println</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">(</span></span><span style="font-family:inherit;color:#00ff;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="str" style="font-family:inherit;color:#dd1144;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">"There isn't a planet at position \(positionToFind)"</span></span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">)</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#0990;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><span class="pun" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">}</span></span><span class="pln" style="font-family:inherit;color:#48484c;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">
</span><span style="font-family:inherit;color:#666666;margin:0px; padding:0px; border:0px;font-size:undefined; font-variant:inherit; line-height:inherit; vertical-align:baseline;"><em><span class="com" style="font-family:inherit;color:#93a1a1;margin:0px; padding:0px; border:0px;font-size:undefined; font-style:inherit; font-variant:inherit; line-height:inherit; vertical-align:baseline;">// prints "There isn't a planet at position 9"</span></em></span>
|
这个范例使用somePlanet= Planet.fromRaw(9)来尝试访问可选集合Planet,在可选Planet集合中设置检索条件somePlanet,在原始值为9的情况下,不能检索到位置为9的星球,所有else分支被执行。