update 03/09

This commit is contained in:
numbbbbb
2014-06-09 23:42:49 +08:00
parent 19c52d124b
commit a90eac43cf
41 changed files with 147 additions and 75 deletions

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3.1" data-basepath=".." data-revision="1402323960213">
<div class="book" data-level="3.1" data-basepath=".." data-revision="1402328527093">
<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_386">
<section class="normal" id="section-gitbook_51">
<h1 id="-">关于语言附注</h1>
<p>本书的这一节描述了Swift编程语言的形式语法。这里描述的语法是为了帮助您更详细的了解该语言而不是让您直接实现一个解析器或编译器。</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3.2" data-basepath=".." data-revision="1402323960213">
<div class="book" data-level="3.2" data-basepath=".." data-revision="1402328527093">
<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_391">
<section class="normal" id="section-gitbook_56">
<h1 id="-">语法结构</h1>
<p>本页包含内容:</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3.3" data-basepath=".." data-revision="1402323960213">
<div class="book" data-level="3.3" data-basepath=".." data-revision="1402328527093">
<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_387">
<section class="normal" id="section-gitbook_52">
<h1 id="-types-">类型Types</h1>
<hr>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3.4" data-basepath=".." data-revision="1402323960213">
<div class="book" data-level="3.4" data-basepath=".." data-revision="1402328527093">
<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

@ -46,7 +46,7 @@
<div class="book" data-level="3.6" data-basepath=".." data-revision="1402323960213">
<div class="book" data-level="3.6" data-basepath=".." data-revision="1402328527093">
<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

@ -46,7 +46,7 @@
<div class="book" data-level="3.7" data-basepath=".." data-revision="1402323960213">
<div class="book" data-level="3.7" data-basepath=".." data-revision="1402328527093">
<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_385">
<section class="normal" id="section-gitbook_50">
<h1 id="-">特性</h1>
<p>特性提供了关于声明和类型的更多信息。在Swift中有两类特性用于修饰声明的以及用于修饰类型的。例如<code>required</code>特性,当应用于一个类的指定或便利初始化器声明时,表明它的每个子类都必须实现那个初始化器。再比如<code>noreturn</code>特性,当应用于函数或方法类型时,表明该函数或方法不会返回到它的调用者。</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3.8" data-basepath=".." data-revision="1402323960213">
<div class="book" data-level="3.8" data-basepath=".." data-revision="1402328527093">
<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

@ -46,7 +46,7 @@
<div class="book" data-level="3.9" data-basepath=".." data-revision="1402323960213">
<div class="book" data-level="3.9" data-basepath=".." data-revision="1402328527093">
<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,6 +587,78 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_58">
<h1 id="-">泛型参数</h1>
<hr>
<p>本页包含内容:</p>
<ul>
<li><a href="#label1">泛型形参语句</a></li>
<li><a href="#label2">泛型实参语句</a></li>
</ul>
<p>本节涉及泛型类型、泛型函数以及泛型构造器的参数,包括形参和实参。声明泛型类型、函数或构造器时,须指定相应的类型参数。类型参数相当于一个占位符,当实例化泛型类型、调用泛型函数或泛型构造器时,就用具体的类型实参替代之。</p>
<p>关于 Swift 语言的泛型概述,见<a href="../charpter2/22_Generics.html">泛型</a>(第二部分第22章)。</p>
<h2 id="-a-name-label1-a-"><a name="label1">泛型实参语句</a></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>
<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 {
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
simpleMin(3.14159, 2.71828) // T is inferred to be Double
</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>
<p>也可以用操作符<code>==</code>来指定两个类型等效的要求。例如,有这样一个约束:<code>T</code><code>U</code>遵守<code>Generator</code>协议,同时要求它们的关联类型等同,可以这样来表达:<code>&lt;T: Generator, U: Generator where T.Element == U.Element&gt;</code></p>
<p>当然,替代类型形参的类型实参必须满足所有类型形参所要求的约束和要求。</p>
<p>泛型函数或构造器可以重载,但在泛型形参语句中的类型形参必须有不同的约束或要求,抑或二者皆不同。当调用重载的泛型函数或构造器时,编译器会用这些约束来决定调用哪个重载函数或构造器。</p>
<p>泛型类可以生成一个子类,但是这个子类也必须是泛型类。</p>
<blockquote>
<p>Grammar of a generic parameter clause</p>
<p>parameter-clause → &lt;­generic-parameter-list­requirement-clause &gt;</p>
<p>­generic-parameter-list → generic-parameter­ generic-parameter­,­generic-parameter-list ­</p>
<p>generic-parameter → type-name­</p>
<p>generic-parameter → type-name­:­type-identifier­</p>
<p>generic-parameter → type-name­:­protocol-composition-type­</p>
<p>requirement-clause → <code>where</code>­requirement-list­</p>
<p>requirement-list → requirement­ requirement­,­requirement-list­</p>
<p>requirement → conformance-requirement­ same-type-requirement­</p>
<p>conformance-requirement → type-identifier­:­type-identifier­</p>
<p>conformance-requirement → type-identifier­:­protocol-composition-type­</p>
<p>same-type-requirement → type-identifier­==­type-identifier</p>
</blockquote>
<h2 id="-a-name-label2-a-"><a name="label2">泛型实参语句</a></h2>
<p>泛型实参语句指定泛型类型的类型实参。泛型实参语句用尖括号(&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 {
/* .. */
}
</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="#label1">泛型形参语句</a>一样,不能用泛型实参语句来指定泛型函数或构造器的类型实参。</p>
<blockquote>
<p>Grammar of a generic argument clause</p>
<p>generic-argument-clause → &lt;­generic-argument-list­&gt;­</p>
<p>generic-argument-list → generic-argument­ generic-argument­,­generic-argument-list­</p>
<p>generic-argument → type</p>
</blockquote>
</section>
</div>
</div>
</div>

View File

@ -44,7 +44,7 @@
<div class="book" data-level="3.10" data-basepath=".." data-revision="1402323960213">
<div class="book" data-level="3.10" data-basepath=".." data-revision="1402328527093">
<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

@ -46,7 +46,7 @@
<div class="book" data-level="3.5" data-basepath=".." data-revision="1402323960213">
<div class="book" data-level="3.5" data-basepath=".." data-revision="1402328527093">
<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_396">
<section class="normal" id="section-gitbook_61">
<h1 id="-">语句</h1>
<p>在 Swift 中有两种类型的语句简单语句和控制流语句。简单语句是最常见的用于构造表达式和声明。控制流语句则用于控制程序执行的流程Swift 中有三种类型的控制流语句:循环语句、分支语句和控制传递语句。</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3" data-basepath=".." data-revision="1402323960213">
<div class="book" data-level="3" data-basepath=".." data-revision="1402328527093">
<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>