make gitbook

This commit is contained in:
numbbbbb
2014-06-15 13:04:11 +08:00
parent fca29b22b1
commit 9791e816fe
41 changed files with 461 additions and 565 deletions

View File

@ -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.1" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="3.1" 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,10 +587,11 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_637">
<section class="normal" id="section-gitbook_57">
<blockquote>
<p>翻译:ChildhoodAndy<br>校对numbbbbb</p>
<p>翻译:<a href="https://github.com/dabing1022" target="_blank">dabing1022</a>
校对:<a href="https://github.com/numbbbbb" target="_blank">numbbbbb</a></p>
</blockquote>
<h1 id="-">关于语言附注</h1>
<hr>
@ -603,8 +604,8 @@
<p><a name="how_to_read_the_grammar"></a></p>
<h2 id="-">如何阅读语法</h2>
<p>用来描述Swift编程语言形式语法的记法遵循下面几个约定</p>
<p>-](<a href="https://github.com/numbbbbb)箭头(→)用来标记语法产式,可以被理](https://github.com/numbbbbb)解为“可以包含”。" target="_blank">https://github.com/numbbbbb)箭头(→)用来标记语法产式,可以被理](https://github.com/numbbbbb)解为“可以包含”。</a></p>
<ul>
<li>箭头(→)用来标记语法产式,可以被理解为“可以包含”。</li>
<li>句法范畴由<em>斜体</em>文字表示,并出现在一个语法产式规则两侧。</li>
<li>义词和标点符号由粗体固定宽度的文本显示和只出现在一个语法产式规则的右边。</li>
<li>选择性的语法产式由竖线(|)分隔。当可选用的语法产式太多时,为了阅读方便,它们将被拆分为多行语法产式规则。</li>

View File

@ -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.2" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="3.2" 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_645">
<section class="normal" id="section-gitbook_65">
<blockquote>
<p>翻译superkam</p>
<p>校对:numbbbbb</p>
<p>翻译:<a href="https://github.com/superkam" target="_blank">superkam</a>
校对:<a href="https://github.com/numbbbbb" target="_blank">numbbbbb</a></p>
</blockquote>
<h1 id="-">词法结构</h1>
<hr>

View File

@ -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.3" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="3.3" 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,10 +587,11 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_639">
<section class="normal" id="section-gitbook_59">
<blockquote>
<p>翻译:lyuka<br>校对numbbbbb, stanzhai</p>
<p>翻译:<a href="https://github.com/lyuka" target="_blank">lyuka</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="-types-">类型Types</h1>
<hr>

View File

@ -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.4" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="3.4" 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,10 +587,11 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_641">
<section class="normal" id="section-gitbook_61">
<blockquote>
<p>翻译:sg552<br>校对numbbbbb, stanzhai</p>
<p>翻译:<a href="https://github.com/sg552" target="_blank">sg552</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="-expressions-">表达式Expressions</h1>
<hr>

View File

@ -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.6" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="3.6" 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,10 +587,11 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_643">
<section class="normal" id="section-gitbook_63">
<blockquote>
<p>翻译:marsprince<br>校对numbbbbb, stanzhai </p>
<p>翻译:<a href="https://github.com/marsprince" target="_blank">marsprince</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="-">声明</h1>
<hr>

View File

@ -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.7" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="3.7" 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_635">
<section class="normal" id="section-gitbook_55">
<blockquote>
<p>翻译Hawstein</p>
<p>校对:numbbbbb</p>
<p>翻译:<a href="https://github.com/Hawstein" target="_blank">Hawstein</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="-">特性</h1>
<hr>
@ -602,9 +602,10 @@
</ul>
<p>特性提供了关于声明和类型的更多信息。在Swift中有两类特性用于修饰声明的以及用于修饰类型的。例如<code>required</code>特性,当应用于一个类的指定或便利初始化器声明时,表明它的每个子类都必须实现那个初始化器。再比如<code>noreturn</code>特性,当应用于函数或方法类型时,表明该函数或方法不会返回到它的调用者。</p>
<p>通过以下方式指定一个特性:符号<code>@</code>后面跟特性名,如果包含参数,则把参数带上:</p>
<pre><code>@attribute name
@attribute name(attribute arguments)
</code></pre><p>有些声明特性通过接收参数来指定特性的更多信息以及它是如何修饰一个特定的声明的。这些特性的参数写在小括号内,它们的格式由它们所属的特性来定义。</p>
<blockquote>
<p>@<code>attribute name</code><br>@<code>attribute name</code>(<code>attribute arguments</code>) </p>
</blockquote>
<p>有些声明特性通过接收参数来指定特性的更多信息以及它是如何修饰一个特定的声明的。这些特性的参数写在小括号内,它们的格式由它们所属的特性来定义。</p>
<p><a name="declaration_attributes"></a></p>
<h2 id="-">声明特性</h2>
<p>声明特性只能应用于声明。然而,你也可以将<code>noreturn</code>特性应用于函数或方法类型。</p>
@ -631,7 +632,7 @@
<p>该特性用于修饰任意可以在Objective-C中表示的声明比如非嵌套类协议类和协议中的属性和方法包含getter和setter初始化器析构器以下下标。<code>objc</code>特性告诉编译器该声明可以在Objective-C代码中使用。</p>
<p>如果你将<code>objc</code>特性应用于一个类或协议,它也会隐式地应用于那个类或协议的成员。对于标记了<code>objc</code>特性的类,编译器会隐式地为它的子类添加<code>objc</code>特性。标记了<code>objc</code>特性的协议不能继承自没有标记<code>objc</code>的协议。</p>
<p><code>objc</code>特性有一个可选的参数,由标记符组成。当你想把<code>objc</code>所修饰的实体以一个不同的名字暴露给Objective-C你就可以使用这个特性参数。你可以使用这个参数来命名类协议方法getterssetters以及初始化器。下面的例子把<code>ExampleClass</code><code>enabled</code>属性的getter暴露给Objective-C名字是<code>isEnabled</code>,而不是它原来的属性名。</p>
<pre><code>@objc
<pre><code class="lang-swift">@objc
class ExampleClass {
var enabled: Bool {
@objc(isEnabled) get {
@ -639,7 +640,8 @@ class ExampleClass {
}
}
}
</code></pre><p><code>optional</code></p>
</code></pre>
<p><code>optional</code></p>
<p>用该特性修饰协议的属性方法或下标成员表示实现这些成员并不需要一致性类型conforming type</p>
<p>你只能用<code>optional</code>特性修饰那些标记了<code>objc</code>特性的协议。因此只有类类型可以adopt和comform to那些包含可选成员需求的协议。更多关于如何使用<code>optional</code>特性以及如何访问可选协议成员的指导例如当你不确定一个conforming类型是否实现了它们请见<a href="">可选协议需求</a></p>
<p><code>required</code></p>
@ -656,7 +658,7 @@ class ExampleClass {
<p><code>noreturn</code></p>
<p>该特性用于修饰函数或方法的类型,表明该函数或方法不会返回到它的调用者中去。你也可以用它标记函数或方法的声明,表示函数或方法的相应类型,<code>T</code>,是<code>@noreturn T</code></p>
<blockquote>
<p>特性语法<br><em></em><strong>@</strong> <a href="..\chapter3\06_Attributes.html#attribute_name"><em>特性名</em></a> <a href="..\chapter3\06_Attributes.html#attribute_argument_clause"><em>特性参数子句</em></a> <em>可选</em><br><em>特性名</em><a href="LexicalStructure.html#identifier"><em>标识符</em></a><br><em>特性参数子句</em><strong>(</strong> <a href="..\chapter3\06_Attributes.html#balanced_tokens"><em>平衡令牌列表</em></a> <em>可选</em> <strong>)</strong><br><em>特性(Attributes)列表</em><a href="..\chapter3\06_Attributes.html#attribute"><em>特色</em></a> <a href="..\chapter3\06_Attributes.html#attributes"><em>特性(Attributes)列表</em></a> <em>可选</em><br><em>平衡令牌列表</em><a href="..\chapter3\06_Attributes.html#balanced_token"><em>平衡令牌</em></a> <a href="..\chapter3\06_Attributes.html#balanced_tokens"><em>平衡令牌列表</em></a> <em>可选</em><br><em>平衡令牌</em><strong>(</strong> <a href="..\chapter3\06_Attributes.html#balanced_tokens"><em>平衡令牌列表</em></a> <em>可选</em> <strong>)</strong><br><em>平衡令牌</em><strong>[</strong> <a href="..\chapter3\06_Attributes.html#balanced_tokens"><em>平衡令牌列表</em></a> <em>可选</em> <strong>]</strong><br><em>平衡令牌</em><strong>{</strong> <a href="..\chapter3\06_Attributes.html#balanced_tokens"><em>平衡令牌列表</em></a> <em>可选</em> <strong>}</strong><br><em>平衡令牌</em><strong>任意标识符, 关键字, 字面量或运算符</strong><br><em>平衡令牌</em><strong>任意标点除了(, ), [, ], {, 或 }</strong></p>
<p>特性语法<br><em></em><strong>@</strong> <a href="..\chapter3\06_Attributes.html#attribute_name"><em>特性名</em></a> <a href="..\chapter3\06_Attributes.html#attribute_argument_clause"><em>特性参数子句</em></a> <em>可选</em><br><em>特性名</em><a href="LexicalStructure.html#identifier"><em>标识符</em></a><br><em>特性参数子句</em><strong>(</strong> <a href="..\chapter3\06_Attributes.html#balanced_tokens"><em>平衡令牌列表</em></a> <em>可选</em> <strong>)</strong><br><em>特性(Attributes)列表</em><a href="..\chapter3\06_Attributes.html#attribute"><em>特色</em></a> <a href="..\chapter3\06_Attributes.html#attributes"><em>特性(Attributes)列表</em></a> <em>可选</em><br><em>平衡令牌列表</em><a href="..\chapter3\06_Attributes.html#balanced_token"><em>平衡令牌</em></a> <a href="..\chapter3\06_Attributes.html#balanced_tokens"><em>平衡令牌列表</em></a> <em>可选</em><br><em>平衡令牌</em><strong>(</strong> <a href="..\chapter3\06_Attributes.html#balanced_tokens"><em>平衡令牌列表</em></a> <em>可选</em> <strong>)</strong><br><em>平衡令牌</em><strong>[</strong> <a href="..\chapter3\06_Attributes.html#balanced_tokens"><em>平衡令牌列表</em></a> <em>可选</em> <strong>]</strong><br><em>平衡令牌</em><strong>{</strong> <a href="..\chapter3\06_Attributes.html#balanced_tokens"><em>平衡令牌列表</em></a> <em>可选</em> <strong>}</strong><br><em>平衡令牌</em><strong>任意标识符, 关键字, 字面量或运算符</strong><br><em>平衡令牌</em><strong>任意标点除了(, ), [, ], {, 或 }</strong></p>
</blockquote>

View File

@ -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(&quot;The point is at (\(x), \(y)).&quot;)
}
// prints &quot;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&#39;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(&quot;(0, 0) is at the origin.&quot;)
@ -693,8 +699,9 @@ default:
println(&quot;The point is at (\(point.0), \(point.1)).&quot;)
}
// prints &quot;(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) -&gt; Bool {
return pattern == &quot;\(value)&quot;
}
@ -707,7 +714,8 @@ default:
println(&quot;The point is at (\(point.0), \(point.1)).&quot;)
}
// prints &quot;(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>

