make gitbook
This commit is contained in:
@ -10,7 +10,7 @@
|
||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
||||
<meta name="robots" content="index, follow">
|
||||
<meta name="author" content="">
|
||||
<meta name="description" content="Swift 兴趣交流群:307017261">
|
||||
<meta name="description" content="Swift 兴趣交流群:307017261Swift 开发者社区">
|
||||
<meta name="keywords" content="gitbook,github" >
|
||||
<meta name="generator" content="www.gitbook.io">
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.8" data-basepath=".." data-revision="1402792177330">
|
||||
<div class="book" data-level="3.8" data-basepath=".." data-revision="1402808574723">
|
||||
<div class="book-header">
|
||||
<!-- Actions Left -->
|
||||
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
|
||||
@ -587,11 +587,11 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_647">
|
||||
<section class="normal" id="section-gitbook_67">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:honghaoz</p>
|
||||
<p>校对:numbbbbb, stanzhai</p>
|
||||
<p>翻译:<a href="https://github.com/honghaoz" target="_blank">honghaoz</a>
|
||||
校对:<a href="https://github.com/numbbbbb" target="_blank">numbbbbb</a>, <a href="https://github.com/stanzhai" target="_blank">stanzhai</a></p>
|
||||
</blockquote>
|
||||
<h1 id="-patterns-">模式(Patterns)</h1>
|
||||
<hr>
|
||||
@ -614,17 +614,19 @@
|
||||
<p><a name="wildcard_pattern"></a></p>
|
||||
<h2 id="-wildcard-pattern-">通配符模式(Wildcard Pattern)</h2>
|
||||
<p>通配符模式匹配并忽略任何值,包含一个下划线(_)。当你不关心被匹配的值时,可以使用此模式。例如,下面这段代码进行了<code>1...3</code>的循环,并忽略了每次循环的值:</p>
|
||||
<pre><code>for _ in 1...3 {
|
||||
<pre><code class="lang-swift">for _ in 1...3 {
|
||||
// Do something three times.
|
||||
}
|
||||
</code></pre><blockquote>
|
||||
</code></pre>
|
||||
<blockquote>
|
||||
<p>通配符模式语法<br><em>通配符模式</em> → <strong>_</strong> </p>
|
||||
</blockquote>
|
||||
<p><a name="identifier_pattern"></a></p>
|
||||
<h2 id="-identifier-pattern-">标识符模式(Identifier Pattern)</h2>
|
||||
<p>标识符模式匹配任何值,并将匹配的值和一个变量或常量绑定起来。例如,在下面的常量申明中,<code>someValue</code>是一个标识符模式,匹配了类型是<code>Int</code>的<code>42</code>。</p>
|
||||
<pre><code> let someValue = 42
|
||||
</code></pre><p>当匹配成功时,<code>42</code>被绑定(赋值)给常量<code>someValue</code>。</p>
|
||||
<pre><code class="lang-swift">let someValue = 42
|
||||
</code></pre>
|
||||
<p>当匹配成功时,<code>42</code>被绑定(赋值)给常量<code>someValue</code>。</p>
|
||||
<p>当一个变量或常量申明的左边是标识符模式时,此时,标识符模式是隐式的值绑定模式(value-binding pattern)。</p>
|
||||
<blockquote>
|
||||
<p>标识符模式语法<br><em>标识符模式</em> → <a href="LexicalStructure.html#identifier"><em>标识符</em></a> </p>
|
||||
@ -633,14 +635,15 @@
|
||||
<h2 id="-value-binding-pattern-">值绑定模式(Value-Binding Pattern)</h2>
|
||||
<p>值绑定模式绑定匹配的值到一个变量或常量。当绑定匹配值给常量时,用关键字<code>let</code>,绑定给变量时,用关键之<code>var</code>。</p>
|
||||
<p>标识符模式包含在值绑定模式中,绑定新的变量或常量到匹配的值。例如,你可以分解一个元组的元素,并把每个元素绑定到相应的标识符模式中。</p>
|
||||
<pre><code>let point = (3, 2)
|
||||
<pre><code class="lang-swift">let point = (3, 2)
|
||||
switch point {
|
||||
// Bind x and y to the elements of point.
|
||||
case let (x, y):
|
||||
println("The point is at (\(x), \(y)).")
|
||||
}
|
||||
// prints "The point is at (3, 2).”
|
||||
</code></pre><p>在上面这个例子中,<code>let</code>将元组模式<code>(x, y)</code>分配到各个标识符模式。因为这种行为,<code>switch</code>语句中<code>case let (x, y):</code>和<code>case (let x, let y):</code>匹配的值是一样的。</p>
|
||||
</code></pre>
|
||||
<p>在上面这个例子中,<code>let</code>将元组模式<code>(x, y)</code>分配到各个标识符模式。因为这种行为,<code>switch</code>语句中<code>case let (x, y):</code>和<code>case (let x, let y):</code>匹配的值是一样的。</p>
|
||||
<blockquote>
|
||||
<p>值绑定(Value Binding)模式语法<br><em>值绑定模式</em> → <strong>var</strong> <a href="..\chapter3\07_Patterns.html#pattern"><em>模式</em></a> | <strong>let</strong> <a href="..\chapter3\07_Patterns.html#pattern"><em>模式</em></a> </p>
|
||||
</blockquote>
|
||||
@ -649,16 +652,18 @@ case let (x, y):
|
||||
<p>元组模式是逗号分隔的列表,包含一个或多个模式,并包含在一对圆括号中。元组模式匹配相应元组类型的值。</p>
|
||||
<p>你可以使用类型注释来限制一个元组模式来匹配某种元组类型。例如,在常量申明<code>let (x, y): (Int, Int) = (1, 2)</code>中的元组模式<code>(x, y): (Int, Int)</code>,只匹配两个元素都是<code>Int</code>这种类型的元组。如果仅需要限制一个元组模式中的某几个元素,只需要直接对这几个元素提供类型注释即可。例如,在<code>let (x: String, y)</code>中的元组模式,只要某个元组类型是包含两个元素,且第一个元素类型是<code>String</code>,则被匹配。</p>
|
||||
<p>当元组模式被用在<code>for-in</code>语句或者变量或常量申明时,它可以包含通配符模式,标识符模式或者其他包含这两种模式的模式。例如,下面这段代码是不正确的,因为<code>(x, 0)</code>中的元素<code>0</code>是一个表达式模式:</p>
|
||||
<pre><code>let points = [(0, 0), (1, 0), (1, 1), (2, 0), (2, 1)]
|
||||
<pre><code class="lang-swift">let points = [(0, 0), (1, 0), (1, 1), (2, 0), (2, 1)]
|
||||
// This code isn't valid.
|
||||
for (x, 0) in points {
|
||||
/* ... */
|
||||
}
|
||||
</code></pre><p>对于只包含一个元素的元组,括号是不起作用的。模式匹配那个单个元素的类型。例如,下面是等效的:</p>
|
||||
<pre><code>let a = 2 // a: Int = 2
|
||||
</code></pre>
|
||||
<p>对于只包含一个元素的元组,括号是不起作用的。模式匹配那个单个元素的类型。例如,下面是等效的:</p>
|
||||
<pre><code class="lang-swift">let a = 2 // a: Int = 2
|
||||
let (a) = 2 // a: Int = 2
|
||||
let (a): Int = 2 // a: Int = 2
|
||||
</code></pre><blockquote>
|
||||
</code></pre>
|
||||
<blockquote>
|
||||
<p>元组模式语法<br><em>元组模式</em> → <strong>(</strong> <a href="..\chapter3\07_Patterns.html#tuple_pattern_element_list"><em>元组模式元素列表</em></a> <em>可选</em> <strong>)</strong><br><em>元组模式元素列表</em> → <a href="..\chapter3\07_Patterns.html#tuple_pattern_element"><em>元组模式元素</em></a> | <a href="..\chapter3\07_Patterns.html#tuple_pattern_element"><em>元组模式元素</em></a> <strong>,</strong> <a href="..\chapter3\07_Patterns.html#tuple_pattern_element_list"><em>元组模式元素列表</em></a><br><em>元组模式元素</em> → <a href="..\chapter3\07_Patterns.html#pattern"><em>模式</em></a> </p>
|
||||
</blockquote>
|
||||
<p><a name="enumeration_case_pattern"></a></p>
|
||||
@ -671,9 +676,10 @@ let (a): Int = 2 // a: Int = 2
|
||||
<p><a name="type-casting_patterns"></a></p>
|
||||
<h2 id="-type-casting-patterns-">类型转换模式(Type-Casting Patterns)</h2>
|
||||
<p>有两种类型转换模式,<code>is</code>模式和<code>as</code>模式。这两种模式均只出现在<code>switch</code>语句中的<code>case</code>标签中。<code>is</code>模式和<code>as</code>模式有以下形式:</p>
|
||||
<pre><code>is type
|
||||
pattern as type
|
||||
</code></pre><p><code>is</code>模式匹配一个值,如果这个值的类型在运行时(runtime)和<code>is</code>模式右边的指定类型(或者那个类型的子类)是一致的。<code>is</code>模式和<code>is</code>操作符一样,它们都进行类型转换,但是抛弃了返回的类型。</p>
|
||||
<blockquote>
|
||||
<p>is <code>type</code><br><code>pattern</code> as <code>type</code></p>
|
||||
</blockquote>
|
||||
<p><code>is</code>模式匹配一个值,如果这个值的类型在运行时(runtime)和<code>is</code>模式右边的指定类型(或者那个类型的子类)是一致的。<code>is</code>模式和<code>is</code>操作符一样,它们都进行类型转换,但是抛弃了返回的类型。</p>
|
||||
<p><code>as</code>模式匹配一个值,如果这个值的类型在运行时(runtime)和<code>as</code>模式右边的指定类型(或者那个类型的子类)是一致的。一旦匹配成功,匹配的值的类型被转换成<code>as</code>模式左边指定的模式。</p>
|
||||
<p>关于使用<code>switch</code>语句来匹配<code>is</code>模式和<code>as</code>模式值的例子,请参阅<code>Type Casting for Any and AnyObject</code>。</p>
|
||||
<blockquote>
|
||||
@ -683,7 +689,7 @@ pattern as type
|
||||
<h2 id="-expression-pattern-">表达式模式(Expression Pattern)</h2>
|
||||
<p>表达式模式代表了一个表达式的值。这个模式只出现在<code>switch</code>语句中的<code>case</code>标签中。</p>
|
||||
<p>由表达式模式所代表的表达式用Swift标准库中的<code>~=</code>操作符与输入表达式的值进行比较。如果<code>~=</code>操作符返回<code>true</code>,则匹配成功。默认情况下,<code>~=</code>操作符使用<code>==</code>操作符来比较两个相同类型的值。它也可以匹配一个整数值与一个<code>Range</code>对象中的整数范围,正如下面这个例子所示:</p>
|
||||
<pre><code>let point = (1, 2)
|
||||
<pre><code class="lang-swift">let point = (1, 2)
|
||||
switch point {
|
||||
case (0, 0):
|
||||
println("(0, 0) is at the origin.")
|
||||
@ -693,8 +699,9 @@ default:
|
||||
println("The point is at (\(point.0), \(point.1)).")
|
||||
}
|
||||
// prints "(1, 2) is near the origin.”
|
||||
</code></pre><p>你可以重载<code>~=</code>操作符来提供自定义的表达式行为。例如,你可以重写上面的例子,以实现用字符串表达的点来比较<code>point</code>表达式。</p>
|
||||
<pre><code>// Overload the ~= operator to match a string with an integer
|
||||
</code></pre>
|
||||
<p>你可以重载<code>~=</code>操作符来提供自定义的表达式行为。例如,你可以重写上面的例子,以实现用字符串表达的点来比较<code>point</code>表达式。</p>
|
||||
<pre><code class="lang-swift">// Overload the ~= operator to match a string with an integer
|
||||
func ~=(pattern: String, value: Int) -> Bool {
|
||||
return pattern == "\(value)"
|
||||
}
|
||||
@ -707,7 +714,8 @@ default:
|
||||
println("The point is at (\(point.0), \(point.1)).")
|
||||
}
|
||||
// prints "(1, 2) is near the origin.”
|
||||
</code></pre><blockquote>
|
||||
</code></pre>
|
||||
<blockquote>
|
||||
<p>表达式模式语法<br><em>表达式模式</em> → <a href="..\chapter3\04_Expressions.html#expression"><em>表达式</em></a> </p>
|
||||
</blockquote>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user