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="1.1" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="1.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_584">
<section class="normal" id="section-gitbook_4">
<blockquote>
<p>翻译:numbbbbb<br>校对yeahdongcn</p>
<p>翻译:<a href="https://github.com/numbbbbb" target="_blank">numbbbbb</a>
校对:<a href="https://github.com/yeahdongcn" target="_blank">yeahdongcn</a></p>
</blockquote>
<h1 id="-swift">关于 Swift</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="1.2" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="1.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,10 +587,11 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_586">
<section class="normal" id="section-gitbook_6">
<blockquote>
<p>翻译:numbbbbb<br>校对shinyzhu, stanzhai</p>
<p>翻译:<a href="https://github.com/numbbbbb" target="_blank">numbbbbb</a>
校对:<a href="https://github.com/shinyzhu" target="_blank">shinyzhu</a>, <a href="https://github.com/stanzhai" target="_blank">stanzhai</a></p>
</blockquote>
<h1 id="swift-">Swift 初见</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="1" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="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,7 +587,7 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_588">
<section class="normal" id="section-gitbook_8">
<h1 id="-swift">欢迎使用 Swift</h1>
<p>在本章中您将了解 Swift 的特性和开发历史,并对 Swift 有一个初步的了解。</p>

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="2.1" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="2.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,10 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_591">
<section class="normal" id="section-gitbook_11">
<blockquote>
<p>翻译:numbbbbb, lyuka, JaySurplus<br>校对lslxdx </p>
<p>翻译:<a href="https://github.com/numbbbbb" target="_blank">numbbbbb</a>, <a href="https://github.com/lyuka" target="_blank">lyuka</a>, <a href="https://github.com/JaySurplus" target="_blank">JaySurplus</a><br>校对:<a href="https://github.com/lslxdx" target="_blank">lslxdx</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="2.2" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="2.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,10 +587,10 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_612">
<section class="normal" id="section-gitbook_32">
<blockquote>
<p>翻译:xielingwang<br>校对Evilcome </p>
<p>翻译:<a href="https://github.com/xielingwang" target="_blank">xielingwang</a><br>校对:<a href="https://github.com/Evilcome" target="_blank">Evilcome</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="2.3" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="2.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_593">
<section class="normal" id="section-gitbook_13">
<blockquote>
<p>翻译:wh1100717<br>校对Hawstein </p>
<p>翻译:<a href="https://github.com/wh1100717" target="_blank">wh1100717</a>
校对:<a href="https://github.com/Hawstein" target="_blank">Hawstein</a></p>
</blockquote>
<h1 id="-strings-and-characters-">字符串和字符Strings and Characters</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="2.4" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="2.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,11 +587,11 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_595">
<section class="normal" id="section-gitbook_15">
<blockquote>
<p>翻译:zqp
校对:shinyzhu</p>
<p>翻译:<a href="https://github.com/zqp" target="_blank">zqp</a>
校对:<a href="https://github.com/shinyzhu" target="_blank">shinyzhu</a>, <a href="https://github.com/stanzhai" target="_blank">stanzhai</a> </p>
</blockquote>
<h1 id="-collection-types-">集合类型 (Collection Types)</h1>
<hr>
@ -604,8 +604,7 @@
<p>Swift 语言提供经典的数组和字典两种集合类型来存储集合数据。数组用来按顺序存储相同类型的数据。字典虽然无序存储相同类型数据值但是需要由独有的标识符引用和寻址(就是键值对)。</p>
<p>Swift 语言里的数组和字典中存储的数据值类型必须明确。 这意味着我们不能把不正确的数据类型插入其中。 同时这也说明我们完全可以对获取出的值类型非常自信。 Swift 对显式类型集合的使用确保了我们的代码对工作所需要的类型非常清楚,也让我们在开发中可以早早地找到任何的类型不匹配错误。</p>
<blockquote>
<p>注意:
Swift 的数组结构在被声明成常量和变量或者被传入函数与方法中时会相对于其他类型展现出不同的特性。 获取更多信息请参见<a href="#mutability_of_collections">集合的可变性</a><a href="09_Classes_and_Structures.html#assignment_and_copy_behavior_for_collection_types">集合在赋值和复制中的行为</a>章节。</p>
<p>注意:<br>Swift 的数组结构在被声明成常量和变量或者被传入函数与方法中时会相对于其他类型展现出不同的特性。 获取更多信息请参见<a href="#mutability_of_collections">集合的可变性</a><a href="09_Classes_and_Structures.html#assignment_and_copy_behavior_for_collection_types">集合在赋值和复制中的行为</a>章节。</p>
</blockquote>
<p><a name="arrays"></a></p>
<h2 id="-">数组</h2>
@ -624,8 +623,7 @@ Swift 的数组结构在被声明成常量和变量或者被传入函数与方
</code></pre>
<p><code>shoppingList</code>变量被声明为“字符串值类型的数组“,记作<code>String[]</code>。 因为这个数组被规定只有<code>String</code>一种数据结构,所以只有<code>String</code>类型可以在其中被存取。 在这里,<code>shoppinglist</code>数组由两个<code>String</code>值(<code>&quot;Eggs&quot;</code><code>&quot;Milk&quot;</code>)构造,并且由字面量定义。</p>
<blockquote>
<p>注意:
<code>Shoppinglist</code>数组被声明为变量(<code>var</code>关键字创建)而不是常量(<code>let</code>创建)是因为以后可能会有更多的数据项被插入其中。</p>
<p>注意:<br><code>Shoppinglist</code>数组被声明为变量(<code>var</code>关键字创建)而不是常量(<code>let</code>创建)是因为以后可能会有更多的数据项被插入其中。 </p>
</blockquote>
<p>在这个例子中,字面量仅仅包含两个<code>String</code>值。匹配了该数组的变量声明(只能包含<code>String</code>的数组),所以这个字面量的分配过程就是允许用两个初始项来构造<code>shoppinglist</code></p>
<p>由于 Swift 的类型推断机制,当我们用字面量构造只拥有相同类型值数组的时候,我们不必把数组的类型定义清楚。 <code>shoppinglist</code>的构造也可以这样写:</p>
@ -672,25 +670,8 @@ Swift 的数组结构在被声明成常量和变量或者被传入函数与方
<pre><code class="lang-swift">shoppingList[4...6] = [&quot;Bananas&quot;, &quot;Apples&quot;]
// shoppingList 现在有六项
</code></pre>
<p>&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD</p>
<blockquote>
<p>注意:</p>
<h1 id="-count-count-0-count-1-">我们不能使用下标语法在数组尾部添加新项。如果我们试着用这种方法对索引越界的数据进行检索或者设置新值的操作,我们会引发一个运行期错误。我们可以使用索引值和数组的<code>count</code>属性进行比较来在使用某个索引之前先检验是否有效。除了当<code>count</code>等于 0 时(说明这是个空数组),最大索引值一直是<code>count - 1</code>,因为数组都是零起索引。</h1>
<p>注意:</p>
<p>我们不能使用下标语法在数组尾部添加新项。如果我们试着用这种方法对索引越界的数据进行检索或者设置新值的操作,我们会引发一个运行时错误。我们可以使用索引值和数组的<code>count</code>属性进行比较来在使用某个索引之前先检验是否有效。除了当<code>count</code>等于 0 时(说明这是个空数组),最大索引值一直是<code>count - 1</code>,因为数组都是零起索引。</p>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<p>a516af6a531a104ec88da0d236ecf389a5ec72af</p>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
<p>注意:<br>我们不能使用下标语法在数组尾部添加新项。如果我们试着用这种方法对索引越界的数据进行检索或者设置新值的操作,我们会引发一个运行期错误。我们可以使用索引值和数组的<code>count</code>属性进行比较来在使用某个索引之前先检验是否有效。除了当<code>count</code>等于 0 时(说明这是个空数组),最大索引值一直是<code>count - 1</code>,因为数组都是零起索引。 </p>
</blockquote>
<p>调用数组的<code>insert(atIndex:)</code>方法来在某个具体索引值之前添加数据项:</p>
<pre><code class="lang-swift">shoppingList.insert(&quot;Maple Syrup&quot;, atIndex: 0)
@ -780,8 +761,7 @@ someInts = []
</code></pre>
<p><code>airports</code>字典被定义为一种<code>Dictionary&lt;String, String&gt;</code>,它意味着这个字典的键和值都是<code>String</code>类型。</p>
<blockquote>
<p>注意:
<code>airports</code>字典被声明为变量(用<code>var</code>关键字)而不是常量(<code>let</code>关键字)因为后来更多的机场信息会被添加到这个示例字典中。</p>
<p>注意:<br><code>airports</code>字典被声明为变量(用<code>var</code>关键字)而不是常量(<code>let</code>关键字)因为后来更多的机场信息会被添加到这个示例字典中。 </p>
</blockquote>
<p><code>airports</code>字典使用字典字面量初始化,包含两个键值对。第一对的键是<code>TYO</code>,值是<code>Tokyo</code>。第二对的键是<code>DUB</code>,值是<code>Dublin</code></p>
<p>这个字典语句包含了两个<code>String: String</code>类型的键值对。它们对应<code>airports</code>变量声明的类型(一个只有<code>String</code>键和<code>String</code>值的字典)所以这个字典字面量是构造两个初始数据项的<code>airport</code>字典。</p>
@ -848,8 +828,8 @@ airports[&quot;APL&quot;] = nil
}
// Airport code: TYO
// Airport code: LHR
</code></pre>
<pre><code class="lang-swift">for airportName in airports.values {
for airportName in airports.values {
println(&quot;Airport name: \(airportName)&quot;)
}
// Airport name: Tokyo
@ -858,13 +838,12 @@ airports[&quot;APL&quot;] = nil
<p>如果我们只是需要使用某个字典的键集合或者值集合来作为某个接受<code>Array</code>实例 API 的参数,可以直接使用<code>keys</code>或者<code>values</code>属性直接构造一个新数组:</p>
<pre><code class="lang-swift">let airportCodes = Array(airports.keys)
// airportCodes is [&quot;TYO&quot;, &quot;LHR&quot;]
</code></pre>
<pre><code class="lang-swift">let airportNames = Array(airports.values)
let airportNames = Array(airports.values)
// airportNames is [&quot;Tokyo&quot;, &quot;London Heathrow&quot;]
</code></pre>
<blockquote>
<p>注意:
Swift 的字典类型是无序集合类型。其中字典键,值,键值对在遍历的时候会重新排列,而且其中顺序是不固定的。</p>
<p>注意:<br>Swift 的字典类型是无序集合类型。其中字典键,值,键值对在遍历的时候会重新排列,而且其中顺序是不固定的。 </p>
</blockquote>
<p><a name="creating_an_empty_dictionary"></a></p>
<h3 id="-">创建一个空字典</h3>
@ -880,8 +859,7 @@ namesOfIntegers = [:]
// namesOfIntegers 又成为了一个 Int, String类型的空字典
</code></pre>
<blockquote>
<p>注意:
在后台Swift 的数组和字典都是由泛型集合来实现的,想了解更多泛型和集合信息请参见<a href="22_Generics.html">泛型</a></p>
<p>注意:<br>在后台Swift 的数组和字典都是由泛型集合来实现的,想了解更多泛型和集合信息请参见<a href="22_Generics.html">泛型</a></p>
</blockquote>
<p><a name="mutability_of_collections"></a></p>
<h2 id="-">集合的可变性</h2>
@ -890,8 +868,7 @@ namesOfIntegers = [:]
不可变性对数组来说有一点不同,当然我们不能试着改变任何不可变数组的大小,但是我们可以重新设定相对现存索引所对应的值。这使得 Swift 数组在大小被固定的时候依然可以做的很棒。</p>
<p>Swift 数组的可变性行为同时影响了数组实例如何被分配和修改,想获取更多信息,请参见<a href="09_Classes_and_Structures.html#assignment_and_copy_behavior_for_collection_types">集合在赋值和复制中的行为</a></p>
<blockquote>
<p>注意:
在我们不需要改变数组大小的时候创建不可变数组是很好的习惯。如此 Swift 编译器可以优化我们创建的集合。</p>
<p>注意:<br>在我们不需要改变数组大小的时候创建不可变数组是很好的习惯。如此 Swift 编译器可以优化我们创建的集合。 </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="2.5" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="2.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,10 +587,11 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_597">
<section class="normal" id="section-gitbook_17">
<blockquote>
<p>翻译:vclwei, coverxit, NicePiao<br>校对:coverxit, stanzhai</p>
<p>翻译:<a href="https://github.com/vclwei" target="_blank">vclwei</a>, <a href="https://github.com/coverxit" target="_blank">coverxit</a>, <a href="https://github.com/NicePiao" target="_blank">NicePiao</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>

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="2.6" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="2.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_599">
<section class="normal" id="section-gitbook_19">
<blockquote>
<p>翻译:honghaoz<br>校对LunaticM </p>
<p>翻译:<a href="https://github.com/honghaoz" target="_blank">honghaoz</a>
校对:<a href="https://github.com/LunaticM" target="_blank">LunaticM</a></p>
</blockquote>
<h1 id="-functions-">函数Functions</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="2.7" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="2.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,10 +587,11 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_601">
<section class="normal" id="section-gitbook_21">
<blockquote>
<p>翻译:wh1100717<br>校对lyuka </p>
<p>翻译:<a href="https://github.com/wh1100717" target="_blank">wh1100717</a>
校对:<a href="https://github.com/lyuka" target="_blank">lyuka</a></p>
</blockquote>
<h1 id="-closures-">闭包Closures</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="2.8" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="2.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,10 +587,11 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_603">
<section class="normal" id="section-gitbook_23">
<blockquote>
<p>翻译:yankuangshi<br>校对shinyzhu </p>
<p>翻译:<a href="https://github.com/yankuangshi" target="_blank">yankuangshi</a>
校对:<a href="https://github.com/shinyzhu" target="_blank">shinyzhu</a></p>
</blockquote>
<h1 id="-enumerations-">枚举Enumerations</h1>
<hr>
@ -609,7 +610,7 @@
<p><a name="enumeration_syntax"></a></p>
<h2 id="-">枚举语法</h2>
<p>使用<code>enum</code>关键词并且把它们的整个定义放在一对大括号内:</p>
<pre><code class="lang-swift">enum SomeEumeration {
<pre><code class="lang-swift">enum SomeEnumeration {
// enumeration definition goes here
}
</code></pre>
@ -721,7 +722,7 @@ case let .QRCode(productCode):
case CarriageReturn = &quot;\r&quot;
}
</code></pre>
<p>在这里,称为<code>ASCIIControlCharacter</code>的枚举的原始值类型被定义为字符型<code>Character</code>,并被设置了一些比较常见的 ASCII 控制字符。字符值的描述请详见字符串和字符<code>Strings and Characters</code>部分。</p>
<p>在这里,称为<code>ASCIIControlCharacter</code>的枚举的原始值类型被定义为字符型<code>Character</code>,并被设置了一些比较常见的 ASCII 控制字符。字符值的描述请详见字符串和字符<a href="03_Strings_and_Characters.html"><code>Strings and Characters</code></a>部分。</p>
<p>注意,原始值和实例值是不相同的。当你开始在你的代码中定义枚举的时候原始值是被预先填充的值,像上述三个 ASCII 码。对于一个特定的枚举成员,它的原始值始终是相同的。实例值是当你在创建一个基于枚举成员的新常量或变量时才会被设置,并且每次当你这么做得时候,它的值可以是不同的。</p>
<p>原始值可以是字符串,字符,或者任何整型值或浮点型值。每个原始值在它的枚举声明中必须是唯一的。当整型值被用于原始值,如果其他枚举成员没有值时,它们会自动递增。</p>
<p>下面的枚举是对之前<code>Planet</code>这个枚举的一个细化,利用原始整型值来表示每个 planet 在太阳系中的顺序:</p>

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="2.9" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="2.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,10 +587,11 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_605">
<section class="normal" id="section-gitbook_25">
<blockquote>
<p>翻译:JaySurplus<br>校对sg552 </p>
<p>翻译:<a href="https://github.com/JaySurplus" target="_blank">JaySurplus</a>
校对:<a href="https://github.com/sg552" target="_blank">sg552</a></p>
</blockquote>
<h1 id="-">类和结构体</h1>
<p>本页包含内容:</p>
@ -609,36 +610,13 @@
<p><a name="comparing_classes_and_structures"></a></p>
<h3 id="-">类和结构体对比</h3>
<p>Swift 中类和结构体有很多共同点。共同处在于:</p>
<p>&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD</p>
<ul>
<li>定义属性用于储存值</li>
<li>定义方法用于提供功能</li>
<li>定义附属脚本用于访问值</li>
<li>定义构造器用于生成初始化值</li>
<li>通过扩展以增加默认实现的功能</li>
<li><h1 id="-">符合协议以对某类提供标准功能</h1>
</li>
<li>定义属性用于储存值</li>
<li>定义方法用于提供功能</li>
<li>定义下标脚本用于访问值</li>
<li>定义构造器用于生成初始化值</li>
<li>通过扩展以增加默认实现的功能</li>
<li>符合协议以对某类提供标准功能<blockquote>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<p>a516af6a531a104ec88da0d236ecf389a5ec72af</p>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</li>
<li>符合协议以对某类提供标准功能</li>
</ul>
<p>更多信息请参见 <a href="10_Properties.html">属性</a><a href="11_Methods.html">方法</a><a href="12_Subscripts.html">下标脚本</a><a href="14_Initialization.html">初始过程</a><a href="20_Extensions.html">扩展</a>,和<a href="21_Protocols.html">协议</a></p>
<p>与结构体相比,类还有如下的附加功能:</p>

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="2.10" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="2.10" 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_606">
<section class="normal" id="section-gitbook_26">
<blockquote>
<p>翻译:shinyzhu<br>校对pp-prog </p>
<p>翻译:<a href="https://github.com/shinyzhu" target="_blank">shinyzhu</a>
校对:<a href="https://github.com/pp-prog" target="_blank">pp-prog</a></p>
</blockquote>
<h1 id="-properties-">属性 (Properties)</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="2.11" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="2.11" 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_608">
<section class="normal" id="section-gitbook_28">
<blockquote>
<p>翻译:pp-prog<br>校对zqp </p>
<p>翻译:<a href="https://github.com/pp-prog" target="_blank">pp-prog</a>
校对:<a href="https://github.com/zqp" target="_blank">zqp</a></p>
</blockquote>
<h1 id="-methods-">方法Methods</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="2.12" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="2.12" 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_610">
<section class="normal" id="section-gitbook_30">
<blockquote>
<p>翻译:siemenliu<br>校对zq54zquan </p>
<p>翻译:<a href="https://github.com/siemenliu" target="_blank">siemenliu</a>
校对:<a href="https://github.com/zq54zquan" target="_blank">zq54zquan</a></p>
</blockquote>
<h1 id="-subscripts-">下标脚本Subscripts</h1>
<hr>
@ -602,23 +603,8 @@
</ul>
<p><em>下标脚本</em> 可以定义在类Class、结构体structure和枚举enumeration这些目标中可以认为是访问对象、集合或序列的快捷方式不需要再调用实例的特定的赋值和访问方法。举例来说用下标脚本访问一个数组(Array)实例中的元素可以这样写 <code>someArray[index]</code> ,访问字典(Dictionary)实例中的元素可以这样写 <code>someDictionary[key]</code></p>
<p>对于同一个目标可以定义多个下标脚本,通过索引值类型的不同来进行重载,而且索引值的个数可以是多个。</p>
<p>&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD</p>
<h1 id="-">&gt; 译者:这里附属脚本重载在本小节中原文并没有任何演示 </h1>
<blockquote>
<p>译者:这里下标脚本重载在本小节中原文并没有任何演示</p>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<p>a516af6a531a104ec88da0d236ecf389a5ec72af</p>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
<p>译者:这里附属脚本重载在本小节中原文并没有任何演示 </p>
</blockquote>
<p><a name="subscript_syntax"></a></p>
<h2 id="-">下标脚本语法</h2>
@ -652,25 +638,8 @@ println(&quot;3的6倍是\(threeTimesTable[6])&quot;)
</code></pre>
<p>在上例中,通过<code>TimesTable</code>结构体创建了一个用来表示索引值三倍的实例。数值<code>3</code>作为结构体<code>构造函数</code>入参初始化实例成员<code>multiplier</code></p>
<p>你可以通过下标脚本来得到结果,比如<code>threeTimesTable[6]</code>。这条语句访问了<code>threeTimesTable</code>的第六个元素,返回<code>6</code><code>3</code>倍即<code>18</code></p>
<p>&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD</p>
<blockquote>
<p>注意: </p>
<h1 id="-timestable-threetimestable-someindex-"><code>TimesTable</code>例子是基于一个固定的数学公式。它并不适合开放写权限来对<code>threeTimesTable[someIndex]</code>进行赋值操作,这也是为什么附属脚本只定义为只读的原因。 </h1>
<p>注意:</p>
<p><code>TimesTable</code>例子是基于一个固定的数学公式。它并不适合开放写权限来对<code>threeTimesTable[someIndex]</code>进行赋值操作,这也是为什么下标脚本只定义为只读的原因。</p>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<p>a516af6a531a104ec88da0d236ecf389a5ec72af</p>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
<p>注意:<br><code>TimesTable</code>例子是基于一个固定的数学公式。它并不适合开放写权限来对<code>threeTimesTable[someIndex]</code>进行赋值操作,这也是为什么附属脚本只定义为只读的原因。 </p>
</blockquote>
<p><a name="subscript_usage"></a></p>
<h2 id="-">下标脚本用法</h2>
@ -681,25 +650,8 @@ numberOfLegs[&quot;bird&quot;] = 2
</code></pre>
<p>上例定义一个名为<code>numberOfLegs</code>的变量并用一个字典字面量初始化出了包含三对键值的字典实例。<code>numberOfLegs</code>的字典存放值类型推断为<code>Dictionary&lt;String, Int&gt;</code>。字典实例创建完成之后通过下标脚本的方式将整型值<code>2</code>赋值到字典实例的索引为<code>bird</code>的位置中。</p>
<p>更多关于字典Dictionary下标脚本的信息请参考<a href="../chapter2/04_Collection_Types.html">读取和修改字典</a></p>
<p>&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD</p>
<blockquote>
<p>注意: </p>
<h1 id="swift-get-int-numberoflegs-int-int-nil-nil-">Swift 中字典的附属脚本实现中,在<code>get</code>部分返回值是<code>Int?</code>,上例中的<code>numberOfLegs</code>字典通过附属脚本返回的是一个<code>Int?</code>或者说“可选的int”不是每个字典的索引都能得到一个整型值对于没有设过值的索引的访问返回的结果就是<code>nil</code>;同样想要从字典实例中删除某个索引下的值也只需要给这个索引赋值为<code>nil</code>即可。 </h1>
<p>注意:</p>
<p>Swift 中字典的下标脚本实现中,在<code>get</code>部分返回值是<code>Int?</code>,上例中的<code>numberOfLegs</code>字典通过下标脚本返回的是一个<code>Int?</code>或者说“可选的int”不是每个字典的索引都能得到一个整型值对于没有设过值的索引的访问返回的结果就是<code>nil</code>;同样想要从字典实例中删除某个索引下的值也只需要给这个索引赋值为<code>nil</code>即可。</p>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<p>a516af6a531a104ec88da0d236ecf389a5ec72af</p>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
<p>注意:<br>Swift 中字典的附属脚本实现中,在<code>get</code>部分返回值是<code>Int?</code>,上例中的<code>numberOfLegs</code>字典通过附属脚本返回的是一个<code>Int?</code>或者说“可选的int”不是每个字典的索引都能得到一个整型值对于没有设过值的索引的访问返回的结果就是<code>nil</code>;同样想要从字典实例中删除某个索引下的值也只需要给这个索引赋值为<code>nil</code>即可。 </p>
</blockquote>
<p><a name="subscript_options"></a></p>
<h2 id="-">下标脚本选项</h2>

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="2.13" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="2.13" 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_589">
<section class="normal" id="section-gitbook_9">
<blockquote>
<p>翻译:Hawstein<br>校对menlongsheng </p>
<p>翻译:<a href="https://github.com/Hawstein" target="_blank">Hawstein</a>
校对:<a href="https://github.com/menlongsheng" target="_blank">menlongsheng</a></p>
</blockquote>
<h1 id="-inheritance-">继承Inheritance</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="2.14" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="2.14" 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_614">
<section class="normal" id="section-gitbook_34">
<blockquote>
<p>翻译:lifedim<br>校对:lifedim </p>
<p>翻译:<a href="https://github.com/lifedim" target="_blank">lifedim</a>
校对:<a href="https://github.com/lifedim" target="_blank">lifedim</a></p>
</blockquote>
<h1 id="-initialization-">构造过程Initialization</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="2.15" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="2.15" 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_616">
<section class="normal" id="section-gitbook_36">
<blockquote>
<p>翻译:bruce0505<br>校对fd5788 </p>
<p>翻译:<a href="https://github.com/bruce0505" target="_blank">bruce0505</a>
校对:<a href="https://github.com/fd5788" target="_blank">fd5788</a></p>
</blockquote>
<h1 id="-deinitialization-">析构过程Deinitialization</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="2.16" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="2.16" 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_618">
<section class="normal" id="section-gitbook_38">
<blockquote>
<p>翻译:TimothyYe<br>校对Hawstein </p>
<p>翻译:<a href="https://github.com/TimothyYe" target="_blank">TimothyYe</a>
校对:<a href="https://github.com/Hawstein" target="_blank">Hawstein</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="2.17" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="2.17" 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_620">
<section class="normal" id="section-gitbook_40">
<blockquote>
<p>翻译:Jasonbroker<br>校对numbbbbb, stanzhai </p>
<p>翻译:<a href="https://github.com/Jasonbroker" target="_blank">Jasonbroker</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="optional-chaining">Optional Chaining</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="2.18" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="2.18" 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_622">
<section class="normal" id="section-gitbook_42">
<blockquote>
<p>翻译:xiehurricane<br>校对happyming </p>
<p>翻译:<a href="https://github.com/xiehurricane" target="_blank">xiehurricane</a>
校对:<a href="https://github.com/happyming" target="_blank">happyming</a></p>
</blockquote>
<h1 id="-type-casting-">类型检查Type Casting</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="2.19" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="2.19" 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_624">
<section class="normal" id="section-gitbook_44">
<blockquote>
<p>翻译:Lin-H<br>校对shinyzhu </p>
<p>翻译:<a href="https://github.com/Lin-H" target="_blank">Lin-H</a>
校对:<a href="https://github.com/shinyzhu" target="_blank">shinyzhu</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="2.20" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="2.20" 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_626">
<section class="normal" id="section-gitbook_46">
<blockquote>
<p>翻译:lyuka<br>校对Hawstein </p>
<p>翻译:<a href="https://github.com/lyuka" target="_blank">lyuka</a>
校对:<a href="https://github.com/Hawstein" target="_blank">Hawstein</a></p>
</blockquote>
<h1 id="-extensions-">扩展Extensions</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="2.21" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="2.21" 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_633">
<section class="normal" id="section-gitbook_48">
<blockquote>
<p>翻译:geek5nan
校对:dabing1022</p>
<p>翻译:<a href="https://github.com/geek5nan" target="_blank">geek5nan</a>
校对:<a href="https://github.com/dabing1022" target="_blank">dabing1022</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="2.22" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="2.22" 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_628">
<section class="normal" id="section-gitbook_50">
<blockquote>
<p>翻译:takalard<br>校对lifedim </p>
<p>翻译:<a href="https://github.com/takalard" target="_blank">takalard</a>
校对:<a href="https://github.com/lifedim" target="_blank">lifedim</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="2.23" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="2.23" 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_630">
<section class="normal" id="section-gitbook_52">
<blockquote>
<p>翻译:xielingwang<br>校对numbbbbb </p>
<p>翻译:<a href="https://github.com/xielingwang" target="_blank">xielingwang</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="2" data-basepath=".." data-revision="1402792177330">
<div class="book" data-level="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,7 +587,7 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_632">
<section class="normal" id="section-gitbook_54">
<h1 id="swift-">Swift 教程</h1>
<p>本章介绍了 Swift 的各种特性及其使用方法,是全书的核心部分。</p>

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>

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">
@ -44,7 +44,7 @@
<div class="book" data-level="0" data-basepath="." data-revision="1402792177330">
<div class="book" data-level="0" 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>
@ -585,11 +585,10 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_583">
<section class="normal" id="section-gitbook_3">
<blockquote>
<p>Swift 兴趣交流群307017261</p>
<p><a href="http://swiftist.org" target="_blank">Swift 开发者社区</a></p>
<p>Swift 兴趣交流群307017261<br><a href="http://swiftist.org" target="_blank">Swift 开发者社区</a></p>
</blockquote>
<p><!-- --></p>
<blockquote>
@ -607,45 +606,62 @@
<p>团队里每个人都有自己的事情上班、上学、创业但是我们只用了9天就完成整本书的翻译。我不知道大家付出了多少牺牲了多少但是我知道他们的付出必将被这些文字记录下来即使再过10年20年依然熠熠生辉永不被人遗忘。</p>
<p>全体人员名单(排名不分先后):</p>
<ul>
<li>numbbbbb</li>
<li>coverxit</li>
<li>wh1100717</li>
<li>TimothyYe</li>
<li>honghaoz</li>
<li>Hawstein</li>
<li>JaySurplus</li>
<li>stanzhai</li>
<li>lyuka</li>
<li>geek5nan</li>
<li>xielingwang</li>
<li>yankuangshi</li>
<li>dabing1022</li>
<li>siemenliu</li>
<li>fd5788</li>
<li>youkugems</li>
<li>haolloyin</li>
<li>superkam</li>
<li>vclwei</li>
<li>sg552</li>
<li>bzsy</li>
<li>pyanfield</li>
<li>ericzyh</li>
<li>088haizi</li>
<li>viztor</li>
<li>pp-prog</li>
<li>baocaixiong</li>
<li>marsprince</li>
<li>shinyzhu</li>
<li>happyming</li>
<li>menlongsheng</li>
<li>zq54zquan</li>
<li>Evilcome</li>
<li>lslxdx</li>
<li>yeahdongcn</li>
<li>zqp</li>
<li>xiehurricane</li>
<li>Jasonbroker</li>
<li>aquaporcus</li>
<li><a href="https://github.com/numbbbbb" target="_blank">numbbbbb</a></li>
<li><a href="https://github.com/stanzhai" target="_blank">stanzhai</a></li>
<li><a href="https://github.com/coverxit" target="_blank">coverxit</a></li>
<li><a href="https://github.com/wh1100717" target="_blank">wh1100717</a></li>
<li><a href="https://github.com/TimothyYe" target="_blank">TimothyYe</a></li>
<li><a href="https://github.com/honghaoz" target="_blank">honghaoz</a></li>
<li><a href="https://github.com/lyuka" target="_blank">lyuka</a></li>
<li><a href="https://github.com/JaySurplus" target="_blank">JaySurplus</a></li>
<li><a href="https://github.com/Hawstein" target="_blank">Hawstein</a></li>
<li><a href="https://github.com/geek5nan" target="_blank">geek5nan</a></li>
<li><a href="https://github.com/yankuangshi" target="_blank">yankuangshi</a></li>
<li><a href="https://github.com/xielingwang" target="_blank">xielingwang</a></li>
<li><a href="https://github.com/yulingtianxia" target="_blank">yulingtianxia</a></li>
<li><a href="https://github.com/twlkyao" target="_blank">twlkyao</a></li>
<li><a href="https://github.com/dabing1022" target="_blank">dabing1022</a></li>
<li><a href="https://github.com/vclwei" target="_blank">vclwei</a></li>
<li><a href="https://github.com/fd5788" target="_blank">fd5788</a></li>
<li><a href="https://github.com/siemenliu" target="_blank">siemenliu</a></li>
<li><a href="https://github.com/youkugems" target="_blank">youkugems</a></li>
<li><a href="https://github.com/haolloyin" target="_blank">haolloyin</a></li>
<li><a href="https://github.com/wxstars" target="_blank">wxstars</a></li>
<li><a href="https://github.com/IceskYsl" target="_blank">IceskYsl</a></li>
<li><a href="https://github.com/sg552" target="_blank">sg552</a></li>
<li><a href="https://github.com/superkam" target="_blank">superkam</a></li>
<li><a href="https://github.com/zac1st1k" target="_blank">zac1st1k</a></li>
<li><a href="https://github.com/bzsy" target="_blank">bzsy</a></li>
<li><a href="https://github.com/pyanfield" target="_blank">pyanfield</a></li>
<li><a href="https://github.com/ericzyh" target="_blank">ericzyh</a></li>
<li><a href="https://github.com/peiyucn" target="_blank">peiyucn</a></li>
<li><a href="https://github.com/sunfiled" target="_blank">sunfiled</a></li>
<li><a href="https://github.com/lzw120" target="_blank">lzw120</a></li>
<li><a href="https://github.com/viztor" target="_blank">viztor</a></li>
<li><a href="https://github.com/wongzigii" target="_blank">wongzigii</a></li>
<li><a href="https://github.com/umcsdon" target="_blank">umcsdon</a></li>
<li><a href="https://github.com/zq54zquan" target="_blank">zq54zquan</a></li>
<li><a href="https://github.com/xiehurricane" target="_blank">xiehurricane</a></li>
<li><a href="https://github.com/Jasonbroker" target="_blank">Jasonbroker</a></li>
<li><a href="https://github.com/tualatrix" target="_blank">tualatrix</a></li>
<li><a href="https://github.com/pp-prog" target="_blank">pp-prog</a></li>
<li><a href="https://github.com/088haizi" target="_blank">088haizi</a></li>
<li><a href="https://github.com/baocaixiong" target="_blank">baocaixiong</a></li>
<li><a href="https://github.com/yeahdongcn" target="_blank">yeahdongcn</a></li>
<li><a href="https://github.com/shinyzhu" target="_blank">shinyzhu</a></li>
<li><a href="https://github.com/lslxdx" target="_blank">lslxdx</a></li>
<li><a href="https://github.com/Evilcome" target="_blank">Evilcome</a></li>
<li><a href="https://github.com/zqp" target="_blank">zqp</a></li>
<li><a href="https://github.com/NicePiao" target="_blank">NicePiao</a></li>
<li><a href="https://github.com/LunaticM" target="_blank">LunaticM</a></li>
<li><a href="https://github.com/menlongsheng" target="_blank">menlongsheng</a></li>
<li><a href="https://github.com/lifedim" target="_blank">lifedim</a></li>
<li><a href="https://github.com/happyming" target="_blank">happyming</a></li>
<li><a href="https://github.com/bruce0505" target="_blank">bruce0505</a></li>
<li><a href="https://github.com/Lin-H" target="_blank">Lin-H</a></li>
<li><a href="https://github.com/takalard" target="_blank">takalard</a></li>
<li><a href="https://github.com/dabing1022" target="_blank">dabing1022</a></li>
<li><a href="https://github.com/marsprince" target="_blank">marsprince</a></li>
</ul>

View File

@ -1,5 +1,5 @@
CACHE MANIFEST
# Revision 1402792177333
# Revision 1402808574724
CACHE:
index.html
@ -26,10 +26,10 @@ chapter2/17_Optional_Chaining.html
chapter2/18_Type_Casting.html
chapter2/19_Nested_Types.html
chapter2/20_Extensions.html
chapter2/21_Protocols.html
chapter2/22_Generics.html
chapter2/23_Advanced_Operators.html
chapter2/chapter2.html
chapter2/21_Protocols.html
chapter3/06_Attributes.html
chapter3/01_About_the_Language_Reference.html
chapter3/03_Types.html

File diff suppressed because one or more lines are too long