make gitbook
This commit is contained in:
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="1.1" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="1.1" data-basepath=".." data-revision="1402673847562">
|
||||
<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_446">
|
||||
<section class="normal" id="section-gitbook_12">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:numbbbbb</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="1.2" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="1.2" data-basepath=".." data-revision="1402673847562">
|
||||
<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_448">
|
||||
<section class="normal" id="section-gitbook_14">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:numbbbbb
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="1" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="1" data-basepath=".." data-revision="1402673847562">
|
||||
<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_450">
|
||||
<section class="normal" id="section-gitbook_16">
|
||||
|
||||
<h1 id="-swift">欢迎使用 Swift</h1>
|
||||
<p>在本章中您将了解 Swift 的特性和开发历史,并对 Swift 有一个初步的了解。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.1" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="2.1" data-basepath=".." data-revision="1402673847562">
|
||||
<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_453">
|
||||
<section class="normal" id="section-gitbook_19">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:numbbbbb, lyuka, JaySurplus</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.2" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="2.2" data-basepath=".." data-revision="1402673847562">
|
||||
<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_472">
|
||||
<section class="normal" id="section-gitbook_40">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:xielingwang</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.3" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="2.3" data-basepath=".." data-revision="1402673847562">
|
||||
<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_455">
|
||||
<section class="normal" id="section-gitbook_21">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:wh1100717</p>
|
||||
@ -794,7 +794,7 @@ for scene in romeoAndJuliet {
|
||||
println("There are \(act1SceneCount) scenes in Act 1")
|
||||
// 打印输出:"There are 5 scenes in Act 1"
|
||||
</code></pre><p>相似地,您可以用<code>hasSuffix</code>方法来计算发生在不同地方的场景数:</p>
|
||||
<pre><code>“var mansionCount = 0
|
||||
<pre><code>var mansionCount = 0
|
||||
var cellCount = 0
|
||||
for scene in romeoAndJuliet {
|
||||
if scene.hasSuffix("Capulet's mansion") {
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.4" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="2.4" data-basepath=".." data-revision="1402673847562">
|
||||
<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_457">
|
||||
<section class="normal" id="section-gitbook_23">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:zqp</p>
|
||||
@ -709,7 +709,7 @@
|
||||
<h3 id="-">创建并且构造一个数组</h3>
|
||||
<p>我们可以使用构造语法来创建一个由特定数据类型构成的空数组:</p>
|
||||
<pre><code>var someInts = Int[]()
|
||||
println("someInts is of type Int[] with \(someInts。count) items。")
|
||||
println("someInts is of type Int[] with \(someInts.count) items。")
|
||||
// 打印 "someInts is of type Int[] with 0 items。"(someInts是0数据项的Int[]数组)
|
||||
</code></pre><p>注意<code>someInts</code>被设置为一个<code>Int[]</code>构造函数的输出所以它的变量类型被定义为<code>Int[]</code>。</p>
|
||||
<p>除此之外,如果代码上下文中提供了类型信息, 例如一个函数参数或者一个已经定义好类型的常量或者变量,我们可以使用空数组语句创建一个空数组,它的写法很简单:<code>[]</code>(一对空方括号):</p>
|
||||
@ -765,14 +765,14 @@ someInts = []
|
||||
<pre><code>if let oldValue = airports.updateValue("Dublin Internation", forKey: "DUB") {
|
||||
println("The old value for DUB was \(oldValue).")
|
||||
}
|
||||
// 输出 "The old value for DUB was Dublin."(dub原值是dublin)
|
||||
// 输出 "The old value for DUB was Dublin."(DUB原值是dublin)
|
||||
</code></pre><p>我们也可以使用下标语法来在字典中检索特定键对应的值。由于使用一个没有值的键这种情况是有可能发生的,可选类型返回这个键存在的相关值,否则就返回<code>nil</code>:</p>
|
||||
<pre><code>if let airportName = airports["DUB"] {
|
||||
println("The name of the airport is \(airportName).")
|
||||
} else {
|
||||
println("That airport is not in the airports dictionary.")
|
||||
}
|
||||
// 打印 "The name of the airport is Dublin INTernation."(机场的名字是都柏林国际)
|
||||
// 打印 "The name of the airport is Dublin Internation."(机场的名字是都柏林国际)
|
||||
</code></pre><p>我们还可以使用下标语法来通过给某个键的对应值赋值为<code>nil</code>来从字典里移除一个键值对:</p>
|
||||
<pre><code>airports["APL"] = "Apple Internation"
|
||||
// "Apple Internation"不是真的 APL机场, 删除它
|
||||
@ -789,20 +789,20 @@ airports["APL"] = nil
|
||||
<h3 id="-">字典遍历</h3>
|
||||
<p>我们可以使用<code>for-in</code>循环来遍历某个字典中的键值对。每一个字典中的数据项都由<code>(key, value)</code>元组形式返回,并且我们可以使用暂时性常量或者变量来分解这些元组:</p>
|
||||
<pre><code>for (airportCode, airportName) in airports {
|
||||
prINTln("\(airportCode): \(airportName)")
|
||||
println("\(airportCode): \(airportName)")
|
||||
}
|
||||
// TYO: Tokyo
|
||||
// LHR: London Heathrow
|
||||
</code></pre><p><code>for-in</code>循环请参见<a href="05_Control_Flow.html#for_loops">For 循环</a>。</p>
|
||||
<p>我们也可以通过访问他的<code>keys</code>或者<code>values</code>属性(都是可遍历集合)检索一个字典的键或者值:</p>
|
||||
<pre><code>for airportCode in airports.keys {
|
||||
prINTln("Airport code: \(airportCode)")
|
||||
println("Airport code: \(airportCode)")
|
||||
}
|
||||
// Airport code: TYO
|
||||
// Airport code: LHR
|
||||
|
||||
for airportName in airports.values {
|
||||
prINTln("Airport name: \(airportName)")
|
||||
println("Airport name: \(airportName)")
|
||||
}
|
||||
// Airport name: Tokyo
|
||||
// Airport name: London Heathrow
|
||||
@ -835,7 +835,7 @@ namesOfIntegers = [:]
|
||||
<h2 id="-">集合的可变性</h2>
|
||||
<p>数组和字典都是在单个集合中存储可变值。如果我们创建一个数组或者字典并且把它分配成一个变量,这个集合将会是可变的。这意味着我们可以在创建之后添加更多或移除已存在的数据项来改变这个集合的大小。与此相反,如果我们把数组或字典分配成常量,那么他就是不可变的,它的大小不能被改变。</p>
|
||||
<p>对字典来说,不可变性也意味着我们不能替换其中任何现有键所对应的值。不可变字典的内容在被首次设定之后不能更改。
|
||||
不可变行对数组来说有一点不同,当然我们不能试着改变任何不可变数组的大小,但是我们·可以重新设定相对现存索引所对应的值。这使得 Swift 数组在大小被固定的时候依然可以做的很棒。</p>
|
||||
不可变性对数组来说有一点不同,当然我们不能试着改变任何不可变数组的大小,但是我们可以重新设定相对现存索引所对应的值。这使得 Swift 数组在大小被固定的时候依然可以做的很棒。</p>
|
||||
<p>Swift 数组的可变性行为同时影响了数组实例如何被分配和修改,想获取更多信息,请参见<a href="09_Classes_and_Structures.html#assignment_and_copy_behavior_for_collection_types">集合在赋值和复制中的行为</a>。</p>
|
||||
<blockquote>
|
||||
<p>注意:</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.5" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="2.5" data-basepath=".." data-revision="1402673847562">
|
||||
<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_459">
|
||||
<section class="normal" id="section-gitbook_25">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:vclwei, coverxit, NicePiao</p>
|
||||
@ -643,7 +643,7 @@ for _ in 1...power {
|
||||
println("\(base) to the power of \(power) is \(answer)")
|
||||
// 输出 "3 to the power of 10 is 59049"
|
||||
</code></pre>
|
||||
<p>这个例子计算 base 这个数的 power 次幂(本例中,是<code>3</code>的<code>10</code>次幂),从<code>1</code>(<code>3</code>的<code>0</code>次幂)开始做<code>3</code>的乘法, 进行<code>10</code>次,使用<code>0</code>到<code>9</code>的半闭区间循环。这个计算并不需要知道每一次循环中计数器具体的值,只需要执行了正确的循环次数即可。下划线符号<code>_</code>(替代循环中的变量)能够忽略具体的值,并且不提供循环遍历时对值的访问。</p>
|
||||
<p>这个例子计算 base 这个数的 power 次幂(本例中,是<code>3</code>的<code>10</code>次幂),从<code>1</code>(<code>3</code>的<code>0</code>次幂)开始做<code>3</code>的乘法, 进行<code>10</code>次,使用<code>1</code>到<code>10</code>的半闭区间循环。这个计算并不需要知道每一次循环中计数器具体的值,只需要执行了正确的循环次数即可。下划线符号<code>_</code>(替代循环中的变量)能够忽略具体的值,并且不提供循环遍历时对值的访问。</p>
|
||||
<p>使用<code>for-in</code>遍历一个数组所有元素:</p>
|
||||
<pre><code class="lang-swift">let names = ["Anna", "Alex", "Brian", "Jack"]
|
||||
for name in names {
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.6" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="2.6" data-basepath=".." data-revision="1402673847562">
|
||||
<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_461">
|
||||
<section class="normal" id="section-gitbook_27">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:honghaoz</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.7" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="2.7" data-basepath=".." data-revision="1402673847562">
|
||||
<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_463">
|
||||
<section class="normal" id="section-gitbook_29">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:wh1100717</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.8" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="2.8" data-basepath=".." data-revision="1402673847562">
|
||||
<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_465">
|
||||
<section class="normal" id="section-gitbook_31">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:yankuangshi</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.9" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="2.9" data-basepath=".." data-revision="1402673847562">
|
||||
<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_471">
|
||||
<section class="normal" id="section-gitbook_33">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:JaySurplus</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.10" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="2.10" data-basepath=".." data-revision="1402673847562">
|
||||
<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_467">
|
||||
<section class="normal" id="section-gitbook_34">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:shinyzhu</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.11" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="2.11" data-basepath=".." data-revision="1402673847562">
|
||||
<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_474">
|
||||
<section class="normal" id="section-gitbook_36">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:pp-prog</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.12" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="2.12" data-basepath=".." data-revision="1402673847562">
|
||||
<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_469">
|
||||
<section class="normal" id="section-gitbook_38">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:siemenliu</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.13" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="2.13" data-basepath=".." data-revision="1402673847562">
|
||||
<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_451">
|
||||
<section class="normal" id="section-gitbook_17">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:Hawstein</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.14" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="2.14" data-basepath=".." data-revision="1402673847562">
|
||||
<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_478">
|
||||
<section class="normal" id="section-gitbook_42">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:lifedim</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.15" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="2.15" data-basepath=".." data-revision="1402673847562">
|
||||
<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_476">
|
||||
<section class="normal" id="section-gitbook_44">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:bruce0505</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.16" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="2.16" data-basepath=".." data-revision="1402673847562">
|
||||
<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_480">
|
||||
<section class="normal" id="section-gitbook_46">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:TimothyYe</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.17" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="2.17" data-basepath=".." data-revision="1402673847562">
|
||||
<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_482">
|
||||
<section class="normal" id="section-gitbook_48">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:Jasonbroker</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.18" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="2.18" data-basepath=".." data-revision="1402673847562">
|
||||
<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_484">
|
||||
<section class="normal" id="section-gitbook_50">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:xiehurricane</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.19" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="2.19" data-basepath=".." data-revision="1402673847562">
|
||||
<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_486">
|
||||
<section class="normal" id="section-gitbook_52">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:Lin-H</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.20" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="2.20" data-basepath=".." data-revision="1402673847562">
|
||||
<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_488">
|
||||
<section class="normal" id="section-gitbook_54">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:lyuka</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.21" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="2.21" data-basepath=".." data-revision="1402673847562">
|
||||
<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_490">
|
||||
<section class="normal" id="section-gitbook_56">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:geek5nan</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.22" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="2.22" data-basepath=".." data-revision="1402673847562">
|
||||
<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_492">
|
||||
<section class="normal" id="section-gitbook_58">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:takalard</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.23" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="2.23" data-basepath=".." data-revision="1402673847562">
|
||||
<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_494">
|
||||
<section class="normal" id="section-gitbook_60">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:xielingwang</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="2" data-basepath=".." data-revision="1402673847562">
|
||||
<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_496">
|
||||
<section class="normal" id="section-gitbook_62">
|
||||
|
||||
<h1 id="swift-">Swift 教程</h1>
|
||||
<p>本章介绍了 Swift 的各种特性及其使用方法,是全书的核心部分。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.1" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="3.1" data-basepath=".." data-revision="1402673847562">
|
||||
<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_499">
|
||||
<section class="normal" id="section-gitbook_65">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:ChildhoodAndy</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.2" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="3.2" data-basepath=".." data-revision="1402673847562">
|
||||
<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_507">
|
||||
<section class="normal" id="section-gitbook_73">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:superkam</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.3" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="3.3" data-basepath=".." data-revision="1402673847562">
|
||||
<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_501">
|
||||
<section class="normal" id="section-gitbook_67">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:lyuka</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.4" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="3.4" data-basepath=".." data-revision="1402673847562">
|
||||
<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_503">
|
||||
<section class="normal" id="section-gitbook_69">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:sg552</p>
|
||||
@ -597,13 +597,13 @@
|
||||
<hr>
|
||||
<p>本页包含内容:</p>
|
||||
<ul>
|
||||
<li>[前缀表达式(Prefix Expressions)]</li>
|
||||
<li>[二元表达式(Binary Expressions)]</li>
|
||||
<li>[赋值表达式(Assignment Operator)]</li>
|
||||
<li>[三元条件运算符(Ternary Conditional Operator)]</li>
|
||||
<li>[类型转换运算符(Type-Casting Operators)]</li>
|
||||
<li>[主要表达式(Primary Expressions)]</li>
|
||||
<li>[后缀表达式(Postfix Expressions)]</li>
|
||||
<li><a href="#prefix_expressions">前缀表达式(Prefix Expressions)</a></li>
|
||||
<li><a href="#binary_expressions">二元表达式(Binary Expressions)</a></li>
|
||||
<li><a href="#assignment_operator">赋值表达式(Assignment Operator)</a></li>
|
||||
<li><a href="#ternary_conditional_operator">三元条件运算符(Ternary Conditional Operator)</a></li>
|
||||
<li><a href="#type-casting_operators">类型转换运算符(Type-Casting Operators)</a></li>
|
||||
<li><a href="#primary_expressions">主要表达式(Primary Expressions)</a></li>
|
||||
<li><a href="#postfix_expressions">后缀表达式(Postfix Expressions)</a></li>
|
||||
</ul>
|
||||
<p>Swift 中存在四种表达式: 前缀(prefix)表达式,二元(binary)表达式,主要(primary)表达式和后缀(postfix)表达式。表达式可以返回一个值,以及运行某些逻辑(causes a side effect)。</p>
|
||||
<p>前缀表达式和二元表达式就是对某些表达式使用各种运算符(operators)。 主要表达式是最短小的表达式,它提供了获取(变量的)值的一种途径。 后缀表达式则允许你建立复杂的表达式,例如配合函数调用和成员访问。 每种表达式都在下面有详细论述~</p>
|
||||
@ -612,6 +612,7 @@
|
||||
<p><em>expression</em> → <em>prefix-expression</em><em>binary-expressions(</em>opt)
|
||||
<em>expression-list</em> → <em>expression</em>| <em>expression</em>,<em>expression-list</em></p>
|
||||
</blockquote>
|
||||
<p><a name="prefix_expressions"></a></p>
|
||||
<h2 id="-prefix-expressions-">前缀表达式(Prefix Expressions)</h2>
|
||||
<p>前缀表达式由 前缀符号和表达式组成。(这个前缀符号只能接收一个参数)</p>
|
||||
<p>Swift 标准库支持如下的前缀操作符:</p>
|
||||
@ -631,6 +632,7 @@
|
||||
<em>prefix-expression</em> → <em>in-out-expression</em>
|
||||
<em>in-out-expression</em> → &<em>identifier</em></p>
|
||||
</blockquote>
|
||||
<p><a name="binary_expressions"></a></p>
|
||||
<h2 id="-binary-expressions-">二元表达式(Binary Expressions)</h2>
|
||||
<p>二元表达式由 "左边参数" + "二元运算符" + "右边参数" 组成, 它有如下的形式:</p>
|
||||
<p> <code>left-hand argument</code> <code>operator</code> <code>right-hand argument</code></p>
|
||||
@ -740,6 +742,7 @@
|
||||
<em>binary-expression</em> → <em>type-casting-operator</em>
|
||||
<em>binary-expression</em>s → <em>binary-expression</em><em>binary-expressions</em>(opt)</p>
|
||||
</blockquote>
|
||||
<p><a name="assignment_operator"></a></p>
|
||||
<h2 id="-assignment-operator-">赋值表达式(Assignment Operator)</h2>
|
||||
<p>The assigment operator sets a new value for a given expression. It has the following form:
|
||||
赋值表达式会对某个给定的表达式赋值。 它有如下的形式;</p>
|
||||
@ -753,6 +756,7 @@
|
||||
<p>赋值表达式的语法</p>
|
||||
<p><em>assignment-operator</em> → =</p>
|
||||
</blockquote>
|
||||
<p><a name="ternary_conditional_operator"></a></p>
|
||||
<h2 id="-ternary-conditional-operator-">三元条件运算符(Ternary Conditional Operator)</h2>
|
||||
<p>三元条件运算符 是根据条件来获取值。 形式如下:</p>
|
||||
<pre><code>`condition` ? `expression used if true` : `expression used if false`
|
||||
@ -762,6 +766,7 @@
|
||||
<p>三元条件表达式</p>
|
||||
<p><code>conditional-operator</code> → ?<code>expression</code>:</p>
|
||||
</blockquote>
|
||||
<p><a name="type-casting_operators"></a></p>
|
||||
<h2 id="-type-casting-operators-">类型转换运算符(Type-Casting Operators)</h2>
|
||||
<p>有两种类型转换操作符: as 和 is. 它们有如下的形式:</p>
|
||||
<pre><code>`expression` as `type`
|
||||
@ -800,6 +805,7 @@ let y2: SomeType = x // Type information from an annotation
|
||||
<p>类型转换的语法</p>
|
||||
<p><em>type-casting-operator</em> → is<em>type</em>| as?(opt)<em>type</em></p>
|
||||
</blockquote>
|
||||
<p><a name="primary_expressions"></a></p>
|
||||
<h2 id="-primary-expressions-">主要表达式(Primary Expressions)</h2>
|
||||
<p><code>主要表达式</code>是最基本的表达式。 它们可以跟 前缀表达式,二元表达式,后缀表达式以及其他主要表达式组合使用。</p>
|
||||
<blockquote>
|
||||
@ -994,6 +1000,7 @@ x = .AnotherValue
|
||||
<p>通配符表达式的语法</p>
|
||||
<p><em>wildcard-expression</em> → _</p>
|
||||
</blockquote>
|
||||
<p><a name="postfix_expressions"></a></p>
|
||||
<h2 id="-postfix-expressions-">后缀表达式(Postfix Expressions)</h2>
|
||||
<p>后缀表达式就是在某个表达式的后面加上 操作符。 严格的讲,每个主要表达式(primary expression)都是一个后缀表达式</p>
|
||||
<p>Swift 标准库提供了下列后缀表达式:</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.6" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="3.6" data-basepath=".." data-revision="1402673847562">
|
||||
<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_505">
|
||||
<section class="normal" id="section-gitbook_71">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:marsprince</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.7" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="3.7" data-basepath=".." data-revision="1402673847562">
|
||||
<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_497">
|
||||
<section class="normal" id="section-gitbook_63">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:Hawstein</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.8" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="3.8" data-basepath=".." data-revision="1402673847562">
|
||||
<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_509">
|
||||
<section class="normal" id="section-gitbook_75">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:honghaoz</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.9" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="3.9" data-basepath=".." data-revision="1402673847562">
|
||||
<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_511">
|
||||
<section class="normal" id="section-gitbook_77">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:fd5788</p>
|
||||
@ -597,13 +597,14 @@
|
||||
<hr>
|
||||
<p>本页包含内容:</p>
|
||||
<ul>
|
||||
<li><a href="#generic_parameter">泛型形参语句</a></li>
|
||||
<li><a href="#generic_argument">泛型实参语句</a></li>
|
||||
<li><a href="#generic_parameter">泛型形参子句</a></li>
|
||||
<li><a href="#generic_argument">泛型实参子句</a></li>
|
||||
</ul>
|
||||
<p>本节涉及泛型类型、泛型函数以及泛型构造器的参数,包括形参和实参。声明泛型类型、函数或构造器时,须指定相应的类型参数。类型参数相当于一个占位符,当实例化泛型类型、调用泛型函数或泛型构造器时,就用具体的类型实参替代之。</p>
|
||||
<p>关于 Swift 语言的泛型概述,见<a href="../charpter2/22_Generics.html">泛型</a>(第二部分第22章)。</p>
|
||||
<h2 id="-a-name-generic_parameter-a-"><a name="generic_parameter">泛型形参语句</a></h2>
|
||||
<p>泛型形参语句指定泛型类型或函数的类型形参,以及这些参数的关联约束和要求。泛型形参语句用尖括号(<>)包住,并且有以下两种形式:</p>
|
||||
<p><a name="generic_parameter"></a></p>
|
||||
<h2 id="-">泛型形参子句</h2>
|
||||
<p>泛型形参子句指定泛型类型或函数的类型形参,以及这些参数的关联约束和要求。泛型形参子句用尖括号(<>)包住,并且有以下两种形式:</p>
|
||||
<pre><code><generic parameter list>
|
||||
<generic parameter list where requirements >
|
||||
</code></pre><p>泛型形参列表中泛型形参用逗号分开,每一个采用以下形式:</p>
|
||||
@ -616,52 +617,35 @@
|
||||
}
|
||||
return x
|
||||
}
|
||||
</code></pre><p>如,<code>Int</code>和<code>Double</code>均满足<code>Comparable</code>协议,该函数接受任何一种类型。与泛型类型相反,调用泛型函数或构造器时不需要指定泛型实参语句。类型实参由传递给函数或构造器的实参推断而出。</p>
|
||||
</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><T where T: C, T: P></code>表示泛型类型<code>T</code>继承自类<code>C</code>且遵守协议<code>P</code>。</p>
|
||||
</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><T where T: C, T: P></code>表示泛型类型<code>T</code>继承自类<code>C</code>且遵守协议<code>P</code>。</p>
|
||||
<p>如上所述,可以强制约束类型形参的关联类型遵守某个协议。<code><T: Generator where T.Element: Equatable></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><T: Generator, U: Generator where T.Element == U.Element></code>。</p>
|
||||
<p>当然,替代类型形参的类型实参必须满足所有类型形参所要求的约束和要求。</p>
|
||||
<p>泛型函数或构造器可以重载,但在泛型形参语句中的类型形参必须有不同的约束或要求,抑或二者皆不同。当调用重载的泛型函数或构造器时,编译器会用这些约束来决定调用哪个重载函数或构造器。</p>
|
||||
<p>泛型函数或构造器可以重载,但在泛型形参子句中的类型形参必须有不同的约束或要求,抑或二者皆不同。当调用重载的泛型函数或构造器时,编译器会用这些约束来决定调用哪个重载函数或构造器。</p>
|
||||
<p>泛型类可以生成一个子类,但是这个子类也必须是泛型类。</p>
|
||||
<blockquote>
|
||||
<p>Grammar of a generic parameter clause</p>
|
||||
<p>parameter-clause → <generic-parameter-listrequirement-clause ></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>
|
||||
<p>泛型形参子句语法<br><em>泛型参数子句</em> → <strong><</strong> <a href="GenericParametersAndArguments.html#generic_parameter_list"><em>泛型参数列表</em></a> <a href="GenericParametersAndArguments.html#requirement_clause"><em>约束子句</em></a> <em>可选</em> <strong>></strong><br><em>泛型参数列表</em> → <a href="GenericParametersAndArguments.html#generic_parameter"><em>泛形参数</em></a> | <a href="GenericParametersAndArguments.html#generic_parameter"><em>泛形参数</em></a> <strong>,</strong> <a href="GenericParametersAndArguments.html#generic_parameter_list"><em>泛型参数列表</em></a><br><em>泛形参数</em> → <a href="..\chapter3\03_Types.html#type_name"><em>类型名称</em></a><br><em>泛形参数</em> → <a href="..\chapter3\03_Types.html#type_name"><em>类型名称</em></a> <strong>:</strong> <a href="..\chapter3\03_Types.html#type_identifier"><em>类型标识</em></a><br><em>泛形参数</em> → <a href="..\chapter3\03_Types.html#type_name"><em>类型名称</em></a> <strong>:</strong> <a href="..\chapter3\03_Types.html#protocol_composition_type"><em>协议合成类型</em></a><br><em>约束子句</em> → <strong>where</strong> <a href="GenericParametersAndArguments.html#requirement_list"><em>约束列表</em></a><br><em>约束列表</em> → <a href="GenericParametersAndArguments.html#requirement"><em>约束</em></a> | <a href="GenericParametersAndArguments.html#requirement"><em>约束</em></a> <strong>,</strong> <a href="GenericParametersAndArguments.html#requirement_list"><em>约束列表</em></a><br><em>约束</em> → <a href="GenericParametersAndArguments.html#conformance_requirement"><em>一致性约束</em></a> | <a href="GenericParametersAndArguments.html#same_type_requirement"><em>同类型约束</em></a><br><em>一致性约束</em> → <a href="..\chapter3\03_Types.html#type_identifier"><em>类型标识</em></a> <strong>:</strong> <a href="..\chapter3\03_Types.html#type_identifier"><em>类型标识</em></a><br><em>一致性约束</em> → <a href="..\chapter3\03_Types.html#type_identifier"><em>类型标识</em></a> <strong>:</strong> <a href="..\chapter3\03_Types.html#protocol_composition_type"><em>协议合成类型</em></a><br><em>同类型约束</em> → <a href="..\chapter3\03_Types.html#type_identifier"><em>类型标识</em></a> <strong>==</strong> <a href="..\chapter3\03_Types.html#type_identifier"><em>类型标识</em></a> </p>
|
||||
</blockquote>
|
||||
<h2 id="-a-name-generic_argument-a-"><a name="generic_argument">泛型实参语句</a></h2>
|
||||
<p>泛型实参语句指定<em>泛型类型</em>的类型实参。泛型实参语句用尖括号(<>)包住,形式如下:</p>
|
||||
<p><a name="generic_argument"></a></p>
|
||||
<h2 id="-">泛型实参子句</h2>
|
||||
<p>泛型实参子句指定<em>泛型类型</em>的类型实参。泛型实参子句用尖括号(<>)包住,形式如下:</p>
|
||||
<pre><code>< generic argument list >
|
||||
</code></pre><p>泛型实参列表中类型实参有逗号分开。类型实参是实际具体类型的名字,用来替代泛型类型的泛型形参语句中的相应的类型形参。从而得到泛型类型的一个特化版本。如,Swift标准库的泛型字典类型定义如下:</p>
|
||||
<pre><code> struct Dictionary<KeyTypel: Hashable, ValueType>: Collection,
|
||||
DictionaryLiteralConvertible {
|
||||
</code></pre><p>泛型实参列表中类型实参有逗号分开。类型实参是实际具体类型的名字,用来替代泛型类型的泛型形参子句中的相应的类型形参。从而得到泛型类型的一个特化版本。如,Swift标准库的泛型字典类型定义如下:</p>
|
||||
<pre><code>struct Dictionary<KeyTypel: Hashable, ValueType>: Collection, DictionaryLiteralConvertible {
|
||||
/* .. */
|
||||
}
|
||||
</code></pre><p>泛型<code>Dictionary</code>类型的特化版本,<code>Dictionary<String, Int></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<String, Int></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<Int></code>替代泛型类型<code>Array<T></code>的类型形参<code>T</code>来实现。</p>
|
||||
<pre><code>let arrayOfArrays: Array<Array<Int>> = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
|
||||
</code></pre><p>如<a href="#generic_parameter">泛型形参语句</a>所述,不能用泛型实参语句来指定泛型函数或构造器的类型实参。</p>
|
||||
</code></pre><p>如<a href="#generic_parameter">泛型形参子句</a>所述,不能用泛型实参子句来指定泛型函数或构造器的类型实参。</p>
|
||||
<blockquote>
|
||||
<p>Grammar of a generic argument clause</p>
|
||||
<p>generic-argument-clause → <generic-argument-list></p>
|
||||
<p>generic-argument-list → generic-argument generic-argument,generic-argument-list</p>
|
||||
<p>generic-argument → type</p>
|
||||
<p>泛型实参子句语法<br><em>(泛型参数子句Generic Argument Clause)</em> → <strong><</strong> <a href="GenericParametersAndArguments.html#generic_argument_list"><em>泛型参数列表</em></a> <strong>></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>
|
||||
<p>==============================================
|
||||
上篇:<a href="07_Patterns.html">模式</a></p>
|
||||
<p>下篇:<a href="09_Summary_of_the_Grammar.html">语法总结</a></p>
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
@ -44,7 +44,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.10" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="3.10" data-basepath=".." data-revision="1402673847562">
|
||||
<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,9 +585,13 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_513">
|
||||
<section class="normal" id="section-gitbook_79">
|
||||
|
||||
<h1 id="-">语法总结</h1>
|
||||
<blockquote>
|
||||
<p>翻译:StanZhai</p>
|
||||
<p>校对:xielingwang</p>
|
||||
</blockquote>
|
||||
<h1 id="-">语法总结</h1>
|
||||
<hr>
|
||||
<p>本页包含内容:</p>
|
||||
<ul>
|
||||
@ -678,7 +682,7 @@
|
||||
<p><a name="generic_parameters_and_arguments"></a></p>
|
||||
<h2 id="-">泛型参数</h2>
|
||||
<blockquote>
|
||||
<p>泛型形参子句(Generic Parameter Clause) 语法<br><em>泛型参数子句</em> → <strong><</strong> <a href="GenericParametersAndArguments.html#generic_parameter_list"><em>泛型参数列表</em></a> <a href="GenericParametersAndArguments.html#requirement_clause"><em>约束子句</em></a> <em>可选</em> <strong>></strong><br><em>泛型参数列表</em> → <a href="GenericParametersAndArguments.html#generic_parameter"><em>泛形参数</em></a> | <a href="GenericParametersAndArguments.html#generic_parameter"><em>泛形参数</em></a> <strong>,</strong> <a href="GenericParametersAndArguments.html#generic_parameter_list"><em>泛型参数列表</em></a><br><em>泛形参数</em> → <a href="..\chapter3\03_Types.html#type_name"><em>类名</em></a><br><em>泛形参数</em> → <a href="..\chapter3\03_Types.html#type_name"><em>类名</em></a> <strong>:</strong> <a href="..\chapter3\03_Types.html#type_identifier"><em>类型标识</em></a><br><em>泛形参数</em> → <a href="..\chapter3\03_Types.html#type_name"><em>类名</em></a> <strong>:</strong> <a href="..\chapter3\03_Types.html#protocol_composition_type"><em>协议合成类型</em></a><br><em>约束子句</em> → <strong>where</strong> <a href="GenericParametersAndArguments.html#requirement_list"><em>约束列表</em></a><br><em>约束列表</em> → <a href="GenericParametersAndArguments.html#requirement"><em>约束</em></a> | <a href="GenericParametersAndArguments.html#requirement"><em>约束</em></a> <strong>,</strong> <a href="GenericParametersAndArguments.html#requirement_list"><em>约束列表</em></a><br><em>约束</em> → <a href="GenericParametersAndArguments.html#conformance_requirement"><em>一致性约束</em></a> | <a href="GenericParametersAndArguments.html#same_type_requirement"><em>同类型约束</em></a><br><em>一致性约束</em> → <a href="..\chapter3\03_Types.html#type_identifier"><em>类型标识</em></a> <strong>:</strong> <a href="..\chapter3\03_Types.html#type_identifier"><em>类型标识</em></a><br><em>一致性约束</em> → <a href="..\chapter3\03_Types.html#type_identifier"><em>类型标识</em></a> <strong>:</strong> <a href="..\chapter3\03_Types.html#protocol_composition_type"><em>协议合成类型</em></a><br><em>同类型约束</em> → <a href="..\chapter3\03_Types.html#type_identifier"><em>类型标识</em></a> <strong>==</strong> <a href="..\chapter3\03_Types.html#type_identifier"><em>类型标识</em></a> </p>
|
||||
<p>泛型形参子句(Generic Parameter Clause) 语法<br><em>泛型参数子句</em> → <strong><</strong> <a href="GenericParametersAndArguments.html#generic_parameter_list"><em>泛型参数列表</em></a> <a href="GenericParametersAndArguments.html#requirement_clause"><em>约束子句</em></a> <em>可选</em> <strong>></strong><br><em>泛型参数列表</em> → <a href="GenericParametersAndArguments.html#generic_parameter"><em>泛形参数</em></a> | <a href="GenericParametersAndArguments.html#generic_parameter"><em>泛形参数</em></a> <strong>,</strong> <a href="GenericParametersAndArguments.html#generic_parameter_list"><em>泛型参数列表</em></a><br><em>泛形参数</em> → <a href="..\chapter3\03_Types.html#type_name"><em>类型名称</em></a><br><em>泛形参数</em> → <a href="..\chapter3\03_Types.html#type_name"><em>类型名称</em></a> <strong>:</strong> <a href="..\chapter3\03_Types.html#type_identifier"><em>类型标识</em></a><br><em>泛形参数</em> → <a href="..\chapter3\03_Types.html#type_name"><em>类型名称</em></a> <strong>:</strong> <a href="..\chapter3\03_Types.html#protocol_composition_type"><em>协议合成类型</em></a><br><em>约束子句</em> → <strong>where</strong> <a href="GenericParametersAndArguments.html#requirement_list"><em>约束列表</em></a><br><em>约束列表</em> → <a href="GenericParametersAndArguments.html#requirement"><em>约束</em></a> | <a href="GenericParametersAndArguments.html#requirement"><em>约束</em></a> <strong>,</strong> <a href="GenericParametersAndArguments.html#requirement_list"><em>约束列表</em></a><br><em>约束</em> → <a href="GenericParametersAndArguments.html#conformance_requirement"><em>一致性约束</em></a> | <a href="GenericParametersAndArguments.html#same_type_requirement"><em>同类型约束</em></a><br><em>一致性约束</em> → <a href="..\chapter3\03_Types.html#type_identifier"><em>类型标识</em></a> <strong>:</strong> <a href="..\chapter3\03_Types.html#type_identifier"><em>类型标识</em></a><br><em>一致性约束</em> → <a href="..\chapter3\03_Types.html#type_identifier"><em>类型标识</em></a> <strong>:</strong> <a href="..\chapter3\03_Types.html#protocol_composition_type"><em>协议合成类型</em></a><br><em>同类型约束</em> → <a href="..\chapter3\03_Types.html#type_identifier"><em>类型标识</em></a> <strong>==</strong> <a href="..\chapter3\03_Types.html#type_identifier"><em>类型标识</em></a> </p>
|
||||
</blockquote>
|
||||
<p></p>
|
||||
|
||||
@ -998,7 +1002,7 @@
|
||||
<p></p>
|
||||
|
||||
<blockquote>
|
||||
<p>类型标识语法<br><em>类型标识</em> → <a href="..\chapter3\03_Types.html#type_name"><em>类名</em></a> <a href="GenericParametersAndArguments.html#generic_argument_clause"><em>泛型参数子句</em></a> <em>可选</em> | <a href="..\chapter3\03_Types.html#type_name"><em>类名</em></a> <a href="GenericParametersAndArguments.html#generic_argument_clause"><em>泛型参数子句</em></a> <em>可选</em> <strong>.</strong> <a href="..\chapter3\03_Types.html#type_identifier"><em>类型标识</em></a><br><em>类名</em> → <a href="LexicalStructure.html#identifier"><em>标识符</em></a> </p>
|
||||
<p>类型标识语法<br><em>类型标识</em> → <a href="..\chapter3\03_Types.html#type_name"><em>类型名称</em></a> <a href="GenericParametersAndArguments.html#generic_argument_clause"><em>泛型参数子句</em></a> <em>可选</em> | <a href="..\chapter3\03_Types.html#type_name"><em>类型名称</em></a> <a href="GenericParametersAndArguments.html#generic_argument_clause"><em>泛型参数子句</em></a> <em>可选</em> <strong>.</strong> <a href="..\chapter3\03_Types.html#type_identifier"><em>类型标识</em></a><br><em>类名</em> → <a href="LexicalStructure.html#identifier"><em>标识符</em></a> </p>
|
||||
</blockquote>
|
||||
<p></p>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.5" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="3.5" data-basepath=".." data-revision="1402673847562">
|
||||
<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_515">
|
||||
<section class="normal" id="section-gitbook_81">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:coverxit</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3" data-basepath=".." data-revision="1402644301599">
|
||||
<div class="book" data-level="3" data-basepath=".." data-revision="1402673847562">
|
||||
<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>
|
||||
|
||||
@ -44,7 +44,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="0" data-basepath="." data-revision="1402644301599">
|
||||
<div class="book" data-level="0" data-basepath="." data-revision="1402673847562">
|
||||
<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,7 +585,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_445">
|
||||
<section class="normal" id="section-gitbook_11">
|
||||
|
||||
<blockquote>
|
||||
<p>Swift 中文翻译组:364279588(要求对翻译感兴趣)</p>
|
||||
@ -644,6 +644,7 @@
|
||||
<li>lslxdx</li>
|
||||
<li>yeahdongcn</li>
|
||||
<li>zqp</li>
|
||||
<li>xiehurricane</li>
|
||||
</ul>
|
||||
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
CACHE MANIFEST
|
||||
# Revision 1402644301600
|
||||
# Revision 1402673847563
|
||||
|
||||
CACHE:
|
||||
index.html
|
||||
@ -14,13 +14,13 @@ chapter2/05_Control_Flow.html
|
||||
chapter2/06_Functions.html
|
||||
chapter2/07_Closures.html
|
||||
chapter2/08_Enumerations.html
|
||||
chapter2/10_Properties.html
|
||||
chapter2/12_Subscripts.html
|
||||
chapter2/09_Classes_and_Structures.html
|
||||
chapter2/02_Basic_Operators.html
|
||||
chapter2/10_Properties.html
|
||||
chapter2/11_Methods.html
|
||||
chapter2/15_Deinitialization.html
|
||||
chapter2/12_Subscripts.html
|
||||
chapter2/02_Basic_Operators.html
|
||||
chapter2/14_Initialization.html
|
||||
chapter2/15_Deinitialization.html
|
||||
chapter2/16_Automatic_Reference_Counting.html
|
||||
chapter2/17_Optional_Chaining.html
|
||||
chapter2/18_Type_Casting.html
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user