View File

@ -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.9" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="3.9" 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_649">
<section class="normal" id="section-gitbook_69">
<blockquote>
<p>翻译fd5788</p>
<p>校对:yankuangshi</p>
<p>翻译:<a href="https://github.com/fd5788" target="_blank">fd5788</a>
校对:<a href="https://github.com/yankuangshi" target="_blank">yankuangshi</a>, <a href="https://github.com/stanzhai" target="_blank">stanzhai</a></p>
</blockquote>
<h1 id="-">泛型参数</h1>
<hr>
@ -605,22 +605,27 @@
<p><a name="generic_parameter"></a></p>
<h2 id="-">泛型形参子句</h2>
<p>泛型形参子句指定泛型类型或函数的类型形参,以及这些参数的关联约束和要求。泛型形参子句用尖括号(&lt;&gt;)包住,并且有以下两种形式:</p>
<pre><code>&lt;generic parameter list&gt;
&lt;generic parameter list where requirements &gt;
</code></pre><p>泛型形参列表中泛型形参用逗号分开,每一个采用以下形式:</p>
<pre><code>type parameter : constrain
</code></pre><p>泛型形参由两部分组成类型形参及其后的可选约束。类型形参只是占位符类型如TUVKeyTypeValueType等的名字而已。你可以在泛型类型、函数的其余部分或者构造器声明以及函数或构造器的签名中使用它。</p>
<blockquote>
<p>&lt;<code>generic parameter list</code>&gt;<br>&lt;<code>generic parameter list</code> where <code>requirements</code>&gt;</p>
</blockquote>
<p>泛型形参列表中泛型形参用逗号分开,每一个采用以下形式:</p>
<blockquote>
<p><code>type parameter</code> : <code>constrain</code></p>
</blockquote>
<p>泛型形参由两部分组成类型形参及其后的可选约束。类型形参只是占位符类型如TUVKeyTypeValueType等的名字而已。你可以在泛型类型、函数的其余部分或者构造器声明以及函数或构造器的签名中使用它。</p>
<p>约束用于指明该类型形参继承自某个类或者遵守某个协议或协议的一部分。例如,在下面的泛型中,泛型形参<code>T: Comparable</code>表示任何用于替代类型形参<code>T</code>的类型实参必须满足<code>Comparable</code>协议。</p>
<pre><code>func simpleMin&lt;T: COmparable&gt;(x: T, y: T) -&gt; T {
<pre><code class="lang-swift">func simpleMin&lt;T: COmparable&gt;(x: T, y: T) -&gt; T {
if x &lt; y {
return y
}
return x
}
</code></pre><p>如,<code>Int</code><code>Double</code>均满足<code>Comparable</code>协议,该函数接受任何一种类型。与泛型类型相反,调用泛型函数或构造器时不需要指定泛型实参子句。类型实参由传递给函数或构造器的实参推断而出。</p>
<pre><code>simpleMin(17, 42) // T is inferred to be Int
</code></pre>
<p>如,<code>Int</code><code>Double</code>均满足<code>Comparable</code>协议,该函数接受任何一种类型。与泛型类型相反,调用泛型函数或构造器时不需要指定泛型实参子句。类型实参由传递给函数或构造器的实参推断而出。</p>
<pre><code class="lang-swift">simpleMin(17, 42) // T is inferred to be Int
simpleMin(3.14159, 2.71828) // T is inferred to be Double
</code></pre><h2 id="where-">Where 子句</h2>
</code></pre>
<h2 id="where-">Where 子句</h2>
<p>要想对类型形参及其关联类型指定额外要求,可以在泛型形参列表之后添加<code>where</code>子句。<code>where</code>子句由关键字<code>where</code>及其后的用逗号分割的多个要求组成。</p>
<p><code>where</code>子句中的要求用于指明该类型形参继承自某个类或遵守某个协议或协议的一部分。尽管<code>where</code>子句有助于表达类型形参上的简单约束(如<code>T: Comparable</code>等同于<code>T where T: Comparable</code>,等等),但是依然可以用来对类型形参及其关联约束提供更复杂的约束。如,<code>&lt;T where T: C, T: P&gt;</code>表示泛型类型<code>T</code>继承自类<code>C</code>且遵守协议<code>P</code></p>
<p>如上所述,可以强制约束类型形参的关联类型遵守某个协议。<code>&lt;T: Generator where T.Element: Equatable&gt;</code>表示<code>T</code>遵守<code>Generator</code>协议,而且<code>T</code>的关联类型<code>T.Element</code>遵守<code>Eauatable</code>协议(<code>T</code>有关联类型是因为<code>Generator</code>声明了<code>Element</code>,而<code>T</code>遵守<code>Generator</code>协议)。</p>
@ -634,15 +639,19 @@ simpleMin(3.14159, 2.71828) // T is inferred to be Double
<p><a name="generic_argument"></a></p>
<h2 id="-">泛型实参子句</h2>
<p>泛型实参子句指定<em>泛型类型</em>的类型实参。泛型实参子句用尖括号(&lt;&gt;)包住,形式如下:</p>
<pre><code>&lt; generic argument list &gt;
</code></pre><p>泛型实参列表中类型实参有逗号分开。类型实参是实际具体类型的名字用来替代泛型类型的泛型形参子句中的相应的类型形参。从而得到泛型类型的一个特化版本。如Swift标准库的泛型字典类型定义如下</p>
<pre><code>struct Dictionary&lt;KeyTypel: Hashable, ValueType&gt;: Collection, DictionaryLiteralConvertible {
<blockquote>
<p>&lt;<code>generic argument list</code>&gt;</p>
</blockquote>
<p>泛型实参列表中类型实参有逗号分开。类型实参是实际具体类型的名字用来替代泛型类型的泛型形参子句中的相应的类型形参。从而得到泛型类型的一个特化版本。如Swift标准库的泛型字典类型定义如下</p>
<pre><code class="lang-swift">struct Dictionary&lt;KeyTypel: Hashable, ValueType&gt;: Collection, DictionaryLiteralConvertible {
/* .. */
}
</code></pre><p>泛型<code>Dictionary</code>类型的特化版本,<code>Dictionary&lt;String, Int&gt;</code>就是用具体的<code>String</code><code>Int</code>类型替代泛型类型<code>KeyType: Hashable</code><code>ValueType</code>产生的。每一个类型实参必须满足它所替代的泛型形参的所有约束,包括任何<code>where</code>子句所指定的额外的要求。上面的例子中,类型形参<code>KeyType</code>要求满足<code>Hashable</code>协议,因此<code>String</code>也必须满足<code>Hashable</code>协议。</p>
</code></pre>
<p>泛型<code>Dictionary</code>类型的特化版本,<code>Dictionary&lt;String, Int&gt;</code>就是用具体的<code>String</code><code>Int</code>类型替代泛型类型<code>KeyType: Hashable</code><code>ValueType</code>产生的。每一个类型实参必须满足它所替代的泛型形参的所有约束,包括任何<code>where</code>子句所指定的额外的要求。上面的例子中,类型形参<code>KeyType</code>要求满足<code>Hashable</code>协议,因此<code>String</code>也必须满足<code>Hashable</code>协议。</p>
<p>可以用本身就是泛型类型的特化版本的类型实参替代类型形参(假设已满足合适的约束和要求)。例如,为了生成一个元素类型是整型数组的数组,可以用数组的特化版本<code>Array&lt;Int&gt;</code>替代泛型类型<code>Array&lt;T&gt;</code>的类型形参<code>T</code>来实现。</p>
<pre><code>let arrayOfArrays: Array&lt;Array&lt;Int&gt;&gt; = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
</code></pre><p><a href="#generic_parameter">泛型形参子句</a>所述,不能用泛型实参子句来指定泛型函数或构造器的类型实参。</p>
<pre><code class="lang-swift">let arrayOfArrays: Array&lt;Array&lt;Int&gt;&gt; = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
</code></pre>
<p><a href="#generic_parameter">泛型形参子句</a>所述,不能用泛型实参子句来指定泛型函数或构造器的类型实参。</p>
<blockquote>
<p>泛型实参子句语法<br><em>(泛型参数子句Generic Argument Clause)</em><strong>&lt;</strong> <a href="GenericParametersAndArguments.html#generic_argument_list"><em>泛型参数列表</em></a> <strong>&gt;</strong><br><em>泛型参数列表</em><a href="GenericParametersAndArguments.html#generic_argument"><em>泛型参数</em></a> | <a href="GenericParametersAndArguments.html#generic_argument"><em>泛型参数</em></a> <strong>,</strong> <a href="GenericParametersAndArguments.html#generic_argument_list"><em>泛型参数列表</em></a><br><em>泛型参数</em><a href="..\chapter3\03_Types.html#type"><em>类型</em></a> </p>
</blockquote>

File diff suppressed because one or more lines are too long

View File

@ -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.5" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="3.5" 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_653">
<section class="normal" id="section-gitbook_73">
<blockquote>
<p>翻译coverxit</p>
<p>校对:numbbbbb, coverxit</p>
<p>翻译:<a href="https://github.com/coverxit" target="_blank">coverxit</a>
校对:<a href="https://github.com/numbbbbb" target="_blank">numbbbbb</a>, <a href="https://github.com/coverxit" target="_blank">coverxit</a>, <a href="https://github.com/stanzhai" target="_blank">stanzhai</a></p>
</blockquote>
<h1 id="-">语句</h1>
<hr>
@ -606,162 +606,113 @@
<p>循环语句用于重复执行代码块;分支语句用于执行满足特定条件的代码块;控制传递语句则用于修改代码的执行顺序。在稍后的叙述中,将会详细地介绍每一种类型的控制流语句。</p>
<p>是否将分号(<code>;</code>)添加到语句的结尾处是可选的。但若要在同一行内写多条独立语句,请务必使用分号。</p>
<blockquote>
<p>GRAMMAR OF A STATEMENT</p>
<p><em>statement</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a><strong>;</strong> <em>opt</em></p>
<p><em>statement</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration" target="_blank"><em>declaration</em></a><strong>;</strong> <em>opt</em></p>
<p><em>statement</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/loop-statement" target="_blank"><em>loop-statement</em></a><strong>;</strong> <em>opt</em></p>
<p><em>statement</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/branch-statement" target="_blank"><em>branch-statement</em></a><strong>;</strong> <em>opt</em></p>
<p><em>statement</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/labeled-statement" target="_blank"><em>labeled-statement</em></a></p>
<p><em>statement</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/control-transfer-statement" target="_blank"><em>control-transfer-statement</em></a><strong>;</strong> <em>opt</em></p>
<p><em>statement</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement" target="_blank"><em>statment</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements" target="_blank"><em>statements</em></a><strong>;</strong> <em>opt</em></p>
<p>语句语法<br><em>语句</em><a href="..\chapter3\04_Expressions.html#expression"><em>表达式</em></a> <strong>;</strong> <em>可选</em><br><em>语句</em><a href="..\chapter3\05_Declarations.html#declaration"><em>声明</em></a> <strong>;</strong> <em>可选</em><br><em>语句</em><a href="..\chapter3\10_Statements.html#loop_statement"><em>循环语句</em></a> <strong>;</strong> <em>可选</em><br><em>语句</em><a href="..\chapter3\10_Statements.html#branch_statement"><em>分支语句</em></a> <strong>;</strong> <em>可选</em><br><em>语句</em><a href="..\chapter3\10_Statements.html#labeled_statement"><em>标记语句(Labeled Statement)</em></a><br><em>语句</em><a href="..\chapter3\10_Statements.html#control_transfer_statement"><em>控制转移语句</em></a> <strong>;</strong> <em>可选</em><br><em>多条语句(Statements)</em><a href="..\chapter3\10_Statements.html#statement"><em>语句</em></a> <a href="..\chapter3\10_Statements.html#statements"><em>多条语句(Statements)</em></a> <em>可选</em> </p>
</blockquote>
<p><a name="loop_statements"></a></p>
<h2 id="-">循环语句</h2>
<p>取决于特定的循环条件循环语句允许重复执行代码块。Swift 提供四种类型的循环语句:<code>for</code>语句、<code>for-in</code>语句、<code>while</code>语句和<code>do-while</code>语句。</p>
<p>通过<code>break</code>语句和<code>continue</code>语句可以改变循环语句的控制流。有关这两条语句,详情参见 <a href="#break_statement">Break 语句</a><a href="#continue_statement">Continue 语句</a></p>
<blockquote>
<p>GRAMMAR OF A LOOP STATEMENT</p>
<p><em>loop-statement</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-statement" target="_blank"><em>for-statement</em></a></p>
<p><em>loop-statement</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-in-statement" target="_blank"><em>for-in-statement</em></a></p>
<p><em>loop-statement</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-statement" target="_blank"><em>while-statement</em></a></p>
<p><em>loop-statement</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/do-while-statement" target="_blank"><em>do-while-statement</em></a></p>
<p>循环语句语法<br><em>循环语句</em><a href="..\chapter3\10_Statements.html#for_statement"><em>for语句</em></a><br><em>循环语句</em><a href="..\chapter3\10_Statements.html#for_in_statement"><em>for-in语句</em></a><br><em>循环语句</em><a href="..\chapter3\10_Statements.html#wheetatype类型ile_statement"><em>while语句</em></a><br><em>循环语句</em><a href="..\chapter3\10_Statements.html#do_while_statement"><em>do-while语句</em></a> </p>
</blockquote>
<h3 id="for-">For 语句</h3>
<p><code>for</code>语句允许在重复执行代码块的同时,递增一个计数器。</p>
<p><code>for</code>语句的形式如下:</p>
<pre><code class="lang-swift">for `initialzation`; `condition`; `increment` {
`statements`
}
</code></pre>
<blockquote>
<p>for <code>initialzation</code>; <code>condition</code>; <code>increment</code> {<br> <code>statements</code><br>} </p>
</blockquote>
<p><em>initialzation</em><em>condition</em><em>increment</em> 之间的分号,以及包围循环体 <em>statements</em> 的大括号都是不可省略的。</p>
<p><code>for</code>语句的执行流程如下:</p>
<ol>
<li><p><em>initialzation</em> 只会被执行一次,通常用于声明和初始化在接下来的循环中需要使用的变量。</p>
</li>
<li><p>计算 <em>condition</em> 表达式:
如果为<code>true</code><em>statements</em> 将会被执行,然后转到第3步。如果为<code>false</code><em>statements</em><em>increment</em> 都不会被执行,<code>for</code>至此执行完毕。</p>
</li>
<li><p>计算 <em>increment</em> 表达式然后转到第2步。</p>
</li>
<li><em>initialzation</em> 只会被执行一次,通常用于声明和初始化在接下来的循环中需要使用的变量。</li>
<li>计算 <em>condition</em> 表达式:
如果为<code>true</code><em>statements</em> 将会被执行然后转到第3步。如果为<code>false</code><em>statements</em><em>increment</em> 都不会被执行,<code>for</code>至此执行完毕。</li>
<li>计算 <em>increment</em> 表达式,然后转到第2步。</li>
</ol>
<p>定义在 <em>initialzation</em> 中的变量仅在<code>for</code>语句的作用域以内有效。<em>condition</em> 表达式的值的类型必须遵循<code>LogicValue</code>协议。</p>
<blockquote>
<p>GRAMMAR OF A FOR STATEMENT</p>
<p><em>for-statement</em><strong>for</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-init" target="_blank"><em>for-init</em></a> <em>opt</em> <strong>;</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> <em>opt</em> <strong>;</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> <em>opt</em> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target="_blank"><em>code-block</em></a></p>
<p><em>for-statement</em><strong>for (</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-init" target="_blank"><em>for-init</em></a> <em>opt</em> <strong>;</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> <em>opt</em> <strong>;</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> <em>opt</em> <strong>)</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target="_blank"><em>code-block</em></a></p>
<p><em>for-statement</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-declaration" target="_blank"><em>variable-declaration</em></a> | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression-list" target="_blank"><em>expression-list</em></a></p>
<p>For 循环语法<br><em>for语句</em><strong>for</strong> <a href="..\chapter3\10_Statements.html#for_init"><em>for初始条件</em></a> <em>可选</em> <strong>;</strong> <a href="..\chapter3\04_Expressions.html#expression"><em>表达式</em></a> <em>可选</em> <strong>;</strong> <a href="..\chapter3\04_Expressions.html#expression"><em>表达式</em></a> <em>可选</em> <a href="..\chapter3\05_Declarations.html#code_block"><em>代码块</em></a><br><em>for语句</em><strong>for</strong> <strong>(</strong> <a href="..\chapter3\10_Statements.html#for_init"><em>for初始条件</em></a> <em>可选</em> <strong>;</strong> <a href="..\chapter3\04_Expressions.html#expression"><em>表达式</em></a> <em>可选</em> <strong>;</strong> <a href="..\chapter3\04_Expressions.html#expression"><em>表达式</em></a> <em>可选</em> <strong>)</strong> <a href="..\chapter3\05_Declarations.html#code_block"><em>代码块</em></a><br><em>for初始条件</em><a href="..\chapter3\05_Declarations.html#variable_declaration"><em>变量声明</em></a> | <a href="..\chapter3\04_Expressions.html#expression_list"><em>表达式列表</em></a> </p>
</blockquote>
<h3 id="for-in-">For-In 语句</h3>
<p><code>for-in</code>语句允许在重复执行代码块的同时,迭代集合(或遵循<code>Sequence</code>协议的任意类型)中的每一项。</p>
<p><code>for-in</code>语句的形式如下:</p>
<pre><code class="lang-swift">for `item` in `collection` {
`statements`
}
</code></pre>
<blockquote>
<p>for <code>item</code> in <code>collection</code> {<br> <code>statements</code><br>} </p>
</blockquote>
<p><code>for-in</code>语句在循环开始前会调用 <em>collection</em> 表达式的<code>generate</code>方法来获取一个生成器类型(这是一个遵循<code>Generator</code>协议的类型)的值。接下来循环开始,调用 <em>collection</em> 表达式的<code>next</code>方法。如果其返回值不是<code>None</code>,它将会被赋给 <em>item</em>,然后执行 <em>statements</em>,执行完毕后回到循环开始处;否则,将不会赋值给 <em>item</em> 也不会执行 <em>statements</em><code>for-in</code>至此执行完毕。</p>
<blockquote>
<p>GRAMMAR OF A FOR-IN STATEMENT</p>
<p><em>for-in-statement</em><strong>for</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern" target="_blank"><em>pattern</em></a> <strong>in</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target="_blank"><em>code-block</em></a></p>
<p>For-In 循环语法<br><em>for-in语句</em><strong>for</strong> <a href="..\chapter3\07_Patterns.html#pattern"><em>模式</em></a> <strong>in</strong> <a href="..\chapter3\04_Expressions.html#expression"><em>表达式</em></a> <a href="..\chapter3\05_Declarations.html#code_block"><em>代码块</em></a> </p>
</blockquote>
<h3 id="while-">While 语句</h3>
<p><code>while</code>语句允许重复执行代码块。</p>
<p><code>while</code>语句的形式如下:</p>
<pre><code class="lang-swift">while `condition` {
`statements`
}
</code></pre>
<blockquote>
<p>while <code>condition</code> {<br> <code>statements</code><br>} </p>
</blockquote>
<p><code>while</code>语句的执行流程如下:</p>
<ol>
<li><p>计算 <em>condition</em> 表达式:
如果为真<code>true</code>转到第2步。如果为<code>false</code><code>while</code>至此执行完毕。</p>
</li>
<li><p>执行 <em>statements</em> 然后转到第1步。</p>
</li>
<li>计算 <em>condition</em> 表达式:
如果为真<code>true</code>转到第2步。如果为<code>false</code><code>while</code>至此执行完毕。</li>
<li>执行 <em>statements</em> 然后转到第1步。</li>
</ol>
<p>由于 <em>condition</em> 的值在 <em>statements</em> 执行前就已计算出,因此<code>while</code>语句中的 <em>statements</em> 可能会被执行若干次,也可能不会被执行。</p>
<p><em>condition</em> 表达式的值的类型必须遵循<code>LogicValue</code>协议。同时,<em>condition</em> 表达式也可以使用可选绑定,详情参见<a href="../chapter2/01_The_Basics.html#optional_binding">可选绑定</a></p>
<blockquote>
<p>GRAMMAR OF A WHILE STATEMENT</p>
<p><em>while-statement</em><strong>while</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-condition" target="_blank"><em>while-condition</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target="_blank"><em>code-block</em></a></p>
<p><em>while-condition</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration" target="_blank"><em>declaration</em></a></p>
<p>While 循环语法<br><em>while语句</em><strong>while</strong> <a href="..\chapter3\10_Statements.html#while_condition"><em>while条件</em></a> <a href="..\chapter3\05_Declarations.html#code_block"><em>代码块</em></a><br><em>while条件</em><a href="..\chapter3\04_Expressions.html#expression"><em>表达式</em></a> | <a href="..\chapter3\05_Declarations.html#declaration"><em>声明</em></a> </p>
</blockquote>
<h3 id="do-while-">Do-While 语句</h3>
<p><code>do-while</code>语句允许代码块被执行一次或多次。</p>
<p><code>do-while</code>语句的形式如下:</p>
<pre><code class="lang-swift">do {
`statements`
} while `condition`
</code></pre>
<blockquote>
<p>do {<br> <code>statements</code><br>} while <code>condition</code> </p>
</blockquote>
<p><code>do-while</code>语句的执行流程如下:</p>
<ol>
<li><p>执行 <em>statements</em>然后转到第2步。</p>
</li>
<li><p>计算 <em>condition</em> 表达式:
如果为<code>true</code>转到第1步。如果为<code>false</code><code>do-while</code>至此执行完毕。</p>
</li>
<li>执行 <em>statements</em>然后转到第2步。</li>
<li>计算 <em>condition</em> 表达式:
如果为<code>true</code>转到第1步。如果为<code>false</code><code>do-while</code>至此执行完毕。</li>
</ol>
<p>由于 <em>condition</em> 表达式的值是在 <em>statements</em> 执行后才计算出,因此<code>do-while</code>语句中的 <em>statements</em> 至少会被执行一次。</p>
<p><em>condition</em> 表达式的值的类型必须遵循<code>LogicValue</code>协议。同时,<em>condition</em> 表达式也可以使用可选绑定,详情参见<a href="../chapter2/01_The_Basics.html#optional_binding">可选绑定</a></p>
<blockquote>
<p>GRAMMAR OF A DO-WHILE STATEMENT</p>
<p><em>do-while-statement</em><strong>do</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target="_blank"><em>code-block</em></a> <strong>while</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-condition" target="_blank"><em>while-condition</em></a></p>
<p>Do-While 循环语法<br><em>do-while语句</em><strong>do</strong> <a href="..\chapter3\05_Declarations.html#code_block"><em>代码块</em></a> <strong>while</strong> <a href="..\chapter3\10_Statements.html#while_condition"><em>while条件</em></a> </p>
</blockquote>
<p><a name="branch_statements"></a></p>
<h2 id="-">分支语句</h2>
<p>取决于一个或者多个条件的值分支语句允许程序执行指定部分的代码。显然分支语句中条件的值将会决定如何分支以及执行哪一块代码。Swift 提供两种类型的分支语句:<code>if</code>语句和<code>switch</code>语句。</p>
<p><code>switch</code>语句中的控制流可以用<code>break</code>语句修改,详情请见<a href="#break_statement">Break 语句</a></p>
<blockquote>
<p>GRAMMAR OF A BRANCH STATEMENT</p>
<p><em>branch-statement</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-statement" target="_blank"><em>if-statement</em></a></p>
<p><em>branch-statement</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-statement" target="_blank"><em>switch-statement</em></a></p>
<p>分支语句语法<br><em>分支语句</em><a href="..\chapter3\10_Statements.html#if_statement"><em>if语句</em></a><br><em>分支语句</em><a href="..\chapter3\10_Statements.html#switch_statement"><em>switch语句</em></a> </p>
</blockquote>
<h3 id="if-">If 语句</h3>
<p>取决于一个或多个条件的值,<code>if</code>语句将决定执行哪一块代码。</p>
<p><code>if</code>语句有两种标准形式,在这两种形式里都必须有大括号。</p>
<p>第一种形式是当且仅当条件为真时执行代码,像下面这样:</p>
<pre><code class="lang-swift">if `condition` {
`statements`
}
</code></pre>
<blockquote>
<p>if <code>condition</code> {<br> <code>statements</code><br>} </p>
</blockquote>
<p>第二种形式是在第一种形式的基础上添加 <em>else 语句</em>,当只有一个 else 语句时,像下面这样:</p>
<pre><code class="lang-swift">if `condition` {
`statements to execute if condition is true`
<blockquote>
<p>if <code>condition</code> {
<code>statements to execute if condition is true</code>
} else {
`statements to execute if condition is false`
}
</code></pre>
<code>statements to execute if condition is false</code>
}</p>
</blockquote>
<p>同时else 语句也可包含<code>if</code>语句,从而形成一条链来测试更多的条件,像下面这样:</p>
<pre><code class="lang-swift">if `condition 1` {
`statements to execute if condition 1 is true`
} else if `condition 2` {
`statements to execute if condition 2 is true`
}
else {
`statements to execute if both conditions are false`
}
</code></pre>
<blockquote>
<p>if <code>condition 1</code> {<br> <code>statements to execute if condition 1 is true</code><br>} else if <code>condition 2</code> {<br> <code>statements to execute if condition 2 is true</code><br>}<br>else {<br> <code>statements to execute if both conditions are false</code><br>} </p>
</blockquote>
<p><code>if</code>语句中条件的值的类型必须遵循<code>LogicValue</code>协议。同时,条件也可以使用可选绑定,详情参见<a href="../chapter2/01_The_Basics.html#optional_binding">可选绑定</a></p>
<blockquote>
<p>GRAMMAR OF AN IF STATEMENT</p>
<p><em>if-statement</em><strong>if</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-condition" target="_blank"><em>if-condition</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target="_blank"><em>code-block</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/else-clause" target="_blank"><em>else-clause</em></a> <em>opt</em></p>
<p><em>if-condition</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration" target="_blank"><em>declaration</em></a></p>
<p><em>else-clause</em><strong>else</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target="_blank"><em>code-block</em></a> | <strong>else</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-statement" target="_blank"><em>if-statement</em></a> <em>opt</em></p>
<p>If语句语法<br><em>if语句</em><strong>if</strong> <a href="..\chapter3\10_Statements.html#if_condition"><em>if条件</em></a> <a href="..\chapter3\05_Declarations.html#code_block"><em>代码块</em></a> <a href="..\chapter3\10_Statements.html#else_clause"><em>else子句(Clause)</em></a> <em>可选</em><br><em>if条件</em><a href="..\chapter3\04_Expressions.html#expression"><em>表达式</em></a> | <a href="..\chapter3\05_Declarations.html#declaration"><em>声明</em></a><br><em>else子句(Clause)</em><strong>else</strong> <a href="..\chapter3\05_Declarations.html#code_block"><em>代码块</em></a> | <strong>else</strong> <a href="..\chapter3\10_Statements.html#if_statement"><em>if语句</em></a> </p>
</blockquote>
<h3 id="switch-">Switch 语句</h3>
<p>取决于<code>switch</code>语句的<em>控制表达式control expression</em><code>switch</code>语句将决定执行哪一块代码。</p>
<p><code>switch</code>语句的形式如下:</p>
<pre><code class="lang-swift">switch `control expression` {
case `pattern 1`:
`statements`
case `pattern 2` where `condition`:
`statements`
case `pattern 3` where `condition`,
`pattern 4` where `condition`:
`statements`
default:
`statements`
}
</code></pre>
<blockquote>
<p>switch <code>control expression</code> {<br> case <code>pattern 1</code>:<br> <code>statements</code><br> case <code>pattern 2</code> where <code>condition</code>:<br> <code>statements</code><br> case <code>pattern 3</code> where <code>condition</code>,<br> <code>pattern 4</code> where <code>condition</code>:<br> <code>statements</code><br> default:<br> <code>statements</code><br>} </p>
</blockquote>
<p><code>switch</code>语句的<em>控制表达式control expression</em>会首先被计算,然后与每一个 case 的模式pattern进行匹配。如果匹配成功程序将会执行对应的 case 分支里的 <em>statements</em>。另外,每一个 case 分支都不能为空,也就是说在每一个 case 分支中至少有一条语句。如果你不想在匹配到的 case 分支中执行代码,只需在该分支里写一条<code>break</code>语句即可。</p>
<p>可以用作控制表达式的值是十分灵活的,除了标量类型(scalar types<code>Int</code><code>Character</code>)外你可以使用任何类型的值包括浮点数、字符串、元组、自定义类的实例和可选optional类型甚至是枚举类型中的成员值和指定的范围(range)等。关于在<code>switch</code>语句中使用这些类型,详情参见<a href="../chapter2/05_Control_Flow.html">控制流</a>一章的 <a href="../chapter2/05_Control_Flow.html#switch">Switch</a></p>
<p>你可以在模式后面添加一个起保护作用的表达式(guard expression)。<em>起保护作用的表达式</em>是这样构成的:关键字<code>where</code>后面跟着一个作为额外测试条件的表达式。因此,当且仅当<em>控制表达式</em>匹配一个<em>case</em>的某个模式且起保护作用的表达式为真时,对应 case 分支中的 <em>statements</em> 才会被执行。在下面的例子中,<em>控制表达式</em>只会匹配含两个相等元素的元组,如<code>(1, 1)</code></p>
@ -775,16 +726,7 @@ else {
<h4 id="-fall-through-">不存在隐式的贯穿(fall through)</h4>
<p>当匹配的 case 分支中的代码执行完毕后,程序会终止<code>switch</code>语句,而不会继续执行下一个 case 分支。这就意味着,如果你想执行下一个 case 分支,需要显式地在你需要的 case 分支里使用<code>fallthrough</code>语句。关于<code>fallthrough</code>语句的更多信息,详情参见 <a href="#fallthrough_statement">Fallthrough 语句</a></p>
<blockquote>
<p>GRAMMAR OF A SWITCH STATEMENT</p>
<p><em>switch-statement</em><strong>switch</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> <strong>{</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-cases" target="_blank"><em>switch-cases</em></a> <em>opt</em> <strong>}</strong></p>
<p><em>switch-cases</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-case" target="_blank"><em>switch-case</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-cases" target="_blank"><em>switch-cases</em></a> <em>opt</em></p>
<p><em>switch-case</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-label" target="_blank"><em>case-label</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements" target="_blank"><em>statement</em></a> | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/default-label" target="_blank"><em>default-label</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements" target="_blank"><em>statements</em></a></p>
<p><em>switch-case</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-label" target="_blank"><em>case-label</em></a> <strong>;</strong> | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/default-label" target="_blank"><em>default-label</em></a> <strong>;</strong></p>
<p><em>case-label</em><strong>case</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-item-list" target="_blank"><em>case-item-list</em></a> <strong>:</strong></p>
<p><em>case-item-list</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern" target="_blank"><em>pattern</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-clause" target="_blank"><em>guard-clause</em></a> <em>opt</em> | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern" target="_blank"><em>pattern</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-clause" target="_blank"><em>guard-clause</em></a> <em>opt</em>, <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-item-list" target="_blank"><em>case-item-list</em></a></p>
<p><em>default-label</em><strong>default :</strong></p>
<p><em>guard-clause</em><strong>where</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-expression" target="_blank"><em>guard-expression</em></a></p>
<p><em>guard-expression</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a></p>
<p>Switch语句语法<br><em>switch语句</em><strong>switch</strong> <a href="..\chapter3\04_Expressions.html#expression"><em>表达式</em></a> <strong>{</strong> <a href="..\chapter3\10_Statements.html#switch_cases"><em>SwitchCase列表</em></a> <em>可选</em> <strong>}</strong><br><em>SwitchCase列表</em><a href="..\chapter3\10_Statements.html#switch_case"><em>SwitchCase</em></a> <a href="..\chapter3\10_Statements.html#switch_cases"><em>SwitchCase列表</em></a> <em>可选</em><br><em>SwitchCase</em><a href="..\chapter3\10_Statements.html#case_label"><em>case标签</em></a> <a href="..\chapter3\10_Statements.html#statements"><em>多条语句(Statements)</em></a> | <a href="..\chapter3\10_Statements.html#default_label"><em>default标签</em></a> <a href="..\chapter3\10_Statements.html#statements"><em>多条语句(Statements)</em></a><br><em>SwitchCase</em><a href="..\chapter3\10_Statements.html#case_label"><em>case标签</em></a> <strong>;</strong> | <a href="..\chapter3\10_Statements.html#default_label"><em>default标签</em></a> <strong>;</strong><br><em>case标签</em><strong>case</strong> <a href="..\chapter3\10_Statements.html#case_item_list"><em>case项列表</em></a> <strong>:</strong><br><em>case项列表</em><a href="..\chapter3\07_Patterns.html#pattern"><em>模式</em></a> <a href="..\chapter3\10_Statements.html#guard_clause"><em>guard-clause</em></a> <em>可选</em> | <a href="..\chapter3\07_Patterns.html#pattern"><em>模式</em></a> <a href="..\chapter3\10_Statements.html#guard_clause"><em>guard-clause</em></a> <em>可选</em> <strong>,</strong> <a href="..\chapter3\10_Statements.html#case_item_list"><em>case项列表</em></a><br><em>default标签</em><strong>default</strong> <strong>:</strong><br><em>guard-clause</em><strong>where</strong> <a href="..\chapter3\10_Statements.html#guard_expression"><em>guard-expression</em></a><br><em>guard-expression</em><a href="..\chapter3\04_Expressions.html#expression"><em>表达式</em></a> </p>
</blockquote>
<p><a name="labeled_statement"></a>
<a name="control_transfer_statements"></a> 带标签的语句</p>
@ -792,48 +734,39 @@ else {
<p>标签的作用域是该标签所标记的语句之后的所有语句。你可以不使用带标签的语句,但只要使用它,标签名就必唯一。</p>
<p>关于使用带标签的语句的例子,详情参见<a href="../chapter2/05_Control_Flow.html">控制流</a>一章的<a href="../chapter2/05_Control_Flow.html#labeled_statements">带标签的语句</a></p>
<blockquote>
<p>GRAMMAR OF A LABELED STATEMENT</p>
<p><em>labeled-statement</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement-label" target="_blank"><em>statement-label</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/loop-statement" target="_blank"><em>loop-statement</em></a> | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement-label" target="_blank"><em>statement-label</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-statement" target="_blank"><em>switch-statement</em></a></p>
<p><em>statement-label</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name" target="_blank"><em>label-name</em></a> <strong>:</strong></p>
<p><em>label-name</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier" target="_blank"><em>identifier</em></a></p>
<p>标记语句语法<br><em>标记语句(Labeled Statement)</em><a href="..\chapter3\10_Statements.html#statement_label"><em>语句标签</em></a> <a href="..\chapter3\10_Statements.html#loop_statement"><em>循环语句</em></a> | <a href="..\chapter3\10_Statements.html#statement_label"><em>语句标签</em></a> <a href="..\chapter3\10_Statements.html#switch_statement"><em>switch语句</em></a><br><em>语句标签</em><a href="..\chapter3\10_Statements.html#label_name"><em>标签名称</em></a> <strong>:</strong><br><em>标签名称</em><a href="..\chapter3\02_Lexical_Structure.html#identifier"><em>标识符</em></a> </p>
</blockquote>
<h2 id="-">控制传递语句</h2>
<p>通过无条件地把控制权从一片代码传递到另一片代码控制传递语句能够改变代码执行的顺序。Swift 提供四种类型的控制传递语句:<code>break</code>语句、<code>continue</code>语句、<code>fallthrough</code>语句和<code>return</code>语句。</p>
<blockquote>
<p>GRAMMAR OF A CONTROL TRANSER STATEMENT</p>
<p><em>control-transfer-statement</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/break-statement" target="_blank"><em>break-statement</em></a></p>
<p><em>control-transfer-statement</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/continue-statement" target="_blank"><em>continue-statement</em></a></p>
<p><em>control-transfer-statement</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/fallthrough-statement" target="_blank"><em>fallthrough-statement</em></a></p>
<p><em>control-transfer-statement</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/return-statement" target="_blank"><em>return-statement</em></a></p>
<p>控制传递语句(Control Transfer Statement) 语法<br><em>控制传递语句</em><a href="..\chapter3\10_Statements.html#break_statement"><em>break语句</em></a><br><em>控制传递语句</em><a href="..\chapter3\10_Statements.html#continue_statement"><em>continue语句</em></a><br><em>控制传递语句</em><a href="..\chapter3\10_Statements.html#fallthrough_statement"><em>fallthrough语句</em></a><br><em>控制传递语句</em><a href="..\chapter3\10_Statements.html#return_statement"><em>return语句</em></a> </p>
</blockquote>
<p><a name="break_statement"></a></p>
<h3 id="break-">Break 语句</h3>
<p><code>break</code>语句用于终止循环或<code>switch</code>语句的执行。使用<code>break</code>语句时,可以只写<code>break</code>这个关键词,也可以在<code>break</code>后面跟上标签名label name像下面这样</p>
<pre><code class="lang-swift">break
break `label name`
</code></pre>
<blockquote>
<p>break<br>break <code>label name</code></p>
</blockquote>
<p><code>break</code>语句后面带标签名时,可用于终止由这个标签标记的循环或<code>switch</code>语句的执行。</p>
<p>而当只写<code>break</code>时,则会终止<code>switch</code>语句或上下文中包含<code>break</code>语句的最内层循环的执行。</p>
<p>在这两种情况下,控制权都会被传递给循环或<code>switch</code>语句外面的第一行语句。</p>
<p>关于使用<code>break</code>语句的例子,详情参见<a href="../chapter2/05_Control_Flow.html">控制流</a>一章的 <a href="../chapter2/05_Control_Flow.html#break">Break</a><a href="../chapter2/05_Control_Flow.html#labeled_statements">带标签的语句</a></p>
<blockquote>
<p>GRAMMAR OF A BREAK STATEMENT</p>
<p><em>break-statement</em><strong>break</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name" target="_blank"><em>label-name</em></a> <em>opt</em></p>
<p>Break 语句语法<br><em>break语句</em><strong>break</strong> <a href="..\chapter3\10_Statements.html#label_name"><em>标签名称</em></a> <em>可选</em> </p>
</blockquote>
<p><a name="continue_statement"></a></p>
<h3 id="continue-">Continue 语句</h3>
<p><code>continue</code>语句用于终止循环中当前迭代的执行,但不会终止该循环的执行。使用<code>continue</code>语句时,可以只写<code>continue</code>这个关键词,也可以在<code>continue</code>后面跟上标签名label name像下面这样</p>
<pre><code class="lang-swift">continue
continue `label name`
</code></pre>
<blockquote>
<p>continue<br>continue <code>label name</code> </p>
</blockquote>
<p><code>continue</code>语句后面带标签名时,可用于终止由这个标签标记的循环中当前迭代的执行。</p>
<p>而当只写<code>break</code>时,可用于终止上下文中包含<code>continue</code>语句的最内层循环中当前迭代的执行。</p>
<p>在这两种情况下,控制权都会被传递给循环外面的第一行语句。</p>
<p><code>for</code>语句中,<code>continue</code>语句执行后,<em>increment</em> 表达式还是会被计算,这是因为每次循环体执行完毕后 <em>increment</em> 表达式都会被计算。</p>
<p>关于使用<code>continue</code>语句的例子,详情参见<a href="../chapter2/05_Control_Flow.html">控制流</a>一章的 <a href="../chapter2/05_Control_Flow.html#continue">Continue</a><a href="../chapter2/05_Control_Flow.html#labeled_statements">带标签的语句</a></p>
<blockquote>
<p>GRAMMAR OF A CONTINUE STATEMENT</p>
<p><em>continue-statement</em><strong>continue</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name" target="_blank"><em>label-name</em></a> <em>opt</em></p>
<p>Continue 语句语法<br><em>continue语句</em><strong>continue</strong> <a href="..\chapter3\10_Statements.html#label_name"><em>标签名称</em></a> <em>可选</em> </p>
</blockquote>
<p><a name="fallthrough_statement"></a></p>
<h3 id="fallthrough-">Fallthrough 语句</h3>
@ -841,20 +774,18 @@ continue `label name`
<p><code>fallthrough</code>语句可出现在<code>switch</code>语句中的任意 case 里,但不能出现在最后一个 case 分支中。同时,<code>fallthrough</code>语句也不能把控制权传递给使用了可选绑定的 case 分支。</p>
<p>关于在<code>switch</code>语句中使用<code>fallthrough</code>语句的例子,详情参见<a href="../chapter2/05_Control_Flow.html">控制流</a>一章的<a href="../chapter2/05_Control_Flow.html#control_transfer_statements">控制传递语句</a></p>
<blockquote>
<p>GRAMMAR OF A FALLTHROUGH STATEMENT</p>
<p><em>continue-statement</em><strong>fallthrough</strong></p>
<p>Fallthrough 语句语法<br><em>fallthrough语句</em><strong>fallthrough</strong> </p>
</blockquote>
<h3 id="return-">Return 语句</h3>
<p><code>return</code>语句用于在函数或方法的实现中将控制权传递给调用者,接着程序将会从调用者的位置继续向下执行。</p>
<p>使用<code>return</code>语句时,可以只写<code>return</code>这个关键词,也可以在<code>return</code>后面跟上表达式,像下面这样:</p>
<pre><code class="lang-swift">return
return `expression`
</code></pre>
<blockquote>
<p>return<br>return <code>expression</code> </p>
</blockquote>
<p><code>return</code>语句后面带表达式时表达式的值将会返回给调用者。如果表达式值的类型与调用者期望的类型不匹配Swift 则会在返回表达式的值之前将表达式值的类型转换为调用者期望的类型。</p>
<p>而当只写<code>return</code>时,仅仅是将控制权从该函数或方法传递给调用者,而不返回一个值。(这就是说,该函数或方法的返回类型为<code>Void</code><code>()</code></p>
<blockquote>
<p>GRAMMAR OF A RETURN STATEMENT</p>
<p><em>return-statement</em><strong>return</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> <em>opt</em></p>
<p>Return 语句语法<br><em>return语句</em><strong>return</strong> <a href="..\chapter3\04_Expressions.html#expression"><em>表达式</em></a> <em>可选</em> </p>
</blockquote>

View File

@ -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" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="3" 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>