refine some contents

This commit is contained in:
numbbbbb
2014-06-10 18:50:57 +08:00
parent f6e3b497fd
commit a7b42bb013
41 changed files with 308 additions and 288 deletions

View File

@ -46,7 +46,7 @@
<div class="book" data-level="1.1" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="1.1" data-basepath=".." data-revision="1402397411238">
<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_82">
<section class="normal" id="section-gitbook_4">
<h1 id="-swift">关于 Swift</h1>
<p>Swift 是一种新的编程语言,用于编写 iOS 和 OS X 应用。Swift 结合了 C 和 Objective-C 的优点并且不受C的兼容性的限制。Swift 使用安全的编程模式并添加了很多新特性这将使编程更简单扩展性更强也更有趣。除此之外Swift 还支持人见人爱的 Cocoa 和 Cocoa Touch 框架。拥有了这些特性Swift将重新定义软件开发。</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="1.2" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="1.2" data-basepath=".." data-revision="1402397411238">
<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_83">
<section class="normal" id="section-gitbook_5">
<h1 id="swift-">Swift 初见</h1>
<p>本页内容包括:</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="1" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="1" data-basepath=".." data-revision="1402397411238">
<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_84">
<section class="normal" id="section-gitbook_6">
<h1 id="-swift">欢迎使用 Swift</h1>
<p>在本章中您将了解 Swift 的特性和开发历史,并对 Swift 有一个初步的了解。</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.1" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="2.1" data-basepath=".." data-revision="1402397411238">
<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_86">
<section class="normal" id="section-gitbook_8">
<h1 id="-">基础部分</h1>
<p>Swift 是 iOS 和 OS X 应用开发的一门新语言。然而,如果你有 C 或者 Objective-C 开发经验的话,你会发现 Swift 的很多内容都是你熟悉的。</p>
@ -625,34 +625,34 @@ var currentLoginAttempt = 0
</blockquote>
<h3 id="-">常量和变量的命名</h3>
<p>你可以用任何你喜欢的字符作为常量和变量名包括Unicode字符</p>
<pre><code> let π = 3.14159
let 你好 = &quot;你好世界&quot;
let 🐶🐮 = &quot;dogcow&quot;
<pre><code>let π = 3.14159
let 你好 = &quot;你好世界&quot;
let 🐶🐮 = &quot;dogcow&quot;
</code></pre><p>常量与变量名不能包含数学符号,箭头,保留的(或者非法的)Unicode码位连线与制表符。尽管常量与变量名中可以包含数字但是它们不能以数字打头。</p>
<p>一旦你将常量或者变量声明为确定的类型,你就不能使用相同的名字再次进行声明,或者以改变其存储的值为其他类型。同时,你也不能将常量与变量进行互转。</p>
<blockquote>
<p>注意如果你需要使用与Swift保留关键字相同的名称作为常量或者变量名你可以使用反引号(`)将关键字围住的方式将其作为名字使用。无论如何,你应当避免使用关键字作为常量或变量名,除非你别无选择。</p>
</blockquote>
<p>你可以更改现有的变量值为其他同类型的值,在下面的例子中,<code>friendlyWelcome</code>的值从<code>&quot;Hello!&quot;</code>改为了<code>&quot;Bonjour!&quot;</code>:</p>
<pre><code> var friendlyWelcome = &quot;Hello!&quot;
friendlyWelcome = &quot;Bonjour!&quot;
// friendlyWelcome is now &quot;Bonjour!&quot;
<pre><code>var friendlyWelcome = &quot;Hello!&quot;
friendlyWelcome = &quot;Bonjour!&quot;
// friendlyWelcome is now &quot;Bonjour!&quot;
</code></pre><p>和变量不一样,常量的值一旦被确定以后就不能更改了。尝试这样做会在编译时报错:</p>
<pre><code> let languageName = &quot;Swift&quot;
languageName = &quot;Swift++&quot;
// this is a compile-time error - languageName cannot be changed
<pre><code>let languageName = &quot;Swift&quot;
languageName = &quot;Swift++&quot;
// this is a compile-time error - languageName cannot be changed
</code></pre><h3 id="-">输出常量和变量</h3>
<p>你可以用<code>println</code>函数来输出当前常量或变量的值:</p>
<pre><code> println(friendlyWelcome)
// prints &quot;Bonjour!&quot;
<pre><code>println(friendlyWelcome)
// prints &quot;Bonjour!&quot;
</code></pre><p><code>println</code>是一个用来输出的全局函数输出的内容会在最后带换行。如果你用Xcode<code>println</code>将会输出内容到“console”面板上。(另一种函数叫<code>print</code>,唯一区别是在输出内容最后不会加入换行。)</p>
<p><code>println</code>函数输出传入的<code>String</code>值:</p>
<pre><code> println(&quot;This is a string&quot;)
// prints &quot;This is a string&quot;
<pre><code>println(&quot;This is a string&quot;)
// prints &quot;This is a string&quot;
</code></pre><p>像Cocoa里的<code>NSLog</code>函数一样,<code>println</code>函数可以输出更复杂的信息。这些信息可以包含当前常量和变量的值。</p>
<p>Swift用字符串插值string interpolation的方式把常量名或者变量名当做占位符加入到长字符串中Swift会用当前常量或变量的值替换这些占位符。将常量或变量名放入反斜杠符加一对圆括号中<code>&quot;\()&quot;</code></p>
<pre><code> println(&quot;The current value of friendlyWelcome is \(friendlyWelcome)&quot;)
// prints &quot;The current value of friendlyWelcome is Bonjour!
<pre><code>println(&quot;The current value of friendlyWelcome is \(friendlyWelcome)&quot;)
// prints &quot;The current value of friendlyWelcome is Bonjour!
</code></pre><blockquote>
<p>注意:字符串插值所有可用的选项在 字符串插值 这章中讲述。</p>
</blockquote>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.2" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="2.2" data-basepath=".." data-revision="1402397411238">
<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_113">
<section class="normal" id="section-gitbook_36">
<h1 id="-">基本运算符</h1>
<p>运算符是检查, 改变, 合并值的特殊符号或短语. 例如, 加号 <code>+</code> 把计算两个数的和(如 <code>let i = 1 + 2</code>). 复杂些的运行算包括逻辑与<code>&amp;&amp;</code>(如 <code>if enteredDoorCode &amp;&amp; passedRetinaScan</code>), 还有自增运算符 <code>++i</code> 这样让自身加一的便捷运算.</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.3" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="2.3" data-basepath=".." data-revision="1402397411238">
<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_87">
<section class="normal" id="section-gitbook_9">
<h1 id="-strings-and-characters-">字符串和字符 (Strings and Characters)</h1>
<hr>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.4" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="2.4" data-basepath=".." data-revision="1402397411238">
<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_99">
<section class="normal" id="section-gitbook_21">
<h1 id="-collection-types-">集合类型 (Collection Types)</h1>
<p>Swift语言提供经典的数组和字典两种集合类型来存储集合数据。数组用来按顺序存储相同类型的数据。字典虽然无序存储相同类型数据值但是需要由独有的标识符引用和寻址就是键值对</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.5" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="2.5" data-basepath=".." data-revision="1402397411238">
<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_100">
<section class="normal" id="section-gitbook_22">
<h1 id="-">控制流</h1>
<p>Swift提供了类似C语言的流程控制结构包括可以多次执行任务的<code>for</code><code>while</code>循环,基于特定条件选择执行不同代码分支的<code>if</code><code>switch</code>语句,还有控制流程跳转到其他代码的<code>break</code><code>continue</code>语句。</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.6" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="2.6" data-basepath=".." data-revision="1402397411238">
<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_101">
<section class="normal" id="section-gitbook_23">
<h1 id="-functions-">函数Functions</h1>
<p>本页包含内容:</p>
@ -605,67 +605,67 @@
<p>当你定义一个函数时你可以定义一个或多个有名字和类型的值作为函数的输入称为参数parameters也可以定义某种类型的值作为函数执行结束的输出称为返回类型</p>
<p>每个函数有个函数名用来描述函数执行的任务。要使用一个函数时你用函数名“调用”并传给它匹配的输入值称作实参arguments。一个函数的实参必须与函数参数表里参数的顺序一致。</p>
<p>在下面例子中的函数叫做<code>&quot;greetingForPerson&quot;</code>,之所以叫这个名字是因为这个函数用一个人的名字当做输入,并返回给这个人的问候语。为了完成这个任务,你定义一个输入参数-一个叫做<code>personName</code><code>String</code>值,和一个包含给这个人问候语的<code>String</code>类型的返回值:</p>
<pre><code> func sayHello(personName: String) -&gt; String {
let greeting = &quot;Hello, &quot; + personName + &quot;!&quot;
return greeting
}
<pre><code>func sayHello(personName: String) -&gt; String {
let greeting = &quot;Hello, &quot; + personName + &quot;!&quot;
return greeting
}
</code></pre><p>所有的这些信息汇总起来成为函数的定义,并以<code>func</code>作为前缀。指定函数返回类型时,用返回箭头<code>-&gt;</code>(一个连字符后跟一个右尖括号)后跟返回类型的名称的方式来表示。</p>
<p>该定义描述了函数做什么,它期望接收什么和执行结束时它返回的结果是什么。这样的定义使的函数可以在别的地方以一种清晰的方式被调用:</p>
<pre><code> println(sayHello(&quot;Anna&quot;))
// prints &quot;Hello, Anna!&quot;
println(sayHello(&quot;Brian&quot;))
// prints &quot;Hello, Brian!
<pre><code>println(sayHello(&quot;Anna&quot;))
// prints &quot;Hello, Anna!&quot;
println(sayHello(&quot;Brian&quot;))
// prints &quot;Hello, Brian!
</code></pre><p>调用<code>sayHello</code>函数时,在圆括号中传给它一个<code>String</code>类型的实参。因为这个函数返回一个<code>String</code>类型的值,<code>sayHello</code>可以被包含在<code>println</code>的调用中,用来输出这个函数的返回值,正如上面所示。</p>
<p><code>sayHello</code>的函数体中,先定义了一个新的名为<code>greeting</code><code>String</code>常量,同时赋值了给<code>personName</code>的一个简单问候消息。然后用<code>return</code>关键字把这个问候返回出去。一旦<code>return greeting</code>被调用,该函数结束它的执行并返回<code>greeting</code>的当前值。</p>
<p>你可以用不同的输入值多次调用<code>sayHello</code>。上面的例子展示的是用<code>&quot;Anna&quot;</code><code>&quot;Brian&quot;</code>调用的结果,该函数分别返回了不同的结果。</p>
<p>为了简化这个函数的定义,可以将问候消息的创建和返回写成一句:</p>
<pre><code> func sayHelloAgain(personName: String) -&gt; String {
return &quot;Hello again, &quot; + personName + &quot;!&quot;
}
println(sayHelloAgain(&quot;Anna&quot;))
// prints &quot;Hello again, Anna!
<pre><code>func sayHelloAgain(personName: String) -&gt; String {
return &quot;Hello again, &quot; + personName + &quot;!&quot;
}
println(sayHelloAgain(&quot;Anna&quot;))
// prints &quot;Hello again, Anna!
</code></pre><h2 id="-function-parameters-and-return-values-">函数参数与返回值Function Parameters and Return Values</h2>
<p>函数参数与返回值在Swift中极为灵活。你可以定义任何类型的函数包括从只带一个未名参数的简单函数到复杂的带有表达性参数名和不同参数选项的复杂函数。</p>
<h3 id="-multiple-input-parameters-">多重输入参数Multiple Input Parameters</h3>
<p>函数可以有多个输入参数,写在圆括号中,用逗号分隔。</p>
<p>下面这个函数用一个半开区间的开始点和结束点,计算出这个范围内包含多少数字:</p>
<pre><code> func halfOpenRangeLength(start: Int, end: Int) -&gt; Int {
return end - start
}
println(halfOpenRangeLength(1, 10))
// prints &quot;9
<pre><code>func halfOpenRangeLength(start: Int, end: Int) -&gt; Int {
return end - start
}
println(halfOpenRangeLength(1, 10))
// prints &quot;9
</code></pre><h3 id="-functions-without-parameters-">无参函数Functions Without Parameters</h3>
<p>函数可以没有参数。下面这个函数就是一个无参函数,当被调用时,它返回固定的<code>String</code>消息:</p>
<pre><code> func sayHelloWorld() -&gt; String {
return &quot;hello, world&quot;
}
println(sayHelloWorld())
// prints &quot;hello, world
<pre><code>func sayHelloWorld() -&gt; String {
return &quot;hello, world&quot;
}
println(sayHelloWorld())
// prints &quot;hello, world
</code></pre><p>尽管这个函数没有参数,但是定义中在函数名后还是需要一对圆括号。当被调用时,也需要在函数名后写一对圆括号。</p>
<h3 id="-functions-without-return-values-">无返回值函数Functions Without Return Values</h3>
<p>函数可以没有返回值。下面是<code>sayHello</code>函数的另一个版本,叫<code>waveGoodbye</code>,这个函数直接输出<code>String</code>值,而不是返回它:</p>
<pre><code> func sayGoodbye(personName: String) {
println(&quot;Goodbye, \(personName)!&quot;)
}
sayGoodbye(&quot;Dave&quot;)
// prints &quot;Goodbye, Dave!
<pre><code>func sayGoodbye(personName: String) {
println(&quot;Goodbye, \(personName)!&quot;)
}
sayGoodbye(&quot;Dave&quot;)
// prints &quot;Goodbye, Dave!
</code></pre><p>因为这个函数不需要返回值,所以这个函数的定义中没有返回箭头(-&gt;)和返回类型。</p>
<blockquote>
<p>注意:
严格上来说,虽然没有返回值被定义,<code>sayGoodbye</code>函数依然返回了值。没有定义返回类型的函数会返回特殊的值,叫<code>Void</code>。它其实是一个空的元组tuple没有任何元素可以写成<code>()</code></p>
</blockquote>
<p>被调用时,一个函数的返回值可以被忽略:</p>
<pre><code> func printAndCount(stringToPrint: String) -&gt; Int {
println(stringToPrint)
return countElements(stringToPrint)
}
func printWithoutCounting(stringToPrint: String) {
printAndCount(stringToPrint)
}
printAndCount(&quot;hello, world&quot;)
// prints &quot;hello, world&quot; and returns a value of 12
printWithoutCounting(&quot;hello, world&quot;)
// prints &quot;hello, world&quot; but does not return a value
<pre><code>func printAndCount(stringToPrint: String) -&gt; Int {
println(stringToPrint)
return countElements(stringToPrint)
}
func printWithoutCounting(stringToPrint: String) {
printAndCount(stringToPrint)
}
printAndCount(&quot;hello, world&quot;)
// prints &quot;hello, world&quot; and returns a value of 12
printWithoutCounting(&quot;hello, world&quot;)
// prints &quot;hello, world&quot; but does not return a value
</code></pre><p>第一个函数<code>printAndCount</code>,输出一个字符串并返回<code>Int</code>类型的字符数。第二个函数<code>printWithoutCounting</code>调用了第一个函数,但是忽略了它的返回值。当第二个函数被调用时,消息依然会由第一个函数输出,但是返回值不会被用到。</p>
<blockquote>
<p>注意:
@ -674,59 +674,59 @@
<h3 id="-functions-with-multiple-return-values-">多重返回值函数Functions with Multiple Return Values</h3>
<p>你可以用元组tuple类型让多个值作为一个复合值从函数中返回。</p>
<p>下面的这个例子中,<code>count</code>函数用来计算一个字符串中元音,辅音和其他字母的个数(基于美式英语的标准)。</p>
<pre><code> func count(string: String) -&gt; (vowels: Int, consonants: Int, others: Int) {
var vowels = 0, consonants = 0, others = 0
for character in string {
switch String(character).lowercaseString {
case &quot;a&quot;, &quot;e&quot;, &quot;i&quot;, &quot;o&quot;, &quot;u&quot;:
++vowels
case &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;f&quot;, &quot;g&quot;, &quot;h&quot;, &quot;j&quot;, &quot;k&quot;, &quot;l&quot;, &quot;m&quot;,
&quot;n&quot;, &quot;p&quot;, &quot;q&quot;, &quot;r&quot;, &quot;s&quot;, &quot;t&quot;, &quot;v&quot;, &quot;w&quot;, &quot;x&quot;, &quot;y&quot;, &quot;z&quot;:
++consonants
default:
++others
}
}
return (vowels, consonants, others)
<pre><code>func count(string: String) -&gt; (vowels: Int, consonants: Int, others: Int) {
var vowels = 0, consonants = 0, others = 0
for character in string {
switch String(character).lowercaseString {
case &quot;a&quot;, &quot;e&quot;, &quot;i&quot;, &quot;o&quot;, &quot;u&quot;:
++vowels
case &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;f&quot;, &quot;g&quot;, &quot;h&quot;, &quot;j&quot;, &quot;k&quot;, &quot;l&quot;, &quot;m&quot;,
&quot;n&quot;, &quot;p&quot;, &quot;q&quot;, &quot;r&quot;, &quot;s&quot;, &quot;t&quot;, &quot;v&quot;, &quot;w&quot;, &quot;x&quot;, &quot;y&quot;, &quot;z&quot;:
++consonants
default:
++others
}
}
return (vowels, consonants, others)
}
</code></pre><p>你可以用<code>count</code>函数来处理任何一个字符串,返回的值将是一个包含三个<code>Int</code>型值的元组tuple</p>
<pre><code> let total = count(&quot;some arbitrary string!&quot;)
println(&quot;\(total.vowels) vowels and \(total.consonants) consonants&quot;)
// prints &quot;6 vowels and 13 consonants
<pre><code>let total = count(&quot;some arbitrary string!&quot;)
println(&quot;\(total.vowels) vowels and \(total.consonants) consonants&quot;)
// prints &quot;6 vowels and 13 consonants
</code></pre><p>需要注意的是,元组的成员不需要在函数中返回时命名,因为它们的名字已经在函数返回类型有有了定义。</p>
<h2 id="-function-parameter-names-">函数参数名Function Parameter Names</h2>
<p>以上所有的函数都给它们的参数定义了<code>参数名parameter name</code></p>
<pre><code> func someFunction(parameterName: Int) {
// function body goes here, and can use parameterName
// to refer to the argument value for that parameter
}
<pre><code>func someFunction(parameterName: Int) {
// function body goes here, and can use parameterName
// to refer to the argument value for that parameter
}
</code></pre><p>但是,这些参数名仅在函数体中使用,不能在函数调用时使用。这种类型的参数名被称作<code>局部参数名local parameter name</code>,因为它们只能在函数体中使用。</p>
<h3 id="-external-parameter-names-">外部参数名External Parameter Names</h3>
<p>有时候,调用函数时,给每个参数命名是非常有用的,因为这些参数名可以指出各个实参的用途是什么。</p>
<p>如果你希望函数的使用者在调用函数时提供参数名字,那就需要给每个参数除了局部参数名外再定义一个<code>外部参数名</code>。外部参数名写在局部参数名之前,用空格分隔。</p>
<pre><code> func someFunction(externalParameterName localParameterName: Int) {
// function body goes here, and can use localParameterName
// to refer to the argument value for that parameter
}
<pre><code>func someFunction(externalParameterName localParameterName: Int) {
// function body goes here, and can use localParameterName
// to refer to the argument value for that parameter
}
</code></pre><blockquote>
<p>注意:
如果你提供了外部参数名,那么函数在被调用时,必须使用外部参数名。</p>
</blockquote>
<p>以下是个例子,这个函数使用一个<code>结合者joiner</code>把两个字符串联在一起:</p>
<pre><code> func join(s1: String, s2: String, joiner: String) -&gt; String {
return s1 + joiner + s2
}
<pre><code>func join(s1: String, s2: String, joiner: String) -&gt; String {
return s1 + joiner + s2
}
</code></pre><p>当你调用这个函数时,这三个字符串的用途是不清楚的:</p>
<pre><code> join(&quot;hello&quot;, &quot;world&quot;, &quot;, &quot;)
// returns &quot;hello, world
<pre><code>join(&quot;hello&quot;, &quot;world&quot;, &quot;, &quot;)
// returns &quot;hello, world
</code></pre><p>为了让这些字符串的用途更为明显,我们为<code>join</code>函数添加外部参数名:</p>
<pre><code> func join(string s1: String, toString s2: String, withJoiner joiner: String) -&gt; String {
return s1 + joiner + s2
}
<pre><code>func join(string s1: String, toString s2: String, withJoiner joiner: String) -&gt; String {
return s1 + joiner + s2
}
</code></pre><p>在这个版本的<code>join</code>函数中,第一个参数有一个叫<code>string</code>的外部参数名和<code>s1</code>的局部参数名,第二个参数有一个叫<code>toString</code>的外部参数名和<code>s2</code>的局部参数名,第三个参数有一个叫<code>withJoiner</code>的外部参数名和<code>joiner</code>的局部参数名。</p>
<p>现在,你可以使用这些外部参数名以一种清晰地方式来调用函数了:</p>
<pre><code> join(string: &quot;hello&quot;, toString: &quot;world&quot;, withJoiner: &quot;, &quot;)
// returns &quot;hello, world
<pre><code>join(string: &quot;hello&quot;, toString: &quot;world&quot;, withJoiner: &quot;, &quot;)
// returns &quot;hello, world
</code></pre><p>使用外部参数名让第二个版本的<code>join</code>函数的调用更为有表现力,更为通顺,同时还保持了函数体是可读的和有明确意图的。</p>
<blockquote>
<p>注意:
@ -735,17 +735,17 @@
<h3 id="-shorthand-external-parameter-names-">简写外部参数名Shorthand External Parameter Names</h3>
<p>如果你需要提供外部参数名,但是局部参数名已经定义好了,那么你不需要写两次这些参数名。相反,只写一次参数名,并用<code>井号(#</code>作为前缀就可以了。这告诉Swift使用这个参数名作为局部和外部参数名。</p>
<p>下面这个例子定义了一个叫<code>containsCharacter</code>的函数,使用<code>井号(#</code>的方式定义了外部参数名:</p>
<pre><code> func containsCharacter(#string: String, #characterToFind: Character) -&gt; Bool {
for character in string {
if character == characterToFind {
return true
}
}
return false
<pre><code>func containsCharacter(#string: String, #characterToFind: Character) -&gt; Bool {
for character in string {
if character == characterToFind {
return true
}
}
return false
}
</code></pre><p>这样定义参数名,使得函数体更为可读,清晰,同时也可以以一个不含糊的方式被调用:</p>
<pre><code> let containsAVee = containsCharacter(string: &quot;aardvark&quot;, characterToFind: &quot;v&quot;)
// containsAVee equals true, because &quot;aardvark&quot; contains a &quot;v”
<pre><code>let containsAVee = containsCharacter(string: &quot;aardvark&quot;, characterToFind: &quot;v&quot;)
// containsAVee equals true, because &quot;aardvark&quot; contains a &quot;v”
</code></pre><h3 id="-default-parameter-values-">默认参数值Default Parameter Values</h3>
<p>你可以在函数体中为每个参数定义<code>默认值</code>。当默认值被定义后,调用这个函数时可以略去这个参数。</p>
<blockquote>
@ -753,25 +753,25 @@
将带有默认值的参数放在函数参数表的最后。这样可以保证在函数调用时,非默认参数的顺序是一致的,同时使得相同的函数在不同情况下调用时显得更为清晰。</p>
</blockquote>
<p>以下是另一个版本的<code>join</code>函数,其中<code>joiner</code>有了默认参数值:</p>
<pre><code> func join(string s1: String, toString s2: String, withJoiner joiner: String = &quot; &quot;) -&gt; String {
return s1 + joiner + s2
}
<pre><code>func join(string s1: String, toString s2: String, withJoiner joiner: String = &quot; &quot;) -&gt; String {
return s1 + joiner + s2
}
</code></pre><p>像第一个版本的<code>join</code>函数一样,如果<code>joiner</code>被幅值时,函数将使用这个字符串值来连接两个字符串:</p>
<pre><code> join(string: &quot;hello&quot;, toString: &quot;world&quot;, withJoiner: &quot;-&quot;)
// returns &quot;hello-world
<pre><code>join(string: &quot;hello&quot;, toString: &quot;world&quot;, withJoiner: &quot;-&quot;)
// returns &quot;hello-world
</code></pre><p>当这个函数被调用时,如果<code>joiner</code>的值没有被指定,函数会使用默认值(&quot; &quot;</p>
<pre><code> join(string: &quot;hello&quot;, toString:&quot;world&quot;)
// returns &quot;hello world&quot;
<pre><code>join(string: &quot;hello&quot;, toString:&quot;world&quot;)
// returns &quot;hello world&quot;
</code></pre><h3 id="-external-names-for-parameters-with-default-values-">默认值参数的外部参数名External Names for Parameters with Default Values</h3>
<p>在大多数情况下,给带默认值的参数起一个外部参数名是很有用的。这样可以保证当函数被调用且带默认值的参数被提供值时,实参的意图是明显的。</p>
<p>为了使定义外部参数名更加简单当你未给带默认值的参数提供外部参数名时Swift会自动提供外部名字。此时外部参数名与局部名字是一样的就像你已经在局部参数名前写了<code>井号(#</code>一样。</p>
<p>下面是<code>join</code>函数的另一个版本,这个版本中并没有为它的参数提供外部参数名,但是<code>joiner</code>参数依然有外部参数名:</p>
<pre><code> func join(s1: String, s2: String, joiner: String = &quot; &quot;) -&gt; String {
return s1 + joiner + s2
}
<pre><code>func join(s1: String, s2: String, joiner: String = &quot; &quot;) -&gt; String {
return s1 + joiner + s2
}
</code></pre><p>在这个例子中Swift自动为<code>joiner</code>提供了外部参数名。因此,当函数调用时,外部参数名必须使用,这样使得参数的用途变得清晰。</p>
<pre><code> join(&quot;hello&quot;, &quot;world&quot;, joiner: &quot;-&quot;)
// returns &quot;hello-world&quot;
<pre><code>join(&quot;hello&quot;, &quot;world&quot;, joiner: &quot;-&quot;)
// returns &quot;hello-world&quot;
</code></pre><blockquote>
<p>注意:
你可以使用<code>下划线_</code>作为默认值参数的外部参数名,这样可以在调用时不用提供外部参数名。但是给带默认值的参数命名总是更加合适的。</p>
@ -780,17 +780,17 @@
<p>一个<code>可变参数variadic parameter</code>可以接受一个或多个值。函数调用时,你可以用可变参数来传入不确定数量的输入参数。通过在变量类型名后面加入<code>...</code>的方式来定义可变参数。</p>
<p>传入可变参数的值在函数体内当做这个类型的一个数组。例如,一个叫做<code>numbers</code><code>Double...</code>型可变参数,在函数体内可以当做一个叫<code>numbers</code><code>Double[]</code>型的数组常量。</p>
<p>下面的这个函数用来计算一组任意长度数字的算术平均数:</p>
<pre><code> func arithmeticMean(numbers: Double...) -&gt; Double {
var total: Double = 0
for number in numbers {
total += number
}
return total / Double(numbers.count)
}
arithmeticMean(1, 2, 3, 4, 5)
// returns 3.0, which is the arithmetic mean of these five numbers
arithmeticMean(3, 8, 19)
// returns 10.0, which is the arithmetic mean of these three numbers
<pre><code>func arithmeticMean(numbers: Double...) -&gt; Double {
var total: Double = 0
for number in numbers {
total += number
}
return total / Double(numbers.count)
}
arithmeticMean(1, 2, 3, 4, 5)
// returns 3.0, which is the arithmetic mean of these five numbers
arithmeticMean(3, 8, 19)
// returns 10.0, which is the arithmetic mean of these three numbers
</code></pre><blockquote>
<p>注意:
一个函数至多能有一个可变参数,而且它必须是参数表中最后的一个。这样做是为了避免函数调用时出现歧义。</p>
@ -800,17 +800,17 @@
<p>函数参数默认是常量。试图在函数体中更改参数值将会导致编译错误。这意味着你不能错误地更改参数值。</p>
<p>但是,有时候,如果函数中有传入参数的变量值副本将是很有用的。你可以通过指定一个或多个参数为变量参数,从而避免自己在函数中定义新的变量。变量参数不是常量,你可以在函数中把它当做新的可修改副本来使用。</p>
<p>通过在参数名前加关键字<code>var</code>来定义变量参数:</p>
<pre><code> func alignRight(var string: String, count: Int, pad: Character) -&gt; String {
let amountToPad = count - countElements(string)
for _ in 1...amountToPad {
string = pad + string
}
return string
}
let originalString = &quot;hello&quot;
let paddedString = alignRight(originalString, 10, &quot;-&quot;)
// paddedString is equal to &quot;-----hello&quot;
// originalString is still equal to &quot;hello
<pre><code>func alignRight(var string: String, count: Int, pad: Character) -&gt; String {
let amountToPad = count - countElements(string)
for _ in 1...amountToPad {
string = pad + string
}
return string
}
let originalString = &quot;hello&quot;
let paddedString = alignRight(originalString, 10, &quot;-&quot;)
// paddedString is equal to &quot;-----hello&quot;
// originalString is still equal to &quot;hello&quot;
</code></pre><p>这个例子中定义了一个新的叫做<code>alignRight</code>的函数,用来右对齐输入的字符串到一个长的输出字符串中。左侧空余的地方用指定的填充字符填充。这个例子中,字符串<code>&quot;hello&quot;</code>被转换成了<code>&quot;-----hello&quot;</code></p>
<p><code>alignRight</code>函数将参数<code>string</code>定义为变量参数。这意味着<code>string</code>现在可以作为一个局部变量,用传入的字符串值初始化,并且可以在函数体中进行操作。</p>
<p>该函数首先计算出多少个字符需要被添加到<code>string</code>的左边,以右对齐到总的字符串中。这个值存在局部常量<code>amountToPad</code>中。这个函数然后将<code>amountToPad</code>多的填充pad字符填充到<code>string</code>左边,并返回结果。它使用了<code>string</code>这个变量参数来进行所有字符串操作。</p>
@ -827,18 +827,18 @@
输入输出参数不能有默认值,而且变量参数不能用<code>inout</code>标记。如果你用<code>inout</code>标记一个参数,这个参数不能别<code>var</code>或者<code>let</code>标记。</p>
</blockquote>
<p>下面是例子,<code>swapTwoInts</code>函数,有两个分别叫做<code>a</code><code>b</code>的输出输出参数:</p>
<pre><code> func swapTwoInts(inout a: Int, inout b: Int) {
let temporaryA = a
a = b
b = temporaryA
}
<pre><code>func swapTwoInts(inout a: Int, inout b: Int) {
let temporaryA = a
a = b
b = temporaryA
}
</code></pre><p>这个<code>swapTwoInts</code>函数仅仅交换<code>a</code><code>b</code>的值。该函数先将<code>a</code>的值存到一个暂时常量<code>temporaryA</code>中,然后将<code>b</code>的值赋给<code>a</code>,最后将<code>temporaryA</code>幅值给<code>b</code></p>
<p>你可以用两个<code>Int</code>型的变量来调用<code>swapTwoInts</code>。需要注意的是,<code>someInt</code><code>anotherInt</code>在传入<code>swapTwoInts</code>函数前,都加了<code>&amp;</code>的前缀:</p>
<pre><code> var someInt = 3
var anotherInt = 107
swapTwoInts(&amp;someInt, &amp;anotherInt)
println(&quot;someInt is now \(someInt), and anotherInt is now \(anotherInt)&quot;)
// prints &quot;someInt is now 107, and anotherInt is now 3”
<pre><code>var someInt = 3
var anotherInt = 107
swapTwoInts(&amp;someInt, &amp;anotherInt)
println(&quot;someInt is now \(someInt), and anotherInt is now \(anotherInt)&quot;)
// prints &quot;someInt is now 107, and anotherInt is now 3”
</code></pre><p>从上面这个例子中,我们可以看到<code>someInt</code><code>anotherInt</code>的原始值在<code>swapTwoInts</code>函数中被修改,尽管它们的定义在函数体外。</p>
<blockquote>
<p>注意:
@ -847,98 +847,98 @@
<h2 id="-function-types-">函数类型Function Types</h2>
<p>每个函数都有种特定的函数类型,由函数的参数类型和返回类型组成。</p>
<p>例如:</p>
<pre><code> func addTwoInts(a: Int, b: Int) -&gt; Int {
return a + b
}
func multiplyTwoInts(a: Int, b: Int) -&gt; Int {
return a * b
}
<pre><code>func addTwoInts(a: Int, b: Int) -&gt; Int {
return a + b
}
func multiplyTwoInts(a: Int, b: Int) -&gt; Int {
return a * b
}
</code></pre><p>这个例子中定义了两个简单的数学函数:<code>addTwoInts</code><code>multiplyTwoInts</code>。这两个函数都传入两个<code>Int</code>类型, 返回一个合适的<code>Int</code>值。</p>
<p>这两个函数的类型是<code>(Int, Int) -&gt; Int</code>,可以读作“这个函数类型,它有两个<code>Int</code>型的参数并返回一个<code>Int</code>型的值。”。</p>
<p>下面是另一个例子,一个没有参数,也没有返回值的函数:</p>
<pre><code> func printHelloWorld() {
println(&quot;hello, world&quot;)
}
<pre><code>func printHelloWorld() {
println(&quot;hello, world&quot;)
}
</code></pre><p>这个函数的类型是:<code>() -&gt; ()</code>,或者叫“没有参数,并返回<code>Void</code>类型的函数。”。没有指定返回类型的函数总返回 <code>Void</code>。在Swift中<code>Void</code>与空的元组是一样的。</p>
<h3 id="-using-function-types-">使用函数类型Using Function Types</h3>
<p>在Swift中使用函数类型就像使用其他类型一样。例如你可以定义一个常量或变量它的类型是函数并且可以幅值为一个函数</p>
<pre><code> var mathFunction: (Int, Int) -&gt; Int = addTwoInts
<pre><code>var mathFunction: (Int, Int) -&gt; Int = addTwoInts
</code></pre><p>这个可以读作:</p>
<p>“定义一个叫做<code>mathFunction</code>的变量,类型是‘一个有两个<code>Int</code>型的参数并返回一个<code>Int</code>型的值的函数’,并让这个新变量指向<code>addTwoInts</code>函数”。</p>
<p><code>addTwoInts</code><code>mathFunction</code>有同样的类型所以这个赋值过程在Swift类型检查中是允许的。</p>
<p>现在,你可以用<code>mathFunction</code>来调用被赋值的函数了:</p>
<pre><code> println(&quot;Result: \(mathFunction(2, 3))&quot;)
// prints &quot;Result: 5
<pre><code>println(&quot;Result: \(mathFunction(2, 3))&quot;)
// prints &quot;Result: 5
</code></pre><p>有相同匹配类型的不同函数可以被赋值给同一个变量,就像非函数类型的变量一样:</p>
<pre><code> mathFunction = multiplyTwoInts
println(&quot;Result: \(mathFunction(2, 3))&quot;)
// prints &quot;Result: 6&quot;
<pre><code>mathFunction = multiplyTwoInts
println(&quot;Result: \(mathFunction(2, 3))&quot;)
// prints &quot;Result: 6&quot;
</code></pre><p>就像其他类型一样当赋值一个函数给常量或变量时你可以让Swift来推测其函数类型</p>
<pre><code> let anotherMathFunction = addTwoInts
// anotherMathFunction is inferred to be of type (Int, Int) -&gt; Int
<pre><code>let anotherMathFunction = addTwoInts
// anotherMathFunction is inferred to be of type (Int, Int) -&gt; Int
</code></pre><h3 id="-function-types-as-parameter-types-">函数类型作为参数类型Function Types as Parameter Types</h3>
<p>你可以用<code>(Int, Int) -&gt; Int</code>这样的函数类型作为另一个函数的参数类型。这样你可以将函数的一部分实现交由给函数的调用者。</p>
<p>下面是另一个例子,正如上面的函数一样,同样是输出某种数学运算结果:</p>
<pre><code> func printMathResult(mathFunction: (Int, Int) -&gt; Int, a: Int, b: Int) {
println(&quot;Result: \(mathFunction(a, b))&quot;)
}
printMathResult(addTwoInts, 3, 5)
// prints &quot;Result: 8”
<pre><code>func printMathResult(mathFunction: (Int, Int) -&gt; Int, a: Int, b: Int) {
println(&quot;Result: \(mathFunction(a, b))&quot;)
}
printMathResult(addTwoInts, 3, 5)
// prints &quot;Result: 8”
</code></pre><p>这个例子定义了<code>printMathResult</code>函数,它有三个参数:第一个参数叫<code>mathFunction</code>,类型是<code>(Int, Int) -&gt; Int</code>,你可以传入任何这种类型的函数;第二个和第三个参数叫<code>a</code><code>b</code>,它们的类型都是<code>Int</code>,这两个值作为已给的函数的输入值。</p>
<p><code>printMathResult</code>被调用时,它被传入<code>addTwoInts</code>函数和整数<code>3</code><code>5</code>。它用传入<code>3</code><code>5</code>调用<code>addTwoInts</code>,并输出结果:<code>8</code></p>
<p><code>printMathResult</code>函数的作用就是输出另一个合适类型的数学函数的调用结果。它不关心传入函数是如何实现的,它只关心这个传入的函数类型是正确的。这使得<code>printMathResult</code>可以以一种类型安全type-safe的方式来保证传入函数的调用是正确的。</p>
<h3 id="-function-type-as-return-types-">函数类型作为返回类型Function Type as Return Types</h3>
<p>你可以用函数类型作为另一个函数的返回类型。你需要做的是在返回箭头(<code>-&gt;</code>)后写一个完整的函数类型。</p>
<p>下面的这个例子中定义了两个简单函数,分别是<code>stepForward</code><code>stepBackward</code><code>stepForward</code>函数返回一个比输入值大一的值。<code>stepBackward</code>函数返回一个比输入值小一的值。这两个函数的类型都是<code>(Int) -&gt; Int</code></p>
<pre><code> func stepForward(input: Int) -&gt; Int {
return input + 1
}
func stepBackward(input: Int) -&gt; Int {
return input - 1
}
<pre><code>func stepForward(input: Int) -&gt; Int {
return input + 1
}
func stepBackward(input: Int) -&gt; Int {
return input - 1
}
</code></pre><p>下面这个叫做<code>chooseStepFunction</code>的函数,它的返回类型是<code>(Int) -&gt; Int</code>的函数。<code>chooseStepFunction</code>根据布尔值<code>backwards</code>来返回<code>stepForward</code>函数或<code>stepBackward</code>函数:</p>
<pre><code> func chooseStepFunction(backwards: Bool) -&gt; (Int) -&gt; Int {
return backwards ? stepBackward : stepForward
}
<pre><code>func chooseStepFunction(backwards: Bool) -&gt; (Int) -&gt; Int {
return backwards ? stepBackward : stepForward
}
</code></pre><p>你现在可以用<code>chooseStepFunction</code>来获得一个函数,不管是那个方向:</p>
<pre><code> var currentValue = 3
let moveNearerToZero = chooseStepFunction(currentValue &gt; 0)
// moveNearerToZero now refers to the stepBackward() function
<pre><code>var currentValue = 3
let moveNearerToZero = chooseStepFunction(currentValue &gt; 0)
// moveNearerToZero now refers to the stepBackward() function
</code></pre><p>上面这个例子中计算出从<code>currentValue</code>逐渐接近到<code>0</code>是需要向正数走还是向负数走。<code>currentValue</code>的初始值是<code>3</code>,这意味着<code>currentValue &gt; 0</code>是真的(<code>true</code>),这将使得<code>chooseStepFunction</code>返回<code>stepBackward</code>函数。一个指向返回的函数的引用保存在了<code>moveNearerToZero</code>常量中。</p>
<p>现在,<code>moveNearerToZero</code>指向了正确的函数,它可以被用来数到<code>0</code></p>
<pre><code> println(&quot;Counting to zero:&quot;)
// Counting to zero:
while currentValue != 0 {
println(&quot;\(currentValue)... &quot;)
currentValue = moveNearerToZero(currentValue)
}
println(&quot;zero!&quot;)
// 3...
// 2...
// 1...
// zero!
<pre><code>println(&quot;Counting to zero:&quot;)
// Counting to zero:
while currentValue != 0 {
println(&quot;\(currentValue)... &quot;)
currentValue = moveNearerToZero(currentValue)
}
println(&quot;zero!&quot;)
// 3...
// 2...
// 1...
// zero!
</code></pre><h2 id="-nested-functions-">嵌套函数Nested Functions</h2>
<p>这章中你所见到的所有函数都叫全局函数global functions它们定义在全局域中。你也可以把函数定义在别的函数体中称作嵌套函数nested functions</p>
<p>默认情况下嵌套函数是对外界不可见的但是可以被他们封闭函数enclosing function来调用。一个封闭函数也可以返回它的某一个嵌套函数使得这个函数可以在其他域中被使用。</p>
<p>你可以用返回嵌套函数的方式重写<code>chooseStepFunction</code>函数:</p>
<pre><code> func chooseStepFunction(backwards: Bool) -&gt; (Int) -&gt; Int {
func stepForward(input: Int) -&gt; Int { return input + 1 }
func stepBackward(input: Int) -&gt; Int { return input - 1 }
return backwards ? stepBackward : stepForward
}
var currentValue = -4
let moveNearerToZero = chooseStepFunction(currentValue &gt; 0)
// moveNearerToZero now refers to the nested stepForward() function
while currentValue != 0 {
println(&quot;\(currentValue)... &quot;)
currentValue = moveNearerToZero(currentValue)
}
println(&quot;zero!&quot;)
// -4...
// -3...
// -2...
// -1...
// zero!
<pre><code>func chooseStepFunction(backwards: Bool) -&gt; (Int) -&gt; Int {
func stepForward(input: Int) -&gt; Int { return input + 1 }
func stepBackward(input: Int) -&gt; Int { return input - 1 }
return backwards ? stepBackward : stepForward
}
var currentValue = -4
let moveNearerToZero = chooseStepFunction(currentValue &gt; 0)
// moveNearerToZero now refers to the nested stepForward() function
while currentValue != 0 {
println(&quot;\(currentValue)... &quot;)
currentValue = moveNearerToZero(currentValue)
}
println(&quot;zero!&quot;)
// -4...
// -3...
// -2...
// -1...
// zero!
</code></pre>
</section>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.7" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="2.7" data-basepath=".." data-revision="1402397411238">
<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_102">
<section class="normal" id="section-gitbook_24">
<h1 id="-">闭包</h1>
<hr>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.8" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="2.8" data-basepath=".." data-revision="1402397411238">
<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_108">
<section class="normal" id="section-gitbook_30">
<h1 id="-">枚举</h1>
<p>本页内容包含:</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.9" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="2.9" data-basepath=".." data-revision="1402397411238">
<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_109">
<section class="normal" id="section-gitbook_31">
<h3 id="-">类和结构体</h3>
<p>本页包含内容:</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.10" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="2.10" data-basepath=".." data-revision="1402397411238">
<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,15 +587,25 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_110">
<section class="normal" id="section-gitbook_32">
<h1 id="-properties-">属性 (Properties)</h1>
<hr>
<p>本页包含内容:</p>
<ul>
<li><a href="#stored_properties">存储属性Stored Properties</a></li>
<li><a href="#computed_properties">计算属性Computed Properties</a></li>
<li><a href="#property_observers">属性监视器Property Observers</a></li>
<li><a href="global_and_local_variables">全局变量和局部变量Global and Local Variables</a></li>
<li><a href="#type_properties">类属性Type Properties</a></li>
</ul>
<p><strong>属性</strong>将值跟特定的类、结构或枚举关联。一种是存储属性,把常量或变量的值作为实例的一部分,一种是计算属性,它计算一个值。计算属性可以用于类、结构和枚举里,存储属性只能用于类和结构。</p>
<p>存储属性和计算属性通常用于特定类型的实例,但是,属性也可以直接用于类型本身,这种属性称为类属性。</p>
<p>另外,还可以定义属性监视器来监控属性值的变化,以此来触发一个自定义的操作。属性监视器可以添加到自己写的存储属性上,也可以添加到从父类继承的属性上。</p>
<p><a name="stored_properties"></a></p>
<h2 id="-">存储属性</h2>
<p>简单来说,一个存储属性就是一个特定类型实例里表示常量或变量的部分,存储属性可以是<em>变量存储属性</em>(用关键字<code>var</code>定义),也可以是<em>常量存储属性</em>(用关键字<code>let</code>定义)。</p>
<p>可以在定义存储属性的时候指定默认值,详见<a href="chapter2/14_Initialization.md#">默认属性值</a>一节。也可以在初始化阶段设置或修改存储属性的值,甚至修改常量存储属性的值,详见<a href="chapter2/14_Initialization.md#">在初始化阶段修改常量存储属性</a>一节。</p>
<p>可以在定义存储属性的时候指定默认值,详见<a href="chapter2/14_Initialization.html#default_property_values">默认属性值</a>一节。也可以在初始化阶段设置或修改存储属性的值,甚至修改常量存储属性的值,详见<a href="chapter2/14_Initialization.html#modifying_constant_properties_during_initialization">在初始化阶段修改常量存储属性</a>一节。</p>
<p>下面的例子定义了一个名为<code>FixedLengthRange</code>的结构体,表示一个在创建后无法修改整数范围的类型:</p>
<pre><code>struct FixedLengthRange {
var firstValue: Int
@ -606,6 +616,7 @@ var rangeOfThreeItems = FixedLengthRange(firstValue: 0, length: 3)
rangeOfThreeItems.firstValue = 6
// the range now represents integer values 6, 7, and 8
</code></pre><p><code>FixedLengthRange</code>的实例包含一个名为<code>firstValue</code>的变量存储属性和一个名为<code>length</code>的常量存储属性。在上面的例子中,<code>length</code>在创建实例的时候被赋值,因为它是一个常量存储属性,所以无法修改它的值。</p>
<p><a name="stored_properties_of_constant_structure_instances"></a></p>
<h3 id="-">常量和存储属性</h3>
<p>如果创建了一个结构体的实例并赋值给一个常量,则无法修改实例的任何属性,即使定义了变量存储属性:</p>
<pre><code>let rangeOfFourItems = FixedLengthRange(firstValue: 0, length: 4)
@ -650,10 +661,12 @@ manager.data += &quot;Some more data&quot;
<pre><code>println(manager.importer.fileName)
// the DataImporter instance for the importer property has now been created
// prints &quot;data.txt”
</code></pre><h3 id="-">存储属性和实例变量</h3>
</code></pre><p><a name="stored_properties_and_instance_variables"></a></p>
<h3 id="-">存储属性和实例变量</h3>
<p>如果您有过 Objective-C 经验,应该知道有两种方式在类实例存储值和引用。对于属性来说,也可以使用实例变量作为属性值的后端存储。</p>
<p>Swift 编程语言中把这些理论统一用属性来实现。Swift 中的属性没有对应的实例变量,属性的后端存储也无法直接访问。这就避免了不同场景下访问方式的困扰,同时也将属性的定义简化成一个语句。
一个类型中属性的全部信息——包括命名、类型和内存管理特征——都在唯一一个地方定义。</p>
<p><a name="computed_properties"></a></p>
<h2 id="-">计算属性</h2>
<p>除存储属性外,类、结构体和枚举可以定义<em>计算属性</em>,计算属性不直接存储值,而是提供一个 getter 来获取值,一个可选的 setter 来间接设置其他属性或变量的值。</p>
<pre><code>struct Point {
@ -694,6 +707,7 @@ println(&quot;square.origin is now at (\(square.origin.x), \(square.origin.y))&q
<p><code>square</code><code>center</code>属性可以通过点运算符(<code>square.center</code>来访问这会调用getter来获取属性的值。跟直接返回已经存在的值不同getter实际上通过计算然后返回一个新的<code>Point</code>实例表示<code>square</code>的中心点。如代码所示,它正确返回了中心点<code>(5, 5)</code></p>
<p><code>center</code>属性之后被设置了一个新的值<code>(15, 15)</code>,表示向右上方移动正方形到如图所示橙色正方形的位置。设置属性<code>center</code>的值会调用setter来修改属性<code>origin</code><code>x</code><code>y</code>的值,从而实现移动正方形到新的位置。</p>
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/computedProperties_2x.png" alt="Computed Properties sample" width="388" height="387" /></p>
<p><a name="shorthand_setter_declaration"></a></p>
<h3 id="-setter-">便捷 setter 声明</h3>
<p>如果计算属性的 setter 没有定义表示新值的参数名,则可以使用默认名称<code>newValue</code>。下面是使用了便捷 setter 声明的<code>Rect</code>结构体代码:</p>
<pre><code>struct AlternativeRect {
@ -711,7 +725,8 @@ println(&quot;square.origin is now at (\(square.origin.x), \(square.origin.y))&q
}
}
}
</code></pre><h3 id="-">只读计算属性</h3>
</code></pre><p><a name="readonly_computed_properties"></a></p>
<h3 id="-">只读计算属性</h3>
<p>只有 getter 没有 setter 的计算属性就是<em>只读计算属性</em>。只读计算属性总是返回一个值,可以通过点运算符访问,但不能设置新的值。</p>
<blockquote>
<p>注意</p>
@ -729,9 +744,10 @@ println(&quot;the volume of fourByFiveByTwo is \(fourByFiveByTwo.volume)&quot;)
// prints &quot;the volume of fourByFiveByTwo is 40.0&quot;
</code></pre><p>这个例子定义了一个名为<code>Cuboid</code>的结构体,表示三维空间的立方体,包含<code>width</code><code>height</code><code>depth</code>属性,还有一个名为<code>volume</code>的只读计算属性用来返回立方体的体积,设置<code>volume</code>的值毫无意义,因为无法确定<code>width</code><code>height</code><code>depth</code>的值。</p>
<p>尽管如此,<code>Cuboid</code>提供一个只读计算属性来让外部用户直接获取体积是很有用的。</p>
<p><a name="property_observers"></a></p>
<h2 id="-">属性监视器</h2>
<p><em>属性监视器</em>监控和响应属性值的变化,每次属性被设置值的时候都会调用属性监视器,甚至新的值和现在的值相同的时候也不例外。</p>
<p>可以为除了延迟存储属性之外的其他存储属性添加属性监视器,也可以通过重载属性的方式为继承的属性(包括存储属性和计算属性)添加属性监视器。属性重载详见<a href="chapter/13_Inheritance.md#">重载</a>一节。</p>
<p>可以为除了延迟存储属性之外的其他存储属性添加属性监视器,也可以通过重载属性的方式为继承的属性(包括存储属性和计算属性)添加属性监视器。属性重载详见<a href="chapter/13_Inheritance.html#overriding">重载</a>一节。</p>
<blockquote>
<p>注意</p>
<p>不需要为无法重载的计算属性添加属性监视器,因为可以通过 setter 直接监控和响应值的变化。</p>
@ -778,6 +794,7 @@ stepCounter.totalSteps = 896
<p>注意</p>
<p>如果在<code>didSet</code>监视器里为属性赋值,这个值会替换监视器之前设置的值。</p>
</blockquote>
<p><a name="global_and_local_variables"></a></p>
<h2 id="-">全局变量和局部变量</h2>
<p>计算属性和属性监视器所描述的模式也可以用于全局变量和局部变量,全局变量是在函数、方法、闭包或任何类型之外定义的变量,局部变量是在函数、方法或闭包内部定义的变量。</p>
<p>前面章节提到的全局或局部变量都属于存储型变量,跟存储属性类似,它提供特定类型的存储空间,并允许读取和写入。</p>
@ -787,6 +804,7 @@ stepCounter.totalSteps = 896
<p>全局的常量或变量都是延迟计算的,跟<a href="#lazy_stored_properties">延迟存储属性</a>相似,不同的地方在于,全局的常量或变量不需要标记<code>@lazy</code>特性。</p>
<p>局部范围的常量或变量不会延迟计算。</p>
</blockquote>
<p><a name="type_properties"></a></p>
<h2 id="-">类属性</h2>
<p>实例的属性属于一个特定类型实例,每次类型实例化后都拥有自己的一套属性值,实例之间的属性相互独立。</p>
<p>也可以为类型本身定义属性,不管类型有多少个实例,这些属性都只有唯一一份。这种属性就是<em>类属性</em></p>
@ -797,6 +815,7 @@ stepCounter.totalSteps = 896
<p>注意</p>
<p>跟实例的存储属性不同,必须给存储型类属性指定默认值,因为类型本身无法在初始化过程中使用构造器给类属性赋值。</p>
</blockquote>
<p><a name="type_property_syntax"></a></p>
<h3 id="-">类属性语法</h3>
<p>在 C 或 Objective-C 中,静态常量和静态变量的定义是通过特定类型加上<code>global</code>关键字。在 Swift 编程语言中,类属性是作为类型定义的一部分写在类型最外层的花括号内,因此它的作用范围也就在类型支持的范围内。</p>
<p>使用关键字<code>static</code>来定义值类型的类属性,关键字<code>class</code>来为类class定义类属性。下面的例子演示了存储型和计算型类属性的语法</p>
@ -821,6 +840,7 @@ class SomeClass {
<p>注意</p>
<p>例子中的计算型类属性是只读的,但也可以定义可读可写的计算型类属性,跟实例计算属性的语法类似。</p>
</blockquote>
<p><a name="querying_and_setting_type_properties"></a></p>
<h3 id="-">获取和设置类属性的值</h3>
<p>跟实例的属性一样,类属性的访问也是通过点运算符来进行,但是,类属性是通过类型本身来获取和设置,而不是通过实例。比如:</p>
<pre><code>println(SomeClass.computedTypeProperty)

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.11" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="2.11" data-basepath=".." data-revision="1402397411238">
<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_111">
<section class="normal" id="section-gitbook_34">
<h1 id="-methods-">方法(Methods)</h1>
<p><strong>方法</strong>是与某些特定类型相关联的函数。类、结构体、枚举都可以定义实例方法实例方法为给定类型的实例封装了具体的任务与功能。类、结构体、枚举也可以定义类型方法类型方法与类型自身相关联。类型方法与Objective-C中的类方法(class methods)相似。</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.12" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="2.12" data-basepath=".." data-revision="1402397411238">
<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_112">
<section class="normal" id="section-gitbook_35">
<h1 id="-subscripts-">下标 (Subscripts)</h1>
<p>下标可以定义在类(Class)、结构体(structures)和枚举(enumerations)这些目标中,可以认为是访问对象、集合或序列的快捷方式。举例来说,用下标访问一个数组(Array)实例中的元素可以这样写 <code>someArray[index]</code> ,访问字典(Dictionary)实例中的元素可以这样写 <code>someDictionary[key]</code>,而不需要再调用实例的某个方法来获得元素的值。</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.13" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="2.13" data-basepath=".." data-revision="1402397411238">
<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_85">
<section class="normal" id="section-gitbook_7">
<h1 id="-">继承</h1>
<p>一个类可以继承另一个类的方法属性和其它特性。当一个类继承其它类继承类叫子类被继承类叫超类或父类。在Swift中继承是区分「类」与其它类型的一个基本特征。</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.14" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="2.14" data-basepath=".." data-revision="1402397411238">
<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="2.15" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="2.15" data-basepath=".." data-revision="1402397411238">
<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_115">
<section class="normal" id="section-gitbook_38">
<h1 id="-">析构过程</h1>
<p>在一个类的实例被释放之前析构函数被立即调用。用关键字deinit来标示析构函数类似于初始化函数用init来标示。析构函数只适用于类类型。</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.16" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="2.16" data-basepath=".." data-revision="1402397411238">
<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_116">
<section class="normal" id="section-gitbook_39">
<h1 id="-">自动引用计数</h1>
<p>本页包含内容:</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.17" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="2.17" data-basepath=".." data-revision="1402397411238">
<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_117">
<section class="normal" id="section-gitbook_40">
<h1 id="optional-chaining">Optional Chaining</h1>
<p>可选链Optional Chaining是一种可以请求和调用属性、方法及子脚本的过程它的自判断性体现于请求或调用的目标当前可能为空<code>nil</code>)。如果自判断的目标有值,那么调用就会成功;相反,如果选择的目标为空(<code>nil</code>),则这种调用将返回空(<code>nil</code>)。多次请求或调用可以被链接在一起形成一个链,如果任何一个节点为空(<code>nil</code>)将导致整个链失效。</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.18" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="2.18" data-basepath=".." data-revision="1402397411238">
<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_118">
<section class="normal" id="section-gitbook_42">
<h1 id="-type-casting-">类型检查Type Casting</h1>
<p>ps为了方便各位检验所以保留了英文可删。

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.19" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="2.19" data-basepath=".." data-revision="1402397411238">
<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_119">
<section class="normal" id="section-gitbook_41">
<h1 id="-">类型嵌套</h1>
<p>本页包含内容:</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.20" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="2.20" data-basepath=".." data-revision="1402397411238">
<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_120">
<section class="normal" id="section-gitbook_43">
<h1 id="-extensions-">扩展Extensions</h1>
<hr>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.21" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="2.21" data-basepath=".." data-revision="1402397411238">
<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_122">
<section class="normal" id="section-gitbook_45">
<h1 id="-">协议</h1>
<p><code>Protocol(协议)</code>用于<strong>统一</strong>方法和属性的名称,而不实现任何功能,(<em>译者注: 协议在其他语言中也称作<code>接口(Interface)</code></em>).<code>协议</code>能够被<code></code>,<code>枚举</code>,<code>结构体</code>实现,满足协议要求的<code></code>,<code>枚举</code>,<code>结构体</code>被称为协议的<code>遵循者</code>.</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.22" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="2.22" data-basepath=".." data-revision="1402397411238">
<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_123">
<section class="normal" id="section-gitbook_46">
<h1 id="-">泛型</h1>
<hr>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.23" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="2.23" data-basepath=".." data-revision="1402397411238">
<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_126">
<section class="normal" id="section-gitbook_49">
<p>In addition to the operators described in <a href="Basic Operators">Basic Operators</a>, Swift provides several advanced operators that perform more complex value manipulation. These include all of the bitwise and bit shifting operators you will be familiar with from C and Objective-C.</p>
<p>除于<a href="Basic Operators">基本操作符</a>中所讲的运算符, Swift还有许多复杂的高级运算符, 包括了C语和Objective-C中的位运算符和位移运算.</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="2" data-basepath=".." data-revision="1402397411238">
<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_127">
<section class="normal" id="section-gitbook_50">
<h1 id="swift-">Swift 教程</h1>
<p>本章介绍了 Swift 的各种特性及其使用方法,是全书的核心部分。</p>

View File

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

View File

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

View File

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

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3.4" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="3.4" data-basepath=".." data-revision="1402397411238">
<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_132">
<section class="normal" id="section-gitbook_55">
<h1 id="-expressions-">表达式(Expressions)</h1>
<hr>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3.6" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="3.6" data-basepath=".." data-revision="1402397411238">
<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="1402390224923">
<div class="book" data-level="3.7" data-basepath=".." data-revision="1402397411238">
<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_128">
<section class="normal" id="section-gitbook_51">
<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="1402390224923">
<div class="book" data-level="3.8" data-basepath=".." data-revision="1402397411238">
<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_136">
<section class="normal" id="section-gitbook_59">
<h1 id="-patterns-">模式Patterns</h1>
<p>模式pattern代表了单个值或者复合值的结构。例如元组<code>(1, 2)</code>的结构是逗号分隔的,包含两个元素的列表。因为模式代表一种值的结构,而不是特定的某个值,你可以把模式和各种同类型的值匹配起来。比如,<code>(x, y)</code>可以匹配元组<code>(1, 2)</code>,以及任何含两个元素的元组。除了将模式与一个值匹配外,你可以从合成值中提取出部分或全部,然后分别把各个部分和一个常量或变量绑定起来。</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3.9" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="3.9" data-basepath=".." data-revision="1402397411238">
<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_137">
<section class="normal" id="section-gitbook_60">
<h1 id="-">泛型参数</h1>
<hr>

View File

@ -44,7 +44,7 @@
<div class="book" data-level="3.10" data-basepath=".." data-revision="1402390224923">
<div class="book" data-level="3.10" data-basepath=".." data-revision="1402397411238">
<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_139">
<section class="normal" id="section-gitbook_63">
<h1 id="-">语法总结</h1>
<p>本页包含内容:</p>

View File

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

View File

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

@ -44,7 +44,7 @@
<div class="book" data-level="0" data-basepath="." data-revision="1402390224923">
<div class="book" data-level="0" data-basepath="." data-revision="1402397411238">
<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_81">
<section class="normal" id="section-gitbook_3">
<h1 id="swift-">Swift 编程语言</h1>
<p>Swift 是苹果在 WWDC 2014 上发布的一款全新的编程语言,本书译自苹果官方的 Swift 教程《The Swift Programming Language》。</p>

View File

@ -1,5 +1,5 @@
CACHE MANIFEST
# Revision 1402390224924
# Revision 1402397411239
CACHE:
index.html
@ -23,8 +23,8 @@ chapter2/14_Initialization.html
chapter2/15_Deinitialization.html
chapter2/16_Automatic_Reference_Counting.html
chapter2/17_Optional_Chaining.html
chapter2/18_Type_Casting.html
chapter2/19_Nested_Types.html
chapter2/18_Type_Casting.html
chapter2/20_Extensions.html
chapter2/21_Protocols.html
chapter2/22_Generics.html
@ -38,8 +38,8 @@ chapter3/05_Declarations.html
chapter3/02_Lexical_Structure.html
chapter3/07_Patterns.html
chapter3/08_Generic_Parameters_and_Arguments.html
chapter3/09_Summary_of_the_Grammar.html
chapter3/10_Statements.html
chapter3/09_Summary_of_the_Grammar.html
chapter3/chapter3.html
gitbook/app.js
gitbook/fonts/anonymouspro/400.woff

File diff suppressed because one or more lines are too long