add list, modify a to -3
This commit is contained in:
14
README.md
14
README.md
@ -15,16 +15,16 @@
|
||||
> 说明:翻译之前请先到PR列表中查看别人认领的内容,尽量不要重复,谢谢!
|
||||
|
||||
* 欢迎使用 Swift
|
||||
* 关于 Swift(完成)
|
||||
* Swift 初见(完成)
|
||||
* 关于 Swift(完成 By numbbbbb)
|
||||
* Swift 初见(完成 By numbbbbb)
|
||||
* Swift 教程
|
||||
* 基础部分(翻译中)
|
||||
* 基本操作符(@xielingwang 认领)
|
||||
* 字符串和字符(完成)
|
||||
* 字符串和字符(完成 By @wh1100717)
|
||||
* 集合类型(@WilliamZang 认领)
|
||||
* 控制流(@vclwei 认领)
|
||||
* 函数(@honghaoz 认领)
|
||||
* 闭包(@wh1100717 认领)
|
||||
* 闭包(完成 By @wh1100717)
|
||||
* 枚举
|
||||
* 类和结构体(@JaySurplus 认领)
|
||||
* 属性(@shinyzhu 认领)
|
||||
@ -36,13 +36,13 @@
|
||||
* 自动引用计数(@TimothyYe 认领)
|
||||
* 可选链(认领)
|
||||
* 类型检查
|
||||
* 嵌套类型(完成)
|
||||
* 嵌套类型(完成 By @Lin-H)
|
||||
* 扩展
|
||||
* 接口
|
||||
* 泛型
|
||||
* 泛型(完成 By @takalard)
|
||||
* 高级操作符
|
||||
* 语言参考
|
||||
* 关于语言参考(完成)
|
||||
* 关于语言参考(完成 By @ChildhoodAndy)
|
||||
* 词法结构
|
||||
* 类型
|
||||
* 表达式
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="1.1" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="1.1" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -576,7 +575,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_4">
|
||||
<section class="normal" id="section-gitbook_8">
|
||||
|
||||
<h1 id="-swift">关于 Swift</h1>
|
||||
<p>Swift 是一种新的编程语言,用于编写 iOS 和 OS X 应用程序。Swift 结合了 C 和 Objective-C 的优点并且不受C的兼容性的限制。Swift 使用安全的编程模式并添加了很多新特性,这将使编程更简单,扩展性更强,也更有趣。除此之外,Swift 还支持人见人爱的 Cocoa 和 Cocoa Touch 框架。拥有了这些特性,Swift将重新定义软件开发。</p>
|
||||
@ -604,8 +603,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="1.2" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="1.2" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -576,9 +575,19 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_5">
|
||||
<section class="normal" id="section-gitbook_9">
|
||||
|
||||
<h1 id="swift-">Swift 初见</h1>
|
||||
<p>本页内容包括:</p>
|
||||
<ul>
|
||||
<li>简单值</li>
|
||||
<li>控制流</li>
|
||||
<li>函数和闭包</li>
|
||||
<li>对象和类</li>
|
||||
<li>枚举和结构体</li>
|
||||
<li>接口和扩展</li>
|
||||
<li>泛型</li>
|
||||
</ul>
|
||||
<p>通常来说,编程语言教程中的第一个程序应该在屏幕上打印“Hello, world”。在 Swift 中,可以用一行代码实现:</p>
|
||||
<pre><code>println("hello, world")
|
||||
</code></pre><p>如果你写过 C 或者 Objective-C 代码,那你应该很熟悉这种形式——在 Swift 中,这行代码就是一个完整的程序。你不需要为了输入输出或者字符串处理导入一个单独的库。全局作用域中的代码会被自动当做程序的入口点,所以你也不需要<code>main</code>函数。你同样不需要在每个语句结尾写上分号。</p>
|
||||
@ -783,7 +792,7 @@ hasAnyMatches(numbers, lessThanTen)
|
||||
<p>练习:重写闭包,对所有奇数返回0.</p>
|
||||
</blockquote>
|
||||
<p>有很多种创建闭包的方法。如果一个闭包的类型已知,比如作为一个回调函数,你可以忽略参数的类型和返回值。单个语句闭包会把它语句的值当做结果返回。</p>
|
||||
<p>你可以获取参数的数量——这个方法在非常短的闭包中很有用。一个被作为最后一个参数传入函数的时候可以直接出现在括号后面。</p>
|
||||
<p>你可以通过参数位置而不是参数名字来引用参数——这个方法在非常短的闭包中非常有用。当一个闭包作为最后一个参数传给一个函数的时候,它可以直接跟在括号后面。</p>
|
||||
<pre><code>sort([1, 5, 3, 12, 2]) { $0 > $1 }
|
||||
</code></pre><h2 id="-">对象和类</h2>
|
||||
<p>使用<code>class</code>和类名来创建一个类。类中属性的声明和常量、变量声明一样,唯一的区别就是它们的上下文是类。同样,方法和函数声明也一样。</p>
|
||||
@ -1100,8 +1109,8 @@ anyCommonElements([1, 2, 3], [3])
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="1" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="1" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -576,7 +575,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_6">
|
||||
<section class="normal" id="section-gitbook_10">
|
||||
|
||||
<h1 id="-swift">欢迎使用 Swift</h1>
|
||||
<p>在本章中您将了解 Swift 的特性和开发历史,并对 Swift 有一个初步的了解。</p>
|
||||
@ -599,8 +598,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.1" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="2.1" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -576,7 +575,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_8">
|
||||
<section class="normal" id="section-gitbook_12">
|
||||
|
||||
<h1 id="-">基础部分</h1>
|
||||
<p>Swift 是 iOS 和 OS X 应用开发的一门新语言。然而,如果你有 C 或者 Objective-C 开发经验的话,你会发现 Swift 的很多内容都是你熟悉的。</p>
|
||||
@ -661,7 +660,119 @@ this is the end of the first multiline comment */
|
||||
<p>与其他大部分编程语言不同,Swift 并不强制要求你在每条语句的结尾处使用分号(;),当然,你也可以按照你自己的习惯添加分号。有一种情况下必须要用分号,即你打算在同一行内写多条独立的语句:</p>
|
||||
<pre><code>let cat = "🐱"; println(cat)
|
||||
// prints "🐱"
|
||||
</code></pre>
|
||||
</code></pre><h2 id="-">整数</h2>
|
||||
<p>整数就是没有小数部分的数字,比如<code>42</code>和<code>-23</code>。整数可以是有符号(正、负、零)或者无符号(正、零)。</p>
|
||||
<p>Swift 提供了8、16、32和64位的有符号和无符号整数类型。这些整数类型和 C 语言的命名方式很像,比如8位无符号整数类型是<code>UInt8</code>,32位有符号整数类型是<code>Int32</code>。就像 Swift 的其他类型一样,整数类型采用大写命名法。</p>
|
||||
<h3 id="-">整数范围</h3>
|
||||
<p>你可以访问不同整数类型的<code>min</code>和<code>max</code>属性来获取对应类型的最大值和最小值:</p>
|
||||
<pre><code>let minValue = UInt8.min // minValue 为 0,是 UInt8 类型的最小值
|
||||
let maxValue = UInt8.max // maxValue 为 255,是 UInt8 类型的最大值
|
||||
</code></pre><h3 id="int">Int</h3>
|
||||
<p>一般来说,你不需要专门指定整数的长度。Swift 提供了一个特殊的整数类型<code>Int</code>,长度与当前平台的原生字长相同:</p>
|
||||
<ul>
|
||||
<li>在32位平台上,<code>Int</code>和<code>Int32</code>长度相同。</li>
|
||||
<li>在64位平台上,<code>Int</code>和<code>Int64</code>长度相同。</li>
|
||||
</ul>
|
||||
<p>除非你需要特定长度的整数,一般来说使用<code>Int</code>就够了。这可以提高代码一致性和可复用性。即使是在32位平台上,<code>Int</code>可以存储的整数范围也可以达到<code>-2147483648</code>~<code>2147483647</code>,大多数时候这已经足够大了。</p>
|
||||
<h3 id="uint">UInt</h3>
|
||||
<p>Swift 也提供了一个特殊的无符号类型<code>UInt</code>,长度与当前平台的原生字长相同:</p>
|
||||
<ul>
|
||||
<li>在32位平台上,<code>UInt</code>和<code>UInt32</code>长度相同。</li>
|
||||
<li>在64位平台上,<code>UInt</code>和<code>UInt64</code>长度相同。</li>
|
||||
</ul>
|
||||
<blockquote>
|
||||
<p>注意:尽量不要使用<code>UInt</code>,除非你真的需要存储一个和当前平台原生字长相同的无符号整数。除了这种情况,最好使用<code>Int</code>,即使你要存储的值已知是非负的。统一使用<code>Int</code>可以提高代码的可复用性,避免不同类型数字之间的转换,并且匹配数字的类型推测,详情参见<a href="## 类型安全和类型推测">类型安全和类型推测</a>。</p>
|
||||
</blockquote>
|
||||
<h2 id="-">浮点数</h2>
|
||||
<p>浮点数是有小数部分的数字,比如<code>3.14159</code>,<code>0.1</code>和<code>-273.15</code>。</p>
|
||||
<p>浮点类型比整数类型表示的范围更大,可以存储比<code>Int</code>类型更大或者更小的数字。Swift 提供了两种有符号浮点数类型:</p>
|
||||
<ul>
|
||||
<li><code>Double</code>表示64位浮点数。当你需要存储很大或者很高精度的浮点数时请使用此类型。</li>
|
||||
<li><code>Float</code>表示32位浮点数。精度要求不高的话可以使用此类型。</li>
|
||||
</ul>
|
||||
<blockquote>
|
||||
<p>注意:<code>Double</code>精确度很高,至少有15位数字,而<code>Float</code>最少只有6位数字。选择哪个类型取决于你的代码需要处理的数字大小。</p>
|
||||
</blockquote>
|
||||
<h2 id="-">类型安全和类型推测</h2>
|
||||
<p>Swift 是一个类型安全的语言。类型安全的语言可以让你清楚地知道代码要处理的值的类型。如果你的代码需要一个<code>String</code>,你绝对不可能不小心传进去一个<code>Int</code>。</p>
|
||||
<p>Swift 是类型安全的,会在编译你的代码时进行类型检查,如果遇到不匹配的类型会报错。这可以让你在开发的时候尽早发现并修复错误。</p>
|
||||
<p>当你要处理不同类型的值时,类型检查可以帮你避免错误。然而,这并不是说你每次声明常量和变量的时候都需要显式指定类型。如果你没有显式指定类型,Swift 会使用类型推测来选择合适的类型。有了类型推测,编译器可以在编译代码的时候自动推测出表达式的类型。原理很简单,判断你赋的值即可。</p>
|
||||
<p>因为有类型推测,和 C 或者 Objc 比起来 Swift 很少需要声明类型。常量和变量虽然需要明确类型,但是大部分工作并不需要你自己来完成。</p>
|
||||
<p>当你声明常量或者变量并赋初值的时候类型推测非常有用。当你在声明常量或者变量的时候赋给它们一个原始值即可触发类型推测。(原始值就是会直接出现在你代码中的值,比如<code>42</code>和<code>3.14159</code>。)</p>
|
||||
<p>举个例子,如果你给一个新常量赋值<code>42</code>并且没有标明类型,Swift 可以推测出常量类型是<code>Int</code>,因为你给它赋的初值看起来很像一个整数:</p>
|
||||
<pre><code>let meaningOfLife = 42
|
||||
// meaningOfLife 会被推测为 Int 类型
|
||||
</code></pre><p>同理,如果你没有给浮点原始值标明类型,Swift 会推测你想要的是<code>Double</code>:</p>
|
||||
<pre><code>let pi = 3.14159
|
||||
// pi 会被推测为 Double 类型
|
||||
</code></pre><p>当推测浮点数的类型时,Swift 总是会选择<code>Double</code>而不是<code>Float</code>。</p>
|
||||
<p>如果表达式中同时出现了整数和浮点数,会被推测为<code>Double</code>类型:</p>
|
||||
<pre><code>let anotherPi = 3 + 0.14159
|
||||
// anotherPi 会被推测为 Double 类型
|
||||
</code></pre><p>原始值<code>3</code>没有显式声明类型,而表达式中出现了一个浮点原始值,所以表达式会被推测为<code>Double</code>类型。</p>
|
||||
<h2 id="-">数值类原始值</h2>
|
||||
<p>整数原始值可以被写作:</p>
|
||||
<ul>
|
||||
<li>一个十进制数,没有前缀</li>
|
||||
<li>一个二进制数,前缀是<code>0b</code></li>
|
||||
<li>一个八进制数,前缀是<code>0o</code></li>
|
||||
<li>一个十六进制数,前缀是<code>0x</code></li>
|
||||
</ul>
|
||||
<p>下面的所有整数原始值的十进制值都是<code>17</code>:</p>
|
||||
<pre><code>let decimalInteger = 17
|
||||
let binaryInteger = 0b10001 // 二进制的17
|
||||
let octalInteger = 0o21 // 八进制的17
|
||||
let hexadecimalInteger = 0x11 // 十六机制的17
|
||||
</code></pre><p>浮点原始值可以是十进制(没有前缀)或者是十六进制(前缀是<code>0x</code>)。小数点两边必须有至少一个十进制数字(或者是十六进制的数字)。浮点原始值还有一个可选的指数,在十进制浮点数中通过大写或者小写的<code>e</code>来指定,在十六进制浮点数中通过大写或者小写的<code>p</code>来指定。</p>
|
||||
<p>如果一个十进制数的指数为<code>exp</code>,那这个数相当于基数和10^exp的乘积:</p>
|
||||
<ul>
|
||||
<li>1.25e2 表示 1.25 × 10^2,等于 125.0。</li>
|
||||
<li>1.25e-2 表示 1.25 × 10^-2,等于 0.0125。</li>
|
||||
</ul>
|
||||
<p>如果一个十六进制数的指数为<code>exp</code>,那这个数相当于基数和2^exp的乘积:</p>
|
||||
<ul>
|
||||
<li>0xFp2 表示 15 × 2^2,等于 60.0。</li>
|
||||
<li>0xFp-2 表示 15 × 2^-2,等于 3.75。</li>
|
||||
</ul>
|
||||
<p>下面的这些浮点原始值都等于十进制的<code>12.1875</code>:</p>
|
||||
<pre><code>let decimalDouble = 12.1875
|
||||
let exponentDouble = 1.21875e1
|
||||
let hexadecimalDouble = 0xC.3p0
|
||||
</code></pre><p>数值类原始值可以包括额外的格式来增强可读性。整数和浮点数都可以添加额外的零并且包含下划线,并不会影响原始值:</p>
|
||||
<pre><code>let paddedDouble = 000123.456
|
||||
let oneMillion = 1_000_000
|
||||
let justOverOneMillion = 1_000_000.000_000_1
|
||||
</code></pre><h2 id="-">数值类型转换</h2>
|
||||
<p>通常来讲,即使代码中的整数常量和变量已知非负,也请使用<code>Int</code>类型。总是使用默认的整数类型可以保证你的整数常量和变量可以直接被复用并且可以匹配整数类原始值的类型推测。
|
||||
只有在必要的时候才使用其他整数类型,比如要处理外部的长度明确的数据或者为了优化性能、内存占用等等。使用显式指定长度的类型可以及时发现值溢出并且可以暗示正在处理特殊数据。</p>
|
||||
<h3 id="-">整数转换</h3>
|
||||
<p>不同整数类型的变量和常量可以存储不同大小的数字。<code>Int8</code>类型的常量或者变量可以存储的数字范围是<code>-128</code>~<code>127</code>,<code>UInt8</code>类型的常量或者变量能存储的数字范围是<code>0</code>~<code>255</code>。如果数字超出了常量或者变量可存储的范围,编译的时候会报错:</p>
|
||||
<pre><code>let cannotBeNegative: UInt8 = -1
|
||||
// UInt8 类型不能存储负数,所以会报错
|
||||
let tooBig: Int8 = Int8.max + 1
|
||||
// Int8 类型不能存储超过最大值的数,所以会报错
|
||||
</code></pre><p>因为每一个整数类型都可以存储不同范围的值,你必须根据情况来选择不同的转换方法。不同的转换方法可以暴露出隐藏的转换错误并让你的代码更加清晰。</p>
|
||||
<p>要将一种数字类型转换成另一种,你要用当前值来初始化一个新数字,这个数字的类型就是你的目标类型。在下面的例子中,常量<code>twoThousand</code>类型是<code>UInt16</code>,然而常量<code>one</code>类型是<code>Uint8</code>。它们不能直接相加,因为它们类型不同。所以要调用<code>UInt16(one)</code>来创建一个新的<code>UInt16</code>数字并用<code>one</code>的值来初始化,然后使用这个新数字来计算:</p>
|
||||
<pre><code>let twoThousand: UInt16 = 2_000
|
||||
let one: UInt8 = 1
|
||||
let twoThousandAndOne = twoThousand + UInt16(one)
|
||||
</code></pre><p>现在两个数字的类型都是<code>UInt16</code>,可以进行相加。目标常量<code>twoThousandAndOne</code>的类型被推测为<code>UInt16</code>,因为它是两个<code>UInt16</code>值的合。</p>
|
||||
<p><code>SomeType(ofInitialValue)</code>是调用 Swift 构造器并传入一个初始值的默认方法。在语言内部,<code>UInt16</code>有一个构造器,可以接受一个<code>UInt8</code>类型的值,所以这个构造器可以用现有的<code>UInt8</code>来创建一个新的<code>UInt16</code>。注意,你并不能传入任意类型的值,只能传入<code>UInt16</code>内部有对应构造器的值。不过你可以扩展现有的类型来让它可以接收其他类型的值(包括自定义类型),详情参见<code>扩展(链接待添加)</code>.</p>
|
||||
<h3 id="-">整数和浮点数转换</h3>
|
||||
<p>整数和浮点数的转换必须显式指定类型:</p>
|
||||
<pre><code>let three = 3
|
||||
let pointOneFourOneFiveNine = 0.14159
|
||||
let pi = Double(three) + pointOneFourOneFiveNine
|
||||
// pi 等于 3.14159,所以被推测为 Double 类型
|
||||
</code></pre><p>这个例子中,常量<code>three</code>的值被用来创建一个<code>Double</code>类型的值,所以加号两边的数类型相同。如果不进行转换,两者无法相加。</p>
|
||||
<p>浮点数转换为整数也一样,整数类型可以用<code>Double</code>或者<code>Float</code>类型来初始化:</p>
|
||||
<pre><code>let integerPi = Int(pi)
|
||||
// integerPi 等于 3,所以被推测为 Int 类型
|
||||
</code></pre><p>当用这种方式来初始化一个新的整数值时,浮点值会被截断。也就是说<code>4.75</code>会变成<code>4</code>,<code>-3.9</code>会变成<code>3</code>。</p>
|
||||
<blockquote>
|
||||
<p>注意:结合数字类常量和变量不同于结合数字类原始值。原始值<code>3</code>可以直接和原始值<code>0.14159</code>相加,因为数字原始值本身没有明确的类型。它们的类型只在编译器需要求值的时候被推测。</p>
|
||||
</blockquote>
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
@ -680,8 +791,8 @@ this is the end of the first multiline comment */
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.2" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="2.2" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -591,8 +590,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.3" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="2.3" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -576,20 +575,28 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_9">
|
||||
<section class="normal" id="section-gitbook_13">
|
||||
|
||||
<h1 id="-strings-and-characters-">字符串和字符 (Strings and Characters)</h1>
|
||||
<p><strong>String</strong> 是一个有序的字符集合,例如 "hello, world", "albatross"。Swift 字符串通过 <strong>String</strong> 类型来表示,也可以表示为 <strong>Character</strong> 类型值的集合。</p>
|
||||
<p>Swift 的 <strong>String</strong> 和 <strong>Character</strong> 类型提供了一个快速的,兼容 Unicode 的方式来处理代码中的文本信息。创建和操作字符串的语法与 C的操作方式相似,轻量并且易读。字符串连接操作只需要简单地通过 <code>+</code> 号将两个字符串相连即可。与 Swift 中其他值一样,能否更改字符串的值,取决于其被定义为常量还是变量。</p>
|
||||
<p>尽管语法简易,但 <strong>String</strong> 类型是一种快速、现代化的字符串实现。每一个字符串都是由独立编码的 Unicode 字符组成,并提供了用于访问这些字符在不同的Unicode表示的支持。</p>
|
||||
<p><strong>String</strong> 是一个有序的字符集合,例如 "hello, world", "albatross"。
|
||||
Swift 字符串通过 <strong>String</strong> 类型来表示,也可以表示为 <strong>Character</strong> 类型值的集合。</p>
|
||||
<p>Swift 的 <strong>String</strong> 和 <strong>Character</strong> 类型提供了一个快速的,兼容 Unicode 的方式来处理代码中的文本信息。
|
||||
创建和操作字符串的语法与 C的操作方式相似,轻量并且易读。
|
||||
字符串连接操作只需要简单地通过 <code>+</code> 号将两个字符串相连即可。
|
||||
与 Swift 中其他值一样,能否更改字符串的值,取决于其被定义为常量还是变量。</p>
|
||||
<p>尽管语法简易,但 <strong>String</strong> 类型是一种快速、现代化的字符串实现。
|
||||
每一个字符串都是由独立编码的 Unicode 字符组成,并提供了用于访问这些字符在不同的Unicode表示的支持。</p>
|
||||
<p><strong>String</strong> 也可以用于在常量、变量、字面量和表达式中进行字符串插值,这使得创建用于展示、存储和打印的字符串变得轻松自如。</p>
|
||||
<blockquote>
|
||||
<p>注意:</p>
|
||||
<p>Swift 的 <strong>String</strong> 类型与 Foundation NSString 类进行了无缝桥接。如果您利用 Cocoa 或 Cocoa Touch 中的 Foundation 框架进行工作,整个 NSString API 都可以调用您创建的任意 String 类型的值,您额外还可以在任意 API 中使用本章介绍的 <strong>String</strong> 特性。您也可以在任意要求传入NSString 实例作为参数的 API 中使用 <strong>String</strong> 类型的值进行替换。</p>
|
||||
<p>Swift 的 <strong>String</strong> 类型与 Foundation NSString 类进行了无缝桥接。
|
||||
如果您利用 Cocoa 或 Cocoa Touch 中的 Foundation 框架进行工作,整个 NSString API 都可以调用您创建的任意 String 类型的值,您额外还可以在任意 API 中使用本章介绍的 <strong>String</strong> 特性。
|
||||
您也可以在任意要求传入NSString 实例作为参数的 API 中使用 <strong>String</strong> 类型的值进行替换。</p>
|
||||
<p>更多关于在 Foundation 和 Cocoa 中使用 <strong>String</strong> 的信息请查看 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/index.html#//apple_ref/doc/uid/TP40014216" target="_blank">Using Swift with Cocoa and Objective-C</a>。</p>
|
||||
</blockquote>
|
||||
<h3 id="-">字符串字面量</h3>
|
||||
<p>您可以在您的代码中包含一段预定义的字符串值作为字符串字面量。字符串字面量是由双引号包裹着的具有固定顺序的文本字符集。</p>
|
||||
<p>您可以在您的代码中包含一段预定义的字符串值作为字符串字面量。
|
||||
字符串字面量是由双引号包裹着的具有固定顺序的文本字符集。</p>
|
||||
<p>字符串字面量可以用于为常量和变量提供初始值。</p>
|
||||
<pre><code>let someString = "Some string literal value"
|
||||
</code></pre><blockquote>
|
||||
@ -611,7 +618,8 @@ let dollarSign = "\x24" // $, Unicode scalar U+0024
|
||||
let blackHeart = "\u2665" // ♥, Unicode scalar U+2665
|
||||
let sparklingHeart = "\U0001F496" // 💖, Unicode scalar U+1F496
|
||||
</code></pre><h3 id="-">初始化空字符串</h3>
|
||||
<p>为了构造一个很长的字符串,可以创建一个空字符串作为初始值。可以将空的字符串字面量赋值给变量,也可以初始化一个新的 <strong>String</strong> 实例:</p>
|
||||
<p>为了构造一个很长的字符串,可以创建一个空字符串作为初始值。
|
||||
可以将空的字符串字面量赋值给变量,也可以初始化一个新的 <strong>String</strong> 实例:</p>
|
||||
<pre><code>var emptyString = "" // empty string literal
|
||||
var anotherEmptyString = String() // initializer syntax
|
||||
// 这两个字符串都为空,并且两者等价
|
||||
@ -633,15 +641,21 @@ constantString += " and another Highlander"
|
||||
<p>在 Objective-C 和 Cocoa 中,您通过选择两个不同的类( NSString 和 NSMutableString )来指定该字符串是否可以被修改,Swift中的字符串是否可以修改仅通过定义的是变量还是常量来决定,实现了多种类型可变性操作的统一。</p>
|
||||
</blockquote>
|
||||
<h3 id="-">字符串是值类型</h3>
|
||||
<p>Swift 的 <strong>String</strong> 类型是值类型。如果您创建了一个新的字符串,那么当其进行常量、变量赋值操作或在函数/方法中传递时,会进行值拷贝。任何情况下,都会对已有字符串值创建新副本,并对该新副本进行传递或赋值。值类型在 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html#//apple_ref/doc/uid/TP40014097-CH13-XID_104" target="_blank">Structures and Enumerations Are Value Types</a> 中进行了说明。</p>
|
||||
<p>Swift 的 <strong>String</strong> 类型是值类型。
|
||||
如果您创建了一个新的字符串,那么当其进行常量、变量赋值操作或在函数/方法中传递时,会进行值拷贝。
|
||||
任何情况下,都会对已有字符串值创建新副本,并对该新副本进行传递或赋值。
|
||||
值类型在 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html#//apple_ref/doc/uid/TP40014097-CH13-XID_104" target="_blank">Structures and Enumerations Are Value Types</a> 中进行了说明。</p>
|
||||
<blockquote>
|
||||
<p>注意:</p>
|
||||
<p>与 Cocoa 中的 NSString 不同,当您在 Cocoa 中创建了一个 NSString 实例,并将其传递给一个函数/方法,或者赋值给一个变量,您永远都是传递或赋值同一个 NSString 实例的一个引用。除非您特别要求其进行值拷贝,否则字符串不会进行赋值新副本操作。</p>
|
||||
<p>与 Cocoa 中的 NSString 不同,当您在 Cocoa 中创建了一个 NSString 实例,并将其传递给一个函数/方法,或者赋值给一个变量,您永远都是传递或赋值同一个 NSString 实例的一个引用,除非您特别要求其进行值拷贝,否则字符串不会进行赋值新副本操作。</p>
|
||||
</blockquote>
|
||||
<p>Swift 默认字符串拷贝的方式保证了在函数/方法中传递的是字符串的值,其明确了无论该值来自于哪里,都是您独自拥有的。您可以放心您传递的字符串本身不会被更改。</p>
|
||||
<p>Swift 默认字符串拷贝的方式保证了在函数/方法中传递的是字符串的值,其明确了无论该值来自于哪里,都是您独自拥有的。
|
||||
您可以放心您传递的字符串本身不会被更改。</p>
|
||||
<p>在实际编译时,Swift编译器会优化字符串的使用,使实际的复制只发生在绝对必要的情况下,这意味着您始终可以将字符串作为值类型的同时获得极高的性能。</p>
|
||||
<h4 id="-characters-">使用字符(Characters)</h4>
|
||||
<p>Swift 的 <strong>String</strong> 类型表示特定序列的字符值的集合。每一个字符值代表一个 Unicode 字符。您可利用 for-in 循环来遍历字符串中的每一个字符:</p>
|
||||
<p>Swift 的 <strong>String</strong> 类型表示特定序列的字符值的集合。
|
||||
每一个字符值代表一个 Unicode 字符。
|
||||
您可利用 for-in 循环来遍历字符串中的每一个字符:</p>
|
||||
<pre><code>for character in "Dog!🐶" {
|
||||
println(character)
|
||||
}
|
||||
@ -660,8 +674,13 @@ println("unusualMenagerie has \(countElements(unusualMenagerie)) characters
|
||||
// prints "unusualMenagerie has 40 characters"
|
||||
</code></pre><blockquote>
|
||||
<p>注意:</p>
|
||||
<p>不同的 Unicode 字符以及相同 Unicode 字符的不同表示方式可能需要不同数量的内存空间来存储,所以Swift 中的字符在一个字符串中并不一定占用相同的内存空间。因此,字符串的长度不得不通过迭代字符串中每一个字符的长度来进行计算。如果您正在处理一个长字符串,需要注意 <code>countElements</code> 函数必须遍历字符串中的字符以精准计算字符串的长度。</p>
|
||||
<p>另外需要注意的是通过 <code>countElements</code> 返回的字符数量并不总是与包含相同字符的 NSString 的 <code>length</code> 属性相同。NSString 的 <code>length</code> 属性是基于利用 UTF-16 表示的十六位代码单元数字,而不是基于 Unicode 字符。为了解决这个问题,NSString 的 <code>length</code> 属性在被 Swift的 <strong>String</strong> 访问时会成为 <code>utf16count</code>。</p>
|
||||
<p>不同的 Unicode 字符以及相同 Unicode 字符的不同表示方式可能需要不同数量的内存空间来存储。
|
||||
所以Swift 中的字符在一个字符串中并不一定占用相同的内存空间。
|
||||
因此字符串的长度不得不通过迭代字符串中每一个字符的长度来进行计算。
|
||||
如果您正在处理一个长字符串,需要注意 <code>countElements</code> 函数必须遍历字符串中的字符以精准计算字符串的长度。</p>
|
||||
<p>另外需要注意的是通过 <code>countElements</code> 返回的字符数量并不总是与包含相同字符的 NSString 的 <code>length</code> 属性相同。
|
||||
NSString 的 <code>length</code> 属性是基于利用 UTF-16 表示的十六位代码单元数字,而不是基于 Unicode 字符。
|
||||
为了解决这个问题,NSString 的 <code>length</code> 属性在被 Swift的 <strong>String</strong> 访问时会成为 <code>utf16count</code>。</p>
|
||||
</blockquote>
|
||||
<h3 id="-">连接字符串和字符</h3>
|
||||
<p>字符串和字符的值可以通过加法运算符 (+) 相加在一起并创建一个新的字符串值:</p>
|
||||
@ -687,12 +706,16 @@ welcome += character1
|
||||
<p>您不能将一个字符串或者字符添加到一个已经存在的字符变量上,因为字符变量只能包含一个字符。</p>
|
||||
</blockquote>
|
||||
<h3 id="-">字符串插值</h3>
|
||||
<p>字符串插值是一种全新的构建字符串的方式,可以在其中包含常量、变量、字面量和表达式。您插入的字符串字面量的每一项都被包裹在以反斜线为前缀的圆括号中:</p>
|
||||
<p>字符串插值是一种全新的构建字符串的方式,可以在其中包含常量、变量、字面量和表达式。
|
||||
您插入的字符串字面量的每一项都被包裹在以反斜线为前缀的圆括号中:</p>
|
||||
<pre><code>let multiplier = 3
|
||||
let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"
|
||||
// message is "3 times 2.5 is 7.5"
|
||||
</code></pre><p>在上面的例子中,<code>multiplier</code> 作为 <code>\(multiplier)</code> 被插入到一个字符串字面量中。当创建字符串执行插值计算时此占位符会被替换为 <code>multiplier</code> 实际的值。</p>
|
||||
<p><code>multiplier</code> 的值也作为字符串中后面表达式的一部分。该表达式计算 <code>Double(multiplier) * 2.5</code> 的值并将结果 (7.5) 插入到字符串中。在这个例子中,表达式写为 <code>\(Double(multiplier) * 2.5)</code> 并包含在字符串字面量中。</p>
|
||||
</code></pre><p>在上面的例子中,<code>multiplier</code> 作为 <code>\(multiplier)</code> 被插入到一个字符串字面量中。
|
||||
当创建字符串执行插值计算时此占位符会被替换为 <code>multiplier</code> 实际的值。</p>
|
||||
<p><code>multiplier</code> 的值也作为字符串中后面表达式的一部分。
|
||||
该表达式计算 <code>Double(multiplier) * 2.5</code> 的值并将结果 (7.5) 插入到字符串中。
|
||||
在这个例子中,表达式写为 <code>\(Double(multiplier) * 2.5)</code> 并包含在字符串字面量中。</p>
|
||||
<blockquote>
|
||||
<p>注意:</p>
|
||||
<p>您插值字符串中写在括号中的表达式不能包含非转义双引号 (") 和反斜杠 (\),并且不能包含回车或换行符。</p>
|
||||
@ -708,7 +731,9 @@ if quotation == sameQuotation {
|
||||
}
|
||||
// prints "These two strings are considered equal"
|
||||
</code></pre><h5 id="-">前缀/后缀相等</h5>
|
||||
<p>通过调用字符串的 <code>hasPrefix</code>/<code>hasSuffix</code> 方法来检查字符串是否拥有特定前缀/后缀。两个方法均需要以字符串作为参数传入并传出 <strong>Boolean</strong> 值。两个方法均执行基本字符串和前缀/后缀字符串之间逐个字符的比较操作。</p>
|
||||
<p>通过调用字符串的 <code>hasPrefix</code>/<code>hasSuffix</code> 方法来检查字符串是否拥有特定前缀/后缀。
|
||||
两个方法均需要以字符串作为参数传入并传出 <strong>Boolean</strong> 值。
|
||||
两个方法均执行基本字符串和前缀/后缀字符串之间逐个字符的比较操作。</p>
|
||||
<p>下面的例子以一个字符串数组表示莎士比亚话剧 <code>罗密欧与朱丽叶</code> 中前两场的场景位置:</p>
|
||||
<pre><code>let romeoAndJuliet = [
|
||||
"Act 1 Scene 1: Verona, A public place",
|
||||
@ -740,14 +765,17 @@ let shouty = normal.uppercaseString
|
||||
let whispered = normal.lowercaseString
|
||||
// whispered 值为 "could you help me, please?"
|
||||
</code></pre><h3 id="unicode">Unicode</h3>
|
||||
<p>Unicode 是文本编码和表示的国际标准。它使您可以用标准格式表示来自任意语言几乎所有的字符,并能够对文本文件或网页这样的外部资源中的字符进行读写操作。</p>
|
||||
<p>Unicode 是文本编码和表示的国际标准。
|
||||
它使您可以用标准格式表示来自任意语言几乎所有的字符,并能够对文本文件或网页这样的外部资源中的字符进行读写操作。</p>
|
||||
<p>Swift 的字符串和字符类型是完全兼容 Unicode 的,它支持如下所述的一系列不同的 Unicode 编码。</p>
|
||||
<h6 id="unicode-terminology-">Unicode 术语(Terminology)</h6>
|
||||
<p>Unicode 中每一个字符都可以被解释为一个或多个 unicode 标量。字符的 unicode 标量是一个唯一的21位数字(和名称),例如 <code>U+0061</code> 表示小写的拉丁字母A ("a"),<code>U+1F425</code> 表示 ("🐥")</p>
|
||||
<p>Unicode 中每一个字符都可以被解释为一个或多个 unicode 标量。
|
||||
字符的 unicode 标量是一个唯一的21位数字(和名称),例如 <code>U+0061</code> 表示小写的拉丁字母A ("a"),<code>U+1F425</code> 表示小幺鸡表情 ("🐥")</p>
|
||||
<p>当 Unicode 字符串被写进文本文件或其他存储结构当中,这些 unicode 标量将会按照 Unicode 定义的集中格式之一进行编码。其包括 <code>UTF-8</code> (以8位代码单元进行编码) 和 <code>UTF-16</code> (以16位代码单元进行编码)。</p>
|
||||
<h5 id="-unicode-">字符串的 Unicode 表示</h5>
|
||||
<p>Swift 提供了几种不同的方式来访问字符串的 Unicode 表示。</p>
|
||||
<p>您可以利用 <code>for-in</code> 来对字符串进行遍历,从而以 Unicode 字符的方式访问每一个字符值。该过程在 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html#//apple_ref/doc/uid/TP40014097-CH7-XID_376" target="_blank">Working with Characters</a> 中进行了描述。</p>
|
||||
<p>您可以利用 <code>for-in</code> 来对字符串进行遍历,从而以 Unicode 字符的方式访问每一个字符值。
|
||||
该过程在 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html#//apple_ref/doc/uid/TP40014097-CH7-XID_376" target="_blank">Working with Characters</a> 中进行了描述。</p>
|
||||
<p>另外,能够以其他三种 Unicode 兼容的方式访问字符串的值:</p>
|
||||
<ul>
|
||||
<li>UTF-8 代码单元集合 (利用字符串的 <code>utf8</code> 属性进行访问)</li>
|
||||
@ -757,31 +785,39 @@ let whispered = normal.lowercaseString
|
||||
<p>下面由 <code>D</code> <code>o</code> <code>g</code> <code>!</code> 和 <code>🐶</code> (狗脸表情,Unicode 标量为 <code>U+1F436</code>)组成的字符串中的每一个字符代表着一种不同的表示:</p>
|
||||
<pre><code>let dogString = "Dog!🐶"
|
||||
</code></pre><h5 id="utf-8">UTF-8</h5>
|
||||
<p>您可以通过遍历字符串的 <code>utf8</code> 属性来访问它的 <code>UTF-8</code> 表示。其为 <strong>UTF8View</strong> 类型的属性,<strong>UTF8View</strong> 是无符号8位 (<code>UInt8</code>) 值的集合,每一个 <code>UIn8</code> 都是一个字符的 UTF-8 表示:</p>
|
||||
<p>您可以通过遍历字符串的 <code>utf8</code> 属性来访问它的 <code>UTF-8</code> 表示。
|
||||
其为 <strong>UTF8View</strong> 类型的属性,<strong>UTF8View</strong> 是无符号8位 (<code>UInt8</code>) 值的集合,每一个 <code>UIn8</code> 都是一个字符的 UTF-8 表示:</p>
|
||||
<pre><code>for codeUnit in dogString.utf8 {
|
||||
print("\(codeUnit) ")
|
||||
}
|
||||
print("\n")
|
||||
// 68 111 103 33 240 159 144 182
|
||||
</code></pre><p>上面的例子中,前四个10进制代码单元值 (68, 111, 103, 33) 代表了字符 <code>D</code> <code>o</code> <code>g</code> 和 <code>!</code> ,他们的 UTF-8 表示与 ASCII 表示相同。后四个代码单元值 (240, 159, 144, 182) 是 <code>狗脸表情</code> 的4位 UTF-8 表示。</p>
|
||||
</code></pre><p>上面的例子中,前四个10进制代码单元值 (68, 111, 103, 33) 代表了字符 <code>D</code> <code>o</code> <code>g</code> 和 <code>!</code> ,他们的 UTF-8 表示与 ASCII 表示相同。
|
||||
后四个代码单元值 (240, 159, 144, 182) 是 <code>狗脸表情</code> 的4位 UTF-8 表示。</p>
|
||||
<h5 id="utf-16">UTF-16</h5>
|
||||
<p>您可以通过遍历字符串的 <code>utf16</code> 属性来访问它的 <code>UTF-16</code> 表示。其为 <strong>UTF16View</strong> 类型的属性,<strong>UTF16View</strong> 是无符号16位 (<code>UInt16</code>) 值的集合,每一个 <code>UInt16</code> 都是一个字符的 UTF-16 表示:</p>
|
||||
<p>您可以通过遍历字符串的 <code>utf16</code> 属性来访问它的 <code>UTF-16</code> 表示。
|
||||
其为 <strong>UTF16View</strong> 类型的属性,<strong>UTF16View</strong> 是无符号16位 (<code>UInt16</code>) 值的集合,每一个 <code>UInt16</code> 都是一个字符的 UTF-16 表示:</p>
|
||||
<pre><code>for codeUnit in dogString.utf16 {
|
||||
print("\(codeUnit) ")
|
||||
}
|
||||
print("\n")
|
||||
// 68 111 103 33 55357 56374
|
||||
</code></pre><p>同样,前四个代码单元值 (68, 111, 103, 33) 代表了字符 <code>D</code> <code>o</code> <code>g</code> 和 <code>!</code> ,他们的 UTF-16 代码单元和 UTF-8 完全相同。</p>
|
||||
<p>第五和第六个代码单元值 (55357 and 56374) 是 <code>狗脸表情</code> 字符的UTF-16 表示。第一个值为 <code>U+D83D</code> (十进制值为 55357),第二个值为 <code>U+DC36</code> (十进制值为 56374)。</p>
|
||||
<p>第五和第六个代码单元值 (55357 and 56374) 是 <code>狗脸表情</code> 字符的UTF-16 表示。
|
||||
第一个值为 <code>U+D83D</code> (十进制值为 55357),第二个值为 <code>U+DC36</code> (十进制值为 56374)。</p>
|
||||
<h5 id="unicode-scalars-">Unicode 标量 (Scalars)</h5>
|
||||
<p>您可以通过遍历字符串的 <code>unicodeScalars</code> 属性来访问它的 Unicode 标量表示。其为 <strong>UnicodeScalarView</strong> 类型的属性, <strong>UnicodeScalarView</strong> 是 <code>UnicodeScalar</code> 的集合。<code>UnicodeScalar</code> 是21位的 Unicode 代码点。</p>
|
||||
<p>您可以通过遍历字符串的 <code>unicodeScalars</code> 属性来访问它的 Unicode 标量表示。
|
||||
其为 <strong>UnicodeScalarView</strong> 类型的属性, <strong>UnicodeScalarView</strong> 是 <code>UnicodeScalar</code> 的集合。
|
||||
<code>UnicodeScalar</code> 是21位的 Unicode 代码点。</p>
|
||||
<p>每一个 <code>UnicodeScalar</code> 拥有一个值属性,可以返回对应的21位数值,用 <code>UInt32</code> 来表示。</p>
|
||||
<pre><code>for scalar in dogString.unicodeScalars {
|
||||
print("\(scalar.value) ")
|
||||
}
|
||||
print("\n")
|
||||
// 68 111 103 33 128054
|
||||
</code></pre><p>同样,前四个代码单元值 (68, 111, 103, 33) 代表了字符 <code>D</code> <code>o</code> <code>g</code> 和 <code>!</code> 。第五位数值,128054,是一个十六进制1F436的十进制表示。其等同于 <code>狗脸表情</code> 的Unicode 标量 U+1F436。</p>
|
||||
</code></pre><p>同样,前四个代码单元值 (68, 111, 103, 33) 代表了字符 <code>D</code> <code>o</code> <code>g</code> 和 <code>!</code> 。
|
||||
第五位数值,128054,是一个十六进制1F436的十进制表示。
|
||||
其等同于 <code>狗脸表情</code> 的Unicode 标量 U+1F436。</p>
|
||||
<p>作为查询字符值属性的一种替代方法,每个 <code>UnicodeScalar</code> 值也可以用来构建一个新的字符串值,比如在字符串插值中使用:</p>
|
||||
<pre><code>for scalar in dogString.unicodeScalars {
|
||||
println("\(scalar) ")
|
||||
@ -810,8 +846,8 @@ print("\n")
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.4" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="2.4" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -591,8 +590,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.5" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="2.5" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -591,8 +590,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.6" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="2.6" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -576,7 +575,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_12">
|
||||
<section class="normal" id="section-gitbook_16">
|
||||
|
||||
<h1 id="-functions-">函数(Functions)</h1>
|
||||
<p>函数是用来完成特定任务的独立的代码块。你给一个函数起一个合适的名字,用来标示函数做什么,并且当函数需要执行的时候,这个名字会被“调用”。</p>
|
||||
@ -625,8 +624,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.7" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="2.7" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -576,6 +575,243 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_17">
|
||||
|
||||
<h1 id="-">闭包</h1>
|
||||
<p>闭包是功能性自包含模块,可以在代码中被传递和使用。
|
||||
Swift 中的闭包与 C 和 Objective-C 中的 <code>blocks</code> 以及其他一些编程语言中的 <code>lambdas</code> 比较相似。</p>
|
||||
<p>闭包可以 <strong>捕获</strong> 和存储其所在上下文中任意常量和变量的引用。
|
||||
这就是所谓的闭合并包裹着这些常量和变量,俗称闭包。Swift 会为您管理在 <strong>捕获</strong> 过程中涉及到的内存操作。</p>
|
||||
<blockquote>
|
||||
<p>注意:</p>
|
||||
<p>如果您不熟悉 <strong>捕获</strong> (capturing) 这个概念也不用担心,后面会详细对其进行介绍。</p>
|
||||
</blockquote>
|
||||
<p>在 <code>函数</code> 章节中介绍的全局和嵌套函数实际上也是特殊的闭包,闭包采取如下三种形式之一:</p>
|
||||
<ul>
|
||||
<li>全局函数是一个有名字但不会捕获任何值的闭包</li>
|
||||
<li>嵌套函数是一个有名字并可以捕获其封闭函数域内值的闭包</li>
|
||||
<li>闭包表达式是一个利用轻量级语法所写的可以捕获其上下文中变量或常量值的没有名字的闭包</li>
|
||||
</ul>
|
||||
<p>Swift 的闭包表达式拥有简洁的风格,并鼓励在常见场景中进行语法优化,主要优化如下:</p>
|
||||
<ul>
|
||||
<li>利用上下文推断参数和返回值类型</li>
|
||||
<li>单表达式闭包可以省略 <code>return</code> 关键字</li>
|
||||
<li>参数名称缩写</li>
|
||||
<li>Trailing 闭包语法</li>
|
||||
</ul>
|
||||
<h3 id="-">闭包表达式</h3>
|
||||
<p>嵌套函数是一个在较复杂函数中方便进行命名和定义自包含代码模块的方式。
|
||||
当然,有时候撰写小巧的没有完整定义和命名的类函数结构也是很有用处的,尤其是在您处理一些函数并需要将另外一些函数作为该函数的参数时。</p>
|
||||
<p>闭包表达式是一种利用简洁语法构建内联闭包的方式。
|
||||
闭包表达式提供了一些语法优化,使得撰写闭包变得简单明了。
|
||||
下面闭包表达式的例子通过使用几次迭代展示了 <code>sort</code> 函数定义和语法优化的方式。
|
||||
每一次迭代都用更简洁的方式描述了相同的功能。</p>
|
||||
<h5 id="-sort-"><code>sort</code> 函数</h5>
|
||||
<p>Swift 标准库提供了 <code>sort</code> 函数,会根据您提供的排序闭包将已知类型数组中的值进行排序。
|
||||
一旦排序完成,函数会返回一个与原数组大小相同的新数组,该数组中包含已经正确排序的同类型元素。</p>
|
||||
<p>下面的闭包表达式示例使用 <code>sort</code> 函数对一个 <strong>String</strong> 类型的数组进行字母逆序排序,以下是初始数组值:</p>
|
||||
<pre><code>let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
|
||||
</code></pre><p>该例子对一个 <strong>String</strong> 类型的数组进行排序,因此排序闭包需为 <code>(String, String) -> Bool</code> 类型的函数。</p>
|
||||
<p>提供排序闭包的一种方式是撰写一个符合其类型要求的普通函数,并将其作为 <code>sort</code> 函数的第二个参数传入:</p>
|
||||
<pre><code>func backwards(s1: String, s2: String) -> Bool {
|
||||
return s1 > s2
|
||||
}
|
||||
var reversed = sort(names, backwards)
|
||||
// reversed is equal to ["Ewa", "Daniella", "Chris", "Barry", "Alex"]
|
||||
</code></pre><p>如果第一个字符串 (s1) 大于第二个字符串 (s2),<code>backwards</code> 函数则返回 <code>true</code>,表示在新的数组中 s1 应该出现在 s2 前。
|
||||
字符中的 "大于" 表示 "按照字母顺序后出现"。
|
||||
这意味着字母 "B" 大于字母 "A", 字符串 "Tom" 大于字符串 "Tim"。
|
||||
其将进行字母逆序排序,"Barry" 将会排在 "Alex" 之后。</p>
|
||||
<p>然而,这是一个相当冗长的方式,本质上只是写了一个单表达式函数 (a > b)。
|
||||
在下面的例子中,利用闭合表达式语法可以更好的构造一个内联排序闭包。</p>
|
||||
<h5 id="-">闭包表达式语法</h5>
|
||||
<p>闭包表达式语法有如下一般形式:</p>
|
||||
<pre><code>{ (parameters) -> returnType in
|
||||
statements
|
||||
}
|
||||
</code></pre><p>闭包表达式语法可以使用常量、变量和 <code>inout</code> 类型作为参数,不提供默认值。
|
||||
也可以在参数列表的最后使用可变参数。元组也可以作为参数和返回值。</p>
|
||||
<p>下面的例子展示了之前 <code>backwards</code> 函数对应的闭包表达式版本的代码:</p>
|
||||
<pre><code>reversed = sort(names, { (s1: String, s2: String) -> Bool in
|
||||
return s1 > s2
|
||||
})
|
||||
</code></pre><p>需要注意的是内联闭包参数和返回值类型声明与 <code>backwards</code> 函数类型声明相同。
|
||||
在这两种方式中,都写成了 (s1: String, s2: String) -> Bool。
|
||||
然而在内联闭包表达式中,函数和返回值类型都写在大括号内,而不是大括号外。</p>
|
||||
<p>闭包的函数体部分由关键字 <code>in</code> 引入。
|
||||
该关键字表示闭包的参数和返回值类型定义已经完成,闭包函数体即将开始。</p>
|
||||
<p>因为这个闭包的函数体部分如此短以至于可以将其改写成一行代码:</p>
|
||||
<pre><code>reversed = sort(names, { (s1: String, s2: String) -> Bool in return s1 > s2 } )
|
||||
</code></pre><p>这说明 <code>sort</code> 函数的整体调用保持不变,一对圆括号仍然包裹住了函数中整个参数集合。而其中一个参数现在变成了内联闭包 (相比于 <code>backwards</code> 版本的代码)。</p>
|
||||
<h5 id="-">根据上下文推断类型</h5>
|
||||
<p>因为排序闭包是作为函数的参数进行传入的,Swift可以推断其参数和返回值的类型。
|
||||
<code>sort</code> 期望第二个参数是类型为 <code>(String, String) -> Bool</code> 的函数,因此实际上 <code>String</code>, <code>String</code> 和 <code>Bool</code> 类型并不需要作为闭包表达式定义中的一部分。
|
||||
因为所有的类型都可以被正确推断,返回箭头 (->) 和 围绕在参数周围的括号也可以被省略:</p>
|
||||
<pre><code>reversed = sort(names, { s1, s2 in return s1 > s2 } )
|
||||
</code></pre><p>实际上任何情况下,通过内联闭包表达式构造的闭包作为参数传递给函数时,都可以推断出闭包的参数和返回值类型,这意味着您几乎不需要利用完整格式构造任何内联闭包。</p>
|
||||
<h5 id="-return-">单行表达式闭包可以省略 <code>return</code></h5>
|
||||
<p>单行表达式闭包可以通过隐藏 <code>return</code> 关键字来隐式返回单行表达式的结果,如上版本的例子可以改写为:</p>
|
||||
<pre><code>reversed = sort(names, { s1, s2 in s1 > s2 } )
|
||||
</code></pre><p>在这个例子中,<code>sort</code> 函数的第二个参数函数类型明确了闭包必须返回一个 <strong>Bool</strong> 类型值。
|
||||
因为闭包函数体只包含了一个单一表达式 (s1 > s2),该表达式返回 <strong>Bool</strong> 类型值,因此这里没有歧义,<code>return</code>关键字可以省略。</p>
|
||||
<h5 id="-">参数名称缩写</h5>
|
||||
<p>Swift 自动为内联函数提供了参数名称缩写功能,您可以直接通过 <code>$0</code>,<code>$1</code>,<code>$2</code> 来顺序调用闭包的参数。</p>
|
||||
<p>如果您在闭包表达式中使用参数名称缩写,您可以在闭包参数列表中省略对其的定义,并且对应参数名称缩写的类型会通过函数类型进行推断。
|
||||
<code>in</code> 关键字也同样可以被省略,因为此时闭包表达式完全由闭包函数体构成:</p>
|
||||
<pre><code>reversed = sort(names, { $0 > $1 } )
|
||||
</code></pre><p>在这个例子中,<code>$0</code> 和 <code>$1</code> 表示闭包中第一个和第二个 <strong>String</strong> 类型的参数。</p>
|
||||
<h5 id="-">运算符函数</h5>
|
||||
<p>实际上还有一种更简短的方式来撰写上面例子中的闭包表达式。
|
||||
Swift 的 <strong>String</strong> 类型定义了关于大于号 (>) 的字符串实现,其作为一个函数接受两个 <strong>String</strong> 类型的参数并返回 <strong>Bool</strong> 类型的值。
|
||||
而这正好与 <code>sort</code> 函数的第二个参数需要的函数类型相符合。
|
||||
因此,您可以简单地传递一个大于号,Swift可以自动推断出您想使用大于号的字符串函数实现:</p>
|
||||
<pre><code>reversed = sort(names, >)
|
||||
</code></pre><p>更多关于运算符表达式的内容请查看 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html#//apple_ref/doc/uid/TP40014097-CH27-XID_43" target="_blank">Operator Functions</a> 。</p>
|
||||
<h3 id="trailing-">Trailing 闭包</h3>
|
||||
<p>如果您需要将一个很长的闭包表达式作为最后一个参数传递给函数,可以使用 trailing 闭包来增强函数的可读性。
|
||||
Trailing 闭包是一个书写在函数括号之外(之后)的闭包表达式,函数支持将其作为最后一个参数调用。</p>
|
||||
<pre><code>func someFunctionThatTakesAClosure(closure: () -> ()) {
|
||||
// 函数体部分
|
||||
}
|
||||
|
||||
// 以下是不使用 trailing 闭包进行函数调用
|
||||
|
||||
someFunctionThatTakesAClosure({
|
||||
// 闭包主体部分
|
||||
})
|
||||
|
||||
// 以下是使用 trailing 闭包进行函数调用
|
||||
|
||||
someFunctionThatTakesAClosure() {
|
||||
// 闭包主体部分
|
||||
}
|
||||
</code></pre><blockquote>
|
||||
<p>注意:</p>
|
||||
<p>如果函数只需要闭包表达式一个参数,当您使用 trailing 闭包时,您甚至可以把 () 省略掉。
|
||||
NOTE</p>
|
||||
</blockquote>
|
||||
<p>在上例中作为 <code>sort</code> 函数参数的字符串排序闭包可以改写为:</p>
|
||||
<pre><code>reversed = sort(names) { $0 > $1 }
|
||||
</code></pre><p>当闭包非常长以至于不能在一行中进行书写时,Trailing 闭包变得非常有用。
|
||||
举例来说,Swift 的 <strong>Array</strong> 类型有一个 <code>map</code> 方法,其获取一个闭包表达式作为其唯一参数。
|
||||
数组中的每一个元素调用一次该闭包函数,并返回该元素所映射的值(也可以是不同类型的值)。
|
||||
具体的映射方式和返回值类型由闭包来指定。</p>
|
||||
<p>当提供给数组闭包函数后,<code>map</code> 方法将返回一个新的数组,数组中包含了与原数组一一对应的映射后的值。</p>
|
||||
<p>下例介绍了如何在 <code>map</code> 方法中使用 trailing 闭包将 <strong>Int</strong> 类型数组 <code>[16,58,510]</code> 转换为包含对应 <strong>String</strong> 类型的数组 <code>["OneSix", "FiveEight", "FiveOneZero"]</code>:</p>
|
||||
<pre><code>let digitNames = [
|
||||
0: "Zero", 1: "One", 2: "Two", 3: "Three", 4: "Four",
|
||||
5: "Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine"
|
||||
]
|
||||
let numbers = [16, 58, 510]
|
||||
</code></pre><p>如上代码创建了一个数字位和他们名字映射的英文版本字典。
|
||||
同时定义了一个准备转换为字符串的整型数组。</p>
|
||||
<p>您现在可以通过传递一个 trailing 闭包给 <code>numbers</code> 的 <code>map</code> 方法来创建对应的字符串版本数组。
|
||||
需要注意的时调用 <code>numbers.map</code> 不需要在 <code>map</code> 后面包含任何括号,因为其只需要传递闭包表达式这一个参数,并且该闭包表达式参数通过 trailing 方式进行撰写:</p>
|
||||
<pre><code>let strings = numbers.map {
|
||||
(var number) -> String in
|
||||
var output = ""
|
||||
while number > 0 {
|
||||
output = digitNames[number % 10]! + output
|
||||
number /= 10
|
||||
}
|
||||
return output
|
||||
}
|
||||
// strings 常量被推断为字符串类型数组,即 String[]
|
||||
// 其值为 ["OneSix", "FiveEight", "FiveOneZero"]
|
||||
</code></pre><p><code>map</code> 在数组中为每一个元素调用了闭包表达式。
|
||||
您不需要指定闭包的输入参数 <code>number</code> 的类型,因为可以通过要映射的数组类型进行推断。</p>
|
||||
<p>闭包 <code>number</code> 参数被声明为一个变量参数 (变量的具体描述请参看<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html#//apple_ref/doc/uid/TP40014097-CH10-XID_224" target="_blank">Constant and Variable Parameters</a>),因此可以在闭包函数体内对其进行修改。
|
||||
闭包表达式制定了返回类型为 <strong>String</strong>,以表明存储映射值的新数组类型为 <strong>String</strong>。</p>
|
||||
<p>闭包表达式在每次被调用的时候创建了一个字符串并返回。
|
||||
其使用求余运算符 (number % 10) 计算最后一位数字并利用 <code>digitNames</code> 字典获取所映射的字符串。</p>
|
||||
<blockquote>
|
||||
<p>注意:</p>
|
||||
<p>字典 <code>digitNames</code> 下标后跟着一个叹号 (!),因为字典下标返回一个可选值 (optional value),表明即使该 key 不存在也不会查找失败。
|
||||
在上例中,它保证了 <code>number % 10</code> 可以总是作为一个 <code>digitNames</code> 字典的有效下标 key。
|
||||
因此叹号可以用于强制展开 (force-unwrap) 存储在可选下标项中的 <strong>String</strong> 类型值。</p>
|
||||
</blockquote>
|
||||
<p>从 <code>digitNames</code> 字典中获取的字符串被添加到输出的前部,逆序建立了一个字符串版本的数字。
|
||||
(在表达式 <code>number % 10</code>中,如果number为16,则返回6,58返回8,510返回0)。</p>
|
||||
<p><code>number</code> 变量之后除以10。
|
||||
因为其是整数,在计算过程中未除尽部分被忽略。
|
||||
因此 16变成了1,58变成了5,510变成了51。</p>
|
||||
<p>整个过程重复进行,直到 <code>number /= 10</code> 为0,这时闭包会将字符串输出,而map函数则会将字符串添加到所映射的数组中。</p>
|
||||
<p>上例中 trailing 闭包语法在函数后整洁封装了具体的闭包功能,而不再需要将整个闭包包裹在 <code>map</code> 函数的括号内。</p>
|
||||
<h3 id="-caputure-">捕获 (Caputure)</h3>
|
||||
<p>闭包可以在其定义的上下文中捕获常量或变量。
|
||||
即使定义这些常量和变量的原域已经不存在,闭包仍然可以在闭包函数体内引用和修改这些值。</p>
|
||||
<p>Swift最简单的闭包形式是嵌套函数,也就是定义在其他函数的函数体内的函数。
|
||||
嵌套函数可以捕获其外部函数所有的参数以及定义的常量和变量。</p>
|
||||
<p>下例为一个叫做 <code>makeIncrementor</code> 的函数,其包含了一个叫做 <code>incrementor</code> 嵌套函数。
|
||||
嵌套函数 <code>incrementor</code> 从上下文中捕获了两个值,<code>runningTotal</code> 和 <code>amount</code>。
|
||||
之后 <code>makeIncrementor</code> 将 <code>incrementor</code> 作为闭包返回。
|
||||
每次调用 <code>incrementor</code> 时,其会以 <code>amount</code> 作为增量增加 <code>runningTotal</code> 的值。</p>
|
||||
<pre><code>func makeIncrementor(forIncrement amount: Int) -> () -> Int {
|
||||
var runningTotal = 0
|
||||
func incrementor() -> Int {
|
||||
runningTotal += amount
|
||||
return runningTotal
|
||||
}
|
||||
return incrementor
|
||||
}
|
||||
</code></pre><p><code>makeIncrementor</code> 返回类型为 <code>() -> Int</code>。
|
||||
这意味着其返回的是一个函数,而不是一个简单类型值。
|
||||
该函数在每次调用时不接受参数只返回一个 <strong>Int</strong> 类型的值。
|
||||
关于函数返回其他函数的内容,请查看<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html#//apple_ref/doc/uid/TP40014097-CH10-XID_232" target="_blank">Function Types as Return Types</a>。</p>
|
||||
<p><code>makeIncrementor</code> 函数定义了一个整型变量 <code>runningTotal</code> (初始为0) 用来存储当前跑步总数。
|
||||
该值通过 <code>incrementor</code> 返回。</p>
|
||||
<p><code>makeIncrementor</code> 有一个 <strong>Int</strong> 类型的参数,其外部命名为 <code>forIncrement</code>, 内部命名为 <code>amount</code>,表示每次 <code>incrementor</code> 被调用时 <code>runningTotal</code> 将要增加的量。</p>
|
||||
<p><code>incrementor</code> 函数用来执行实际的增加操作。
|
||||
该函数简单地使 <code>runningTotal</code> 增加 <code>amount</code>,并将其返回。</p>
|
||||
<p>如果我们单独看这个函数,会发现看上去不同寻常:</p>
|
||||
<pre><code>func incrementor() -> Int {
|
||||
runningTotal += amount
|
||||
return runningTotal
|
||||
}
|
||||
</code></pre><p><code>incrementor</code> 函数并没有获取任何参数,但是在函数体内访问了 <code>runningTotal</code> 和 <code>amount</code> 变量。这是因为其通过捕获在包含它的函数体内已经存在的 <code>runningTotal</code> 和 <code>amount</code> 变量而实现。</p>
|
||||
<p>由于没有修改 <code>amount</code> 变量,<code>incrementor</code> 实际上捕获并存储了该变量的一个副本,而该副本随着 <code>incrementor</code> 一同被存储。</p>
|
||||
<p>然而,因为每次调用该函数的时候都会修改 <code>runningTotal</code> 的值,<code>incrementor</code> 捕获了当前 <code>runningTotal</code> 变量的引用,而不是仅仅复制该变量的初始值。捕获一个引用保证了当 <code>makeIncrementor</code> 结束时候并不会消失,也保证了当下一次执行 <code>incrementor</code> 函数时,<code>runningTotal</code> 可以继续增加。</p>
|
||||
<blockquote>
|
||||
<p>注意:</p>
|
||||
<p>Swift 会决定捕获引用还是拷贝值。
|
||||
您不需要标注 <code>amount</code> 或者 <code>runningTotal</code> 来声明在嵌入的 <code>incrementor</code> 函数中的使用方式。
|
||||
Swift 同时也处理 <code>runingTotal</code> 变量的内存管理操作,如果不再被 <code>incrementor</code> 函数使用,则会被清除。</p>
|
||||
</blockquote>
|
||||
<p>下面为一个使用 <code>makeIncrementor</code> 的例子:</p>
|
||||
<pre><code>let incrementByTen = makeIncrementor(forIncrement: 10)
|
||||
</code></pre><p>该例子定义了一个叫做 <code>incrementByTen</code> 的常量,该常量指向一个每次调用会加10的 <code>incrementor</code> 函数。
|
||||
调用这个函数多次可以得到以下结果:</p>
|
||||
<pre><code>incrementByTen()
|
||||
// 返回的值为10
|
||||
incrementByTen()
|
||||
// 返回的值为20
|
||||
incrementByTen()
|
||||
// 返回的值为30
|
||||
</code></pre><p>如果您创建了另一个 <code>incrementor</code>,其会有一个属于自己的独立的 <code>runningTotal</code> 变量的引用。
|
||||
下面的例子中,<code>incrementBySevne</code> 捕获了一个新的 <code>runningTotal</code> 变量,该变量和 <code>incrementByTen</code> 中捕获的变量没有任何联系:</p>
|
||||
<pre><code>let incrementBySeven = makeIncrementor(forIncrement: 7)
|
||||
incrementBySeven()
|
||||
// 返回的值为7
|
||||
incrementByTen()
|
||||
// 返回的值为40
|
||||
</code></pre><blockquote>
|
||||
<p>注意:</p>
|
||||
<p>如果您闭包分配给一个类实例的属性,并且该闭包通过指向该实例或其成员来捕获了该实例,您将创建一个在闭包和实例间的强引用环。
|
||||
Swift 使用捕获列表来打破这种强引用环。更多信息,请参考 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html#//apple_ref/doc/uid/TP40014097-CH20-XID_61" target="_blank">Strong Reference Cycles for Closures</a>。</p>
|
||||
</blockquote>
|
||||
<h3 id="-">闭包是引用类型</h3>
|
||||
<p>上面的例子中,<code>incrementBySeven</code> 和 <code>incrementByTen</code> 是常量,但是这些常量指向的闭包仍然可以增加其捕获的变量值。
|
||||
这是因为函数和闭包都是引用类型。</p>
|
||||
<p>无论您将函数/闭包赋值给一个常量还是变量,您实际上都是将常量/变量的值设置为对应函数/闭包的引用。
|
||||
上面的例子中,<code>incrementByTen</code> 指向闭包的引用是一个常量,而并非闭包内容本身。</p>
|
||||
<p>这也意味着如果您将闭包赋值给了两个不同的常量/变量,两个值都会指向同一个闭包:</p>
|
||||
<pre><code>let alsoIncrementByTen = incrementByTen
|
||||
alsoIncrementByTen()
|
||||
// 返回的值为50
|
||||
</code></pre>
|
||||
|
||||
</section>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -591,8 +827,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.8" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="2.8" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -591,8 +590,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.9" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="2.9" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -576,7 +575,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_15">
|
||||
<section class="normal" id="section-gitbook_19">
|
||||
|
||||
<h3 id="-">类和结构体</h3>
|
||||
|
||||
@ -598,8 +597,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.10" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="2.10" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -591,8 +590,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.11" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="2.11" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -591,8 +590,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.12" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="2.12" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -591,8 +590,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.13" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="2.13" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -584,15 +583,15 @@
|
||||
<a href="../chapter2/12_Subscripts.html" class="navigation navigation-prev " aria-label="Previous page: 下标"><i class="fa fa-angle-left"></i></a>
|
||||
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" class="navigation navigation-next " aria-label="Next page: 构造过程"><i class="fa fa-angle-right"></i></a>
|
||||
<a href="../chapter2/14_Initialization.html" class="navigation navigation-next " aria-label="Next page: 构造函数"><i class="fa fa-angle-right"></i></a>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
|
||||
|
||||
<meta charset="UTF-8">
|
||||
<title>构造过程 | Swift 编程语言</title>
|
||||
<title>构造函数 | Swift 编程语言</title>
|
||||
|
||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
||||
<meta name="robots" content="index, follow">
|
||||
@ -21,7 +21,7 @@
|
||||
<link rel="prev" href="../chapter2/13_Inheritance.html" />
|
||||
|
||||
|
||||
<meta property="og:title" content="构造过程 | Swift 编程语言">
|
||||
<meta property="og:title" content="构造函数 | Swift 编程语言">
|
||||
<meta property="og:site_name" content="Swift 编程语言">
|
||||
<meta property="og:type" content="book">
|
||||
<meta property="og:locale" content="en_US">
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.14" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="2.14" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -584,15 +583,15 @@
|
||||
<a href="../chapter2/13_Inheritance.html" class="navigation navigation-prev " aria-label="Previous page: 继承"><i class="fa fa-angle-left"></i></a>
|
||||
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" class="navigation navigation-next " aria-label="Next page: 析构过程"><i class="fa fa-angle-right"></i></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" class="navigation navigation-next " aria-label="Next page: 析构函数"><i class="fa fa-angle-right"></i></a>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
|
||||
|
||||
<meta charset="UTF-8">
|
||||
<title>析构过程 | Swift 编程语言</title>
|
||||
<title>析构函数 | Swift 编程语言</title>
|
||||
|
||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
||||
<meta name="robots" content="index, follow">
|
||||
@ -21,7 +21,7 @@
|
||||
<link rel="prev" href="../chapter2/14_Initialization.html" />
|
||||
|
||||
|
||||
<meta property="og:title" content="析构过程 | Swift 编程语言">
|
||||
<meta property="og:title" content="析构函数 | Swift 编程语言">
|
||||
<meta property="og:site_name" content="Swift 编程语言">
|
||||
<meta property="og:type" content="book">
|
||||
<meta property="og:locale" content="en_US">
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.15" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="2.15" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -581,7 +580,7 @@
|
||||
</div>
|
||||
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" class="navigation navigation-prev " aria-label="Previous page: 构造过程"><i class="fa fa-angle-left"></i></a>
|
||||
<a href="../chapter2/14_Initialization.html" class="navigation navigation-prev " aria-label="Previous page: 构造函数"><i class="fa fa-angle-left"></i></a>
|
||||
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" class="navigation navigation-next " aria-label="Next page: 自动引用计数"><i class="fa fa-angle-right"></i></a>
|
||||
@ -591,8 +590,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.16" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="2.16" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -576,7 +575,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_22">
|
||||
<section class="normal" id="section-gitbook_26">
|
||||
|
||||
<h1 id="-">自动引用计数</h1>
|
||||
<p>Swift使用自动引用计数(ARC)这一机制来跟踪和管理你的应用程序的内存。通常情况下,Swift的内存管理机制会一直起着作用,你无须自己来考虑内存的管理。ARC会在类的实例不再被使用时,自动释放其占用的内存。</p>
|
||||
@ -599,7 +598,7 @@
|
||||
</div>
|
||||
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" class="navigation navigation-prev " aria-label="Previous page: 析构过程"><i class="fa fa-angle-left"></i></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" class="navigation navigation-prev " aria-label="Previous page: 析构函数"><i class="fa fa-angle-left"></i></a>
|
||||
|
||||
|
||||
<a href="../chapter2/17_Optional_Chaining.html" class="navigation navigation-next " aria-label="Next page: 可选链"><i class="fa fa-angle-right"></i></a>
|
||||
@ -609,8 +608,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.17" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="2.17" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -591,8 +590,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.18" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="2.18" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -591,8 +590,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.19" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="2.19" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -576,7 +575,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_25">
|
||||
<section class="normal" id="section-gitbook_29">
|
||||
|
||||
<h1 id="-">类型嵌套</h1>
|
||||
<p>枚举类型常被用于实现特定类或结构体的功能。也能够在有多种变量类型的环境中,方便地定义通用类或结构体来使用,为了实现这种功能,Swift允许你定义类型嵌套,可以在枚举类型、类和结构体中定义支持嵌套的类型。</p>
|
||||
@ -654,8 +653,8 @@ println("theAceOfSpades: \(theAceOfSpades.description)")
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.20" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="2.20" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -591,8 +590,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.21" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="2.21" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -591,8 +590,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.22" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="2.22" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -576,6 +575,357 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_32">
|
||||
|
||||
<h1 id="-">泛型</h1>
|
||||
<hr>
|
||||
<p><em>泛型代码</em>可以确保你写出灵活的,可重用的函数和定义出任何你所确定好的需求的类型。你的可以写出避免重复的代码,并且用一种清晰的,抽象的方式表达出来。</p>
|
||||
<p>泛型是Swift需要强大特征中的其中一个,许多Swift标准库是通过泛型代码构建出来的。事实上,你已经使用泛型贯穿着整个Language Guide,即便你没有实现它。例如:Swift的Array和Dictionary类型都是泛型集。你可以创建一个Int数组,也可创建一个String数组,或者甚至于可以是任何其他Swift的类型数据数组。同样的,你也可以创建存储任何指定类型的字典(dictionary),而且这些类型可以是没有限制的。</p>
|
||||
<hr>
|
||||
<h2 id="-">泛型所解决的问题</h2>
|
||||
<p>这里是一个标准的,非泛型函数swapTwoInts,用来交换两个Int值:</p>
|
||||
<pre><code class="lang-C"> func swapTwoInts(inout a: Int, inout b: Int)
|
||||
let temporaryA = a
|
||||
a = b
|
||||
b = temporaryA
|
||||
}
|
||||
</code></pre>
|
||||
<p>这个函数使用in-out参数交换a和b的值,这两个参数被描述为[In-Out类型参数][1]。</p>
|
||||
<p><code>swapTwoInts</code>函数可以交换b的原始值到a,也可以交换a的原始值到b,你可以调用这个函数交换两个Int变量值:</p>
|
||||
<pre><code class="lang-C"> var someInt = <span class="hljs-number">3</span>
|
||||
var anotherInt = <span class="hljs-number">107</span>
|
||||
swapTwoInts(&someInt, &anotherInt)
|
||||
println(<span class="hljs-string">"someInt is now \(someInt), and anotherInt is now \(anotherInt)"</span>)
|
||||
<span class="hljs-comment">// prints "someInt is now 107, and anotherInt is now 3"</span>
|
||||
</code></pre>
|
||||
<p><code>swapTwoInts</code>函数是非常有用的,但是它只能交换Int值,如果你想要交换两个String或者Double,就不得不写更多的函数,如 <code>swapTwoStrings</code>和<code>swapTwoDoublesfunctions</code>,如同如下所示:</p>
|
||||
<pre><code class="lang-C"> func swapTwoStrings(inout a: String, inout b: String) {
|
||||
let temporaryA = a
|
||||
a = b
|
||||
b = temporaryA
|
||||
}
|
||||
|
||||
func swapTwoDoubles(inout a: Double, inout b: Double) {
|
||||
let temporaryA = a
|
||||
a = b
|
||||
b = temporaryA
|
||||
}
|
||||
</code></pre>
|
||||
<p>你可能注意到 <code>swapTwoInts</code>、 <code>swapTwoStrings</code>和<code>swapTwoDoubles</code>函数主题都是相同的,唯一不同之处就在于传入的变量不同,分别是Int、String和Double。</p>
|
||||
<p>但实际应用中通常需要一个用处更强大并且尽可能的考虑到更多的灵活性单个函数,可以用来交换两个任何类型值,很幸运的是,泛型代码帮你解决了这种问题。(一个这种泛型函数后面已经定义好了。)</p>
|
||||
<blockquote>
|
||||
<p>NOTE</p>
|
||||
<p>In all three functions, it is important that the types of a and b are defined to be the same as each other. If a and b were not of the same type, it would not be possible to swap their values. Swift is a type-safe language, and does not allow (for example) a variable of type String and a variable of type Double to swap values with each other. Attempting to do so would be reported as a compile-time error.</p>
|
||||
</blockquote>
|
||||
<h2 id="-">泛型函数</h2>
|
||||
<p><code>泛型函数</code>可以工作于任何类型,这里是一个上面<code>swapTwoInts</code>函数的泛型版本,用于交换两个值: </p>
|
||||
<pre><code class="lang-C"> func swapTwoValues<T>(inout a: T, inout b: T) {
|
||||
let temporaryA = a
|
||||
a = b
|
||||
b = temporaryA
|
||||
}
|
||||
</code></pre>
|
||||
<p><code>swapTwoValues</code>函数主体和<code>swapTwoInts</code>函数是一样,而且,只在第一行稍微有那么一点点不同于<code>swapTwoInts</code>,如下所示:</p>
|
||||
<pre><code class="lang-C"> func swapTwoInts(inout a: Int, inout b: Int)
|
||||
func swapTwoValues<T>(inout a: T, inout b: T)
|
||||
</code></pre>
|
||||
<p>这个函数的泛型版本使用了节点类型命名(通常此情况下用字母T来表示)来代替实际类型名(如Int、String或Double)。节点类型名并不是表示T必须是任何类型,但是其规定a和b必须是同一类型的T,而不管T表示任何类型。只有<code>swapTwoValues</code>函数在每次调用时所传入的实际类型决定了T所代表的类型。</p>
|
||||
<p>另外一个不同之处在于这个泛型函数名后面跟着的节点类型名(T)是用尖括号括起来的(<T>)。这个尖括号告诉Swift那个T是<code>swapTwoValues</code>函数所定义的一个节点类型。因为T是一个节点,Swift不会去查找每一个命名为T的实际类型。</p>
|
||||
<p><code>swapTwoValues</code>函数除了只要传入的两个任何类型值是同一类型外,也可以作为<code>swapTwoInts</code>函数被调用。每次<code>swapTwoValues</code>被调用,T所代表的类型值都会传给函数。</p>
|
||||
<p>在下面的两个例子中,T分别代表Tnt和String:</p>
|
||||
<pre><code class="lang-C"> var someInt = <span class="hljs-number">3</span>
|
||||
var anotherInt = <span class="hljs-number">107</span>
|
||||
swapTwoValues(&someInt, &anotherInt)
|
||||
<span class="hljs-comment">// someInt is now 107, and anotherInt is now 3</span>
|
||||
|
||||
var someString = <span class="hljs-string">"hello"</span>
|
||||
var anotherString = <span class="hljs-string">"world"</span>
|
||||
swapTwoValues(&someString, &anotherString)
|
||||
<span class="hljs-comment">// someString is now "world", and anotherString is now "hello"</span>
|
||||
</code></pre>
|
||||
<blockquote>
|
||||
<p>NOTE</p>
|
||||
<p>The swapTwoValues function defined above is inspired by a generic function called swap, which is part of the Swift standard library, and is automatically made available for you to use in your apps. If you need the behavior of the swapTwoValues function in your own code, you can use Swift’s existing swap function rather than providing your own implementation.</p>
|
||||
</blockquote>
|
||||
<h2 id="type-parameters">Type Parameters</h2>
|
||||
<h2 id="-">类型参数</h2>
|
||||
<p>在上面的<code>swapTwoValues</code>例子中,节点类型T是一种类型参数的示例。类型参数指定并命名为一个节点类型,并且紧随在函数名后面,并用一对尖括号括起来(如<T>)。</p>
|
||||
<p>一旦一个类型参数被指定,那么其可以被使用来定义一个函数的参数类型(如<code>swapTwoValues</code>函数中的参数a和b),或作为一个函数返回类型,或用作函数主体中的注释类型。在这种情况下,被类型参数所代表的节点类型不管函数任何时候被调用,都会被实际类型所替换(在上面<code>swapTwoValues</code>例子中,当函数第一次被调用时,T被Int替换,第二次调用时,被String替换。)。</p>
|
||||
<p>你可支持多个类型参数,命名在尖括号中,用逗号分开。</p>
|
||||
<h2 id="-">命名类型参数</h2>
|
||||
<p>在简单的情况下,泛型函数或泛型类型需要指定一个节点类型(如上面的<code>swapTwoValues</code>泛型函数,或一个存储单一类型的泛型集,如Array),通常用一单个字母T来命名类型参数。不过,你可以使用任何有效的标识符来作为类型参数名。</p>
|
||||
<p>如果你使用多个参数定义更复杂的泛型函数或泛型类型,那么使用更多的描述类型参数是非常有用的。例如,Swift字典(Dictionary)类型有两个类型参数,一个是key,另外一个是值。如果你自己写字典,你或许会定义这两个类型参数为KeyType和ValueType,用来记住它们在你的泛型代码中的作用。</p>
|
||||
<blockquote>
|
||||
<p>NOTE</p>
|
||||
<p>Always give type parameters UpperCamelCase names (such as T and KeyType) to indicate that they are a placeholder for a type, not a value.</p>
|
||||
</blockquote>
|
||||
<h2 id="-">泛型类型</h2>
|
||||
<p>通常在泛型函数中,Swift允许你定义你自己的泛型类型。这些自定义类、结构体和枚举作用于任何类型,如同<code>Array</code>和<code>Dictionary</code>的用法。</p>
|
||||
<p>这部分向你展示如何写一个泛型集类型-<code>Stack</code>(栈)。一个栈是一系列值域的集合,和array(数组)相似,但其是一个比Swift的<code>Array</code>类型更多限制的集合。一个数组可以允许其里面任何位置的插入/删除操作,而栈,只允许,只允许在集合的末端添加新的项(如同<em>push</em>一个新值进栈)。同样的一个栈也只能从末端移除项(如同<em>pop</em>一个值出栈)。</p>
|
||||
<blockquote>
|
||||
<p>NOTE</p>
|
||||
<p>The concept of a stack is used by the UINavigationController class to model the view controllers in its navigation hierarchy. You call the UINavigationController class pushViewController:animated: method to add (or push) a view controller on to the navigation stack, and its popViewControllerAnimated: method to remove (or pop) a view controller from the navigation stack. A stack is a useful collection model whenever you need a strict “last in, first out” approach to managing a collection.</p>
|
||||
</blockquote>
|
||||
<p>下图展示了一个栈的压栈(push)/出栈(pop)的行为:</p>
|
||||
<p>![此处输入图片的描述][2]</p>
|
||||
<ol>
|
||||
<li>现在有三个值在栈中;</li>
|
||||
<li>第四个值“pushed”到栈的顶部;</li>
|
||||
<li>现在有四个值在栈中,最近的那个在顶部;</li>
|
||||
<li>栈中最顶部的那个项被移除,或称之为“popped”;</li>
|
||||
<li>移除掉一个值后,现在栈又重新只有三个值。</li>
|
||||
</ol>
|
||||
<p>这里展示了如何写一个非泛型版本的栈,<code>Int</code>值型的栈:</p>
|
||||
<pre><code class="lang-C"><span class="hljs-keyword">struct</span> IntStack {
|
||||
var items = Int[]()
|
||||
mutating func push(item: Int) {
|
||||
items.append(item)
|
||||
}
|
||||
mutating func pop() -> Int {
|
||||
<span class="hljs-keyword">return</span> items.removeLast()
|
||||
}
|
||||
}
|
||||
</code></pre>
|
||||
<p>这个结构体在栈中使用一个<code>Array</code>性质的<code>items</code>存储值。<code>Stack</code>提供两个方法:<code>push</code>和<code>pop</code>,从栈中压进一个值和移除一个值。这些方法标记为可变的,因为他们需要修改(或<em>转换</em>)结构体的<code>items</code>数组。</p>
|
||||
<p>上面所展现的<code>IntStack</code>类型只能用于<code>Int</code>值,不过,其对于定义一个泛型<code>Stack</code>类(可以处理<em>任何</em>类型值的栈)是非常有用的。</p>
|
||||
<p>这里是一个相同代码的泛型版本:</p>
|
||||
<pre><code class="lang-C"><span class="hljs-keyword">struct</span> Stack<T> {
|
||||
var items = T[]()
|
||||
mutating func push(item: T) {
|
||||
items.append(item)
|
||||
}
|
||||
mutating func pop() -> T {
|
||||
<span class="hljs-keyword">return</span> items.removeLast()
|
||||
}
|
||||
}
|
||||
</code></pre>
|
||||
<p>注意到<code>Stack</code>的泛型版本基本上和非泛型版本相同,但是泛型版本的节点类型参数为T代替了实际<code>Int</code>类型。这种类型参数包含在一对尖括号里(<code><T></code>),紧随在结构体名字后面。</p>
|
||||
<p>T定义了一个名为“某种类型T”的节点提供给后来用。这种将来类型可以在结构体的定义里任何地方表示为“T”。在这种情况下,T在如下三个地方被用作节点:</p>
|
||||
<ul>
|
||||
<li>创建一个名为<code>items</code>的属性,使用空的T类型值数组对其进行初始化;</li>
|
||||
<li>指定一个包含一个参数名为<code>item</code>的<code>push</code>方法,该参数必须是T类型;</li>
|
||||
<li>指定一个<code>pop</code>方法的返回值,该返回值将是一个T类型值。</li>
|
||||
</ul>
|
||||
<p>当创建一个新单例并初始化时, 通过用一对紧随在类型名后的尖括号里写出实际指定栈用到类型,创建一个<code>Stack</code>实例,同创建<code>Array</code>和<code>Dictionary</code>一样:</p>
|
||||
<pre><code class="lang-c">var stackOfStrings = Stack<String>()
|
||||
stackOfStrings.push(<span class="hljs-string">"uno"</span>)
|
||||
stackOfStrings.push(<span class="hljs-string">"dos"</span>)
|
||||
stackOfStrings.push(<span class="hljs-string">"tres"</span>)
|
||||
stackOfStrings.push(<span class="hljs-string">"cuatro"</span>)
|
||||
<span class="hljs-comment">// 现在栈已经有4个string了</span>
|
||||
</code></pre>
|
||||
<p>下图将展示<code>stackOfStrings</code>如何push这四个值进栈的过程:</p>
|
||||
<p>![此处输入图片的描述][3]</p>
|
||||
<p>从栈中pop并移除值"cuatro":</p>
|
||||
<pre><code class="lang-C">let fromTheTop = stackOfStrings.pop()
|
||||
<span class="hljs-comment">// fromTheTop is equal to "cuatro", and the stack now contains 3 strings</span>
|
||||
</code></pre>
|
||||
<p>下图展示了如何从栈中pop一个值的过程:
|
||||
![此处输入图片的描述][4]</p>
|
||||
<p>由于<code>Stack</code>是泛型类型,所以在Swift中其可以用来创建任何有效类型的栈,这种方式如同<code>Array</code>和<code>Dictionary</code>。</p>
|
||||
<h2 id="-">类型约束</h2>
|
||||
<p><code>swapTwoValues</code>函数和<code>Stack</code>类型可以作用于任何类型,不过,有的时候对使用在泛型函数和泛型类型上的类型强制约束为某种特定类型是非常有用的。类型约束指定了一个必须继承自指定类的类型参数,或者遵循一个特定的协议或协议构成。</p>
|
||||
<p>例如,Swift的<code>Dictionary</code>类型对作用于其keys的类型做了些限制。在[Dictionaries][5]的描述中,字典的keys类型必须是<em>hashable</em>,也就是说,必须有一种方法可以使其是唯一的表示。<code>Dictionary</code>之所以需要其keys是hashable是为了以便于其检查其是否包含某个特定key的值。如无此需求,<code>Dictionary</code>即不会告诉是否插入或者替换了某个特定key的值,也不能查找到已经存储在字典里面的给定key值。</p>
|
||||
<p>这个需求强制加上一个类型约束作用于<code>Dictionary</code>的key上,当然其key类型必须遵循<code>Hashable</code>协议(Swift标准库中定义的一个特定协议)。所有的Swift基本类型(如<code>String</code>,<code>Int</code>, <code>Double</code>和 <code>Bool</code>)默认都是hashable。</p>
|
||||
<p>当你创建自定义泛型类型时,你可以定义你自己的类型约束,当然,这些约束要支持泛型编程的强力特征中的多数。抽象概念如<code>Hashtable</code>具有的类型特征是根据他们概念特征来界定的,而不是他们的直接类型特征。</p>
|
||||
<h3 id="-">类型约束语法</h3>
|
||||
<p>你可以写一个在一个类型参数名后面的类型约束,通过冒号分割,来作为类型参数链的一部分。这种作用于泛型函数的类型约束的基础语法如下所示(和泛型类型的语法相同):</p>
|
||||
<pre><code class="lang-c">func someFunction<T: SomeClass, U: SomeProtocol>(someT: T, someU: U) {
|
||||
<span class="hljs-comment">// function body goes here</span>
|
||||
}
|
||||
</code></pre>
|
||||
<p>上面这个假定函数有两个类型参数。第一个类型参数<code>T</code>,有一个需要<code>T</code>必须是<code>SomeClass</code>子类的类型约束;第二个类型参数<code>U</code>,有一个需要<code>U</code>必须遵循<code>SomeProtocol</code>协议的类型约束。</p>
|
||||
<h3 id="-">类型约束行为</h3>
|
||||
<p>这里有个名为<code>findStringIndex</code>的非泛型函数,该函数功能是去查找包含一给定<code>String</code>值的数组。若查找到匹配的字符串,<code>findStringIndex</code>函数返回该字符串在数组中的索引值(<code>Int</code>),反之则返回<code>nil</code>:</p>
|
||||
<pre><code class="lang-c">func findStringIndex(<span class="hljs-built_in">array</span>: String[], valueToFind: String) -> Int? {
|
||||
<span class="hljs-keyword">for</span> (index, value) in enumerate(<span class="hljs-built_in">array</span>) {
|
||||
<span class="hljs-keyword">if</span> value == valueToFind {
|
||||
<span class="hljs-keyword">return</span> index
|
||||
}
|
||||
}
|
||||
<span class="hljs-keyword">return</span> nil
|
||||
}
|
||||
</code></pre>
|
||||
<p><code>findStringIndex</code>函数可以作用于查找一字符串数组中的某个字符串:</p>
|
||||
<pre><code class="lang-c">let strings = [<span class="hljs-string">"cat"</span>, <span class="hljs-string">"dog"</span>, <span class="hljs-string">"llama"</span>, <span class="hljs-string">"parakeet"</span>, <span class="hljs-string">"terrapin"</span>]
|
||||
<span class="hljs-keyword">if</span> let foundIndex = findStringIndex(strings, <span class="hljs-string">"llama"</span>) {
|
||||
println(<span class="hljs-string">"The index of llama is \(foundIndex)"</span>)
|
||||
}
|
||||
<span class="hljs-comment">// prints "The index of llama is 2"</span>
|
||||
</code></pre>
|
||||
<p>如果只是针对字符串而言查找在数组中的某个值的索引,用处不是很大,不过,你可以写出相同功能的泛型函数<code>findIndex</code>,用某个类型<code>T</code>值替换掉提到的字符串。</p>
|
||||
<p>这里展示如何写一个你或许期望的<code>findStringIndex</code>的泛型版本<code>findIndex</code>。请注意这个函数仍然返回<code>Int</code>,是不是有点迷惑呢,而不是泛型类型?那是因为函数返回的是一个可选的索引数,而不是从数组中得到的一个可选值。需要提醒的是,这个函数不会编译,原因在例子后面会说明:</p>
|
||||
<pre><code class="lang-c">func findIndex<T>(<span class="hljs-built_in">array</span>: T[], valueToFind: T) -> Int? {
|
||||
<span class="hljs-keyword">for</span> (index, value) in enumerate(<span class="hljs-built_in">array</span>) {
|
||||
<span class="hljs-keyword">if</span> value == valueToFind {
|
||||
<span class="hljs-keyword">return</span> index
|
||||
}
|
||||
}
|
||||
<span class="hljs-keyword">return</span> nil
|
||||
}
|
||||
</code></pre>
|
||||
<p>上面所写的函数不会编译。这个问题的位置在等式的检查上,<code>“if value == valueToFind”</code>。不是所有的Swift中的类型都可以用等式符(==)进行比较。例如,如果你创建一个你自己的类或结构体来表示一个复杂的数据模型,那么Swift没法猜到对于这个类或结构体而言“等于”的意思。正因如此,这部分代码不能可能保证工作于每个可能的类型<code>T</code>,当你试图编译这部分代码时估计会出现相应的错误。</p>
|
||||
<p>不过,所有的这些并不会让我们无从下手。Swift标准库中定义了一个<code>Equatable</code>协议,该协议要求任何遵循的类型实现等式符(==)和不等符(!=)对任何两个该类型进行比较。所有的Swift标准类型自动支持<code>Equatable</code>协议。</p>
|
||||
<p>任何<code>Equatable</code>类型都可以安全的使用在<code>findIndex</code>函数中,因为其保证支持等式操作。为了说明这个事实,当你定义一个函数时,你可以写一个<code>Equatable</code>类型约束作为类型参数定义的一部分:</p>
|
||||
<pre><code class="lang-c">func findIndex<T: Equatable>(<span class="hljs-built_in">array</span>: T[], valueToFind: T) -> Int? {
|
||||
<span class="hljs-keyword">for</span> (index, value) in enumerate(<span class="hljs-built_in">array</span>) {
|
||||
<span class="hljs-keyword">if</span> value == valueToFind {
|
||||
<span class="hljs-keyword">return</span> index
|
||||
}
|
||||
}
|
||||
<span class="hljs-keyword">return</span> nil
|
||||
}
|
||||
</code></pre>
|
||||
<p><code>findIndex</code>中这个单个类型参数写做:<code>T: Equatable</code>,也就意味着“任何T类型都遵循<code>Equatable</code>协议”。</p>
|
||||
<p>findIndex函数现在则可以成功的编译过,并且作用于任何遵循Equatable的类型,如Double或String:</p>
|
||||
<pre><code class="lang-c">let doubleIndex = findIndex([<span class="hljs-number">3.14159</span>, <span class="hljs-number">0.1</span>, <span class="hljs-number">0.25</span>], <span class="hljs-number">9.3</span>)
|
||||
<span class="hljs-comment">// doubleIndex is an optional Int with no value, because 9.3 is not in the array</span>
|
||||
let stringIndex = findIndex([<span class="hljs-string">"Mike"</span>, <span class="hljs-string">"Malcolm"</span>, <span class="hljs-string">"Andrea"</span>], <span class="hljs-string">"Andrea"</span>)
|
||||
<span class="hljs-comment">// stringIndex is an optional Int containing a value of 2</span>
|
||||
</code></pre>
|
||||
<h2 id="-">关联类型</h2>
|
||||
<p>当定义一个协议时,有的时候声明一个或多个关联类型作为协议定义的一部分是非常有用的。一个关联类型给定作用于协议部分的类型一个节点名(或<em>别名</em>)。作用于关联类型上实际类型是不需要指定的,直到该协议接受。关联类型被指定为<code>typealias</code>关键字。</p>
|
||||
<h3 id="-">关联类型行为</h3>
|
||||
<p>这里是一个<code>Container</code>协议的例子,定义了一个ItemType关联类型:</p>
|
||||
<pre><code class="lang-c">protocol Container {
|
||||
typealias ItemType
|
||||
mutating func append(item: ItemType)
|
||||
var count: Int { get }
|
||||
subscript(i: Int) -> ItemType { get }
|
||||
}
|
||||
</code></pre>
|
||||
<p><code>Container</code>协议定义了三个任何容器必须支持的兼容要求:</p>
|
||||
<ul>
|
||||
<li>必须可能通过<code>append</code>方法添加一个新item到容器里;</li>
|
||||
<li>必须可能通过使用<code>count</code>属性获取容器里items的数量,并返回一个<code>Int</code>值;</li>
|
||||
<li>必须可能通过容器的<code>Int</code>索引值下标可以检索到每一个item。</li>
|
||||
</ul>
|
||||
<p>这个协议没有指定容器里item是如何存储的或何种类型是允许的。这个协议只指定三个任何遵循<code>Container</code>类型所必须支持的功能点。一个遵循的类型也可以提供其他额外的功能,只要满足这三个条件。</p>
|
||||
<p>任何遵循<code>Container</code>协议的类型必须指定存储在其里面的值类型,必须保证只有正确类型的items可以加进容器里,必须明确可以通过其下标返回item类型。</p>
|
||||
<p>为了定义这三个条件,<code>Container</code>协议需要一个方法指定容器里的元素将会保留,而不需要知道特定容器的类型。<code>Container</code>协议需要指定任何通过<code>append</code>方法添加到容器里的值和容器里元素是相同类型,并且通过容器下标返回的容器元素类型的值的类型是相同类型。</p>
|
||||
<p>为了达到此目的,<code>Container</code>协议声明了一个ItemType的关联类型,写作<code>typealias ItemType</code>。The protocol does not define what ItemType is an alias for—that information is left for any conforming type to provide(这个协议不会定义<code>ItemType</code>是遵循类型所提供的何种信息的别名)。尽管如此,<code>ItemType</code>别名支持一种方法识别在一个容器里的items类型,以及定义一种使用在<code>append</code>方法和下标中的类型,以便保证任何期望的<code>Container</code>的行为是强制性的。</p>
|
||||
<p>这里是一个早前IntStack类型的非泛型版本,适用于遵循Container协议:</p>
|
||||
<pre><code class="lang-c"><span class="hljs-keyword">struct</span> IntStack: Container {
|
||||
<span class="hljs-comment">// original IntStack implementation</span>
|
||||
var items = Int[]()
|
||||
mutating func push(item: Int) {
|
||||
items.append(item)
|
||||
}
|
||||
mutating func pop() -> Int {
|
||||
<span class="hljs-keyword">return</span> items.removeLast()
|
||||
}
|
||||
<span class="hljs-comment">// conformance to the Container protocol</span>
|
||||
typealias ItemType = Int
|
||||
mutating func append(item: Int) {
|
||||
self.push(item)
|
||||
}
|
||||
var count: Int {
|
||||
<span class="hljs-keyword">return</span> items.count
|
||||
}
|
||||
subscript(i: Int) -> Int {
|
||||
<span class="hljs-keyword">return</span> items[i]
|
||||
}
|
||||
}
|
||||
</code></pre>
|
||||
<p><code>IntStack</code>类型实现了<code>Container</code>协议的所有三个要求,在<code>IntStack</code>类型的每个包含部分的功能都满足这些要求。</p>
|
||||
<p>此外,<code>IntStack</code>指定了<code>Container</code>的实现,适用的ItemType被用作<code>Int</code>类型。对于这个<code>Container</code>协议实现而言,定义 <code>typealias ItemType = Int</code>,将抽象的<code>ItemType</code>类型转换为具体的<code>Int</code>类型。</p>
|
||||
<p>感谢Swift类型参考,你不用在<code>IntStack</code>定义部分声明一个具体的<code>Int</code>的<code>ItemType</code>。由于<code>IntStack</code>遵循<code>Container</code>协议的所有要求,只要通过简单的查找<code>append</code>方法的item参数类型和下标返回的类型,Swift就可以推断出合适的<code>ItemType</code>来使用。确实,如果上面的代码中你删除了 <code>typealias ItemType = Int</code>这一行,一切仍旧可以工作,因为它清楚的知道ItemType使用的是何种类型。</p>
|
||||
<p>你也可以生成遵循<code>Container</code>协议的泛型<code>Stack</code>类型:</p>
|
||||
<pre><code class="lang-c"><span class="hljs-keyword">struct</span> Stack<T>: Container {
|
||||
<span class="hljs-comment">// original Stack<T> implementation</span>
|
||||
var items = T[]()
|
||||
mutating func push(item: T) {
|
||||
items.append(item)
|
||||
}
|
||||
mutating func pop() -> T {
|
||||
<span class="hljs-keyword">return</span> items.removeLast()
|
||||
}
|
||||
<span class="hljs-comment">// conformance to the Container protocol</span>
|
||||
mutating func append(item: T) {
|
||||
self.push(item)
|
||||
}
|
||||
var count: Int {
|
||||
<span class="hljs-keyword">return</span> items.count
|
||||
}
|
||||
subscript(i: Int) -> T {
|
||||
<span class="hljs-keyword">return</span> items[i]
|
||||
}
|
||||
}
|
||||
</code></pre>
|
||||
<p>这个时候,节点类型参数<code>T</code>被用作<code>append</code>方法的item参数和下标的返回类型。Swift因此可以推断出被用作这个特定容器的<code>ItemType</code>的<code>T</code>的合适类型。</p>
|
||||
<h3 id="-">扩展一个存在的类型为一指定关联类型</h3>
|
||||
<p>在[Adding Protocol Conformance with an Extension][6]中有描述扩展一个存在的类型添加遵循一个协议。这个类型包含一个关联类型的协议。</p>
|
||||
<p>Swift的<code>Array</code>已经提供<code>append</code>方法,一个<code>count</code>属性和通过下标来查找一个自己的元素。这三个功能都达到<code>Container</code>协议的要求。也就意味着你可以扩展<code>Array</code>去遵循<code>Container</code>协议,只要通过简单声明<code>Array</code>适用于该协议而已。如何实践这样一个空扩展,在[Declaring Protocol Adoption with an Extension][7]中有描述这样一个实现一个空扩展的行为:</p>
|
||||
<pre><code class="lang-c">extension Array: Container {}
|
||||
</code></pre>
|
||||
<p>如同上面的泛型<code>Stack</code>类型一样,<code>Array的append</code>方法和下标保证<code>Swift</code>可以推断出<code>ItemType</code>所使用的适用的类型。定义了这个扩展后,你可以将任何<code>Array</code>当作<code>Container</code>来使用。</p>
|
||||
<h2 id="where-">Where 语句</h2>
|
||||
<p>[Type Constraints][8]中描述的类型约束确保你定义关于类型参数的需求和一泛型函数或类型有关联。</p>
|
||||
<p>对于关联类型的定义需求也是非常有用的。你可以通过这样去定义<em>where语句</em>作为一个类型参数队列的一部分。一个where语句使你能够要求一个关联类型遵循一个特定的协议,以及(或)那个特定的类型参数和关联类型可以是相同的。你可写一个where语句,通过紧随放置<code>where</code>关键字在类型参数队列后面,其后跟着一个或者多个针对关联类型的约束,以及(或)一个或多个类型和关联类型的等于关系。</p>
|
||||
<p>下面的列子定义了一个名为<code>allItemsMatch</code>的泛型函数,用来检查是否两个<code>Container</code>单例包含具有相同顺序的相同items。如果匹配到所有的items,那么返回一个为true的Boolean值,反之,则相反。</p>
|
||||
<p>这两个容器可以被检查出是否是相同类型的容器(虽然它们可以是),但他们确实拥有相同类型的items。这个需求通过一个类型约束和where语句结合来表示:</p>
|
||||
<pre><code class="lang-c">func allItemsMatch<
|
||||
C1: Container, C2: Container
|
||||
where C1.ItemType == C2.ItemType, C1.ItemType: Equatable>
|
||||
(someContainer: C1, anotherContainer: C2) -> Bool {
|
||||
|
||||
<span class="hljs-comment">// check that both containers contain the same number of items</span>
|
||||
<span class="hljs-keyword">if</span> someContainer.count != anotherContainer.count {
|
||||
<span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>
|
||||
}
|
||||
|
||||
<span class="hljs-comment">// check each pair of items to see if they are equivalent</span>
|
||||
<span class="hljs-keyword">for</span> i in <span class="hljs-number">0.</span>.someContainer.count {
|
||||
<span class="hljs-keyword">if</span> someContainer[i] != anotherContainer[i] {
|
||||
<span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>
|
||||
}
|
||||
}
|
||||
|
||||
<span class="hljs-comment">// all items match, so return true</span>
|
||||
<span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>
|
||||
|
||||
}
|
||||
</code></pre>
|
||||
<p>这个函数用了两个参数:<code>someContainer</code>和<code>anotherContainer</code>。<code>someContainer</code>参数是类型<code>C1</code>,<code>anotherContainer</code>参数是类型<code>C2</code>。<code>C1</code>和<code>C2</code>是容器的两个节点类型参数,决定了这个函数何时被调用。</p>
|
||||
<p>这个函数的类型参数列紧随在两个类型参数需求的后面:</p>
|
||||
<ul>
|
||||
<li><code>C1</code>必须遵循<code>Container</code>协议 (写作 <code>C1: Container</code>)。</li>
|
||||
<li><code>C2</code>必须遵循<code>Container</code>协议 (写作 <code>C2: Container</code>)。</li>
|
||||
<li><code>C1</code>的<code>ItemType</code>同样是C2的<code>ItemType</code>(写作 <code>C1.ItemType == C2.ItemType</code>)。</li>
|
||||
<li><code>C1</code>的<code>ItemType</code>必须遵循<code>Equatable</code>协议 (写作 <code>C1.ItemType: Equatable</code>)。</li>
|
||||
</ul>
|
||||
<p>第三个和第四个要求被定义为一个where语句的一部分,写在关键字<code>where</code>后面,作为函数类型参数链的一部分。</p>
|
||||
<p>这些要求意思是:</p>
|
||||
<p><code>someContainer</code>是一个<code>C1</code>类型的容器。
|
||||
<code>anotherContainer</code>是一个<code>C2</code>类型的容器。
|
||||
<code>someContainer</code>和<code>anotherContainer</code>包含相同的items类型。
|
||||
<code>someContainer</code>中的items可以通过不等于操作(<code>!=</code>)来检查它们是否彼此不同。</p>
|
||||
<p>第三个和第四个要求结合起来的意思是<code>anotherContainer</code>中的items也可以通过 <code>!=</code> 操作来检查,因为他们在<code>someContainer</code>中items确实是相同的类型。</p>
|
||||
<p>这些要求能够使<code>allItemsMatch</code>函数比较两个容器,即便他们是不同的容器类型。</p>
|
||||
<p><code>allItemsMatch</code>首先检查两个容器是否拥有同样数目的items,如果他们的items数目不同,没有办法进行匹配,函数就会<code>false</code>。</p>
|
||||
<p>检查完之后,函数通过<code>for-in</code>循环和半闭区间操作(..)来迭代<code>someContainer</code>中的所有items。对于每个item,函数检查是否<code>someContainer</code>中的item不等于对应的<code>anotherContainer</code>中的item,如果这两个items不等,则这两个容器不匹配,返回<code>false</code>。</p>
|
||||
<p>如果循环体结束后未发现没有任何的不匹配,那表明两个容器匹配,函数返回true。</p>
|
||||
<p>Here’s how the allItemsMatch function looks in action:
|
||||
这里演示了allItemsMatch函数运算的过程:</p>
|
||||
<pre><code class="lang-c">var stackOfStrings = Stack<String>()
|
||||
stackOfStrings.push(<span class="hljs-string">"uno"</span>)
|
||||
stackOfStrings.push(<span class="hljs-string">"dos"</span>)
|
||||
stackOfStrings.push(<span class="hljs-string">"tres"</span>)
|
||||
|
||||
var arrayOfStrings = [<span class="hljs-string">"uno"</span>, <span class="hljs-string">"dos"</span>, <span class="hljs-string">"tres"</span>]
|
||||
|
||||
<span class="hljs-keyword">if</span> allItemsMatch(stackOfStrings, arrayOfStrings) {
|
||||
println(<span class="hljs-string">"All items match."</span>)
|
||||
} <span class="hljs-keyword">else</span> {
|
||||
println(<span class="hljs-string">"Not all items match."</span>)
|
||||
}
|
||||
<span class="hljs-comment">// prints "All items match."</span>
|
||||
</code></pre>
|
||||
<p> 上面的例子创建一个<code>Stack</code>单例来存储<code>String</code>,然后压了三个字符串进栈。这个例子也创建了一个<code>Array</code>单例,并初始化包含三个同栈里一样的原始字符串。即便栈和数组否是不同的类型,但他们都遵循<code>Container</code>协议,而且他们都包含同样的类型值。你因此可以调用<code>allItemsMatch</code>函数,用这两个容器作为它的参数。在上面的例子中,<code>allItemsMatch</code>函数正确的显示了所有的这两个容器的items匹配。</p>
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -591,8 +941,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.23" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="2.23" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -591,8 +590,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="2" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -576,7 +575,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_30">
|
||||
<section class="normal" id="section-gitbook_36">
|
||||
|
||||
<h1 id="swift-">Swift 教程</h1>
|
||||
<p>本章介绍了 Swift 的各种特性及其使用方法,是全书的核心部分。</p>
|
||||
@ -599,8 +598,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.1" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="3.1" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -576,7 +575,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_31">
|
||||
<section class="normal" id="section-gitbook_37">
|
||||
|
||||
<h1 id="-">关于语言附注</h1>
|
||||
<p>本书的这一节描述了Swift编程语言的形式语法。这里描述的语法是为了帮助您更详细的了解该语言,而不是让您直接实现一个解析器或编译器。</p>
|
||||
@ -624,8 +623,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.2" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="3.2" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -591,8 +590,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.3" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="3.3" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -591,8 +590,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.4" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="3.4" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -591,8 +590,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.5" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="3.5" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -591,8 +590,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.6" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="3.6" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -591,8 +590,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.7" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="3.7" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -591,8 +590,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.8" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="3.8" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -591,8 +590,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -44,7 +44,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.9" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="3.9" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -284,7 +284,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -293,7 +293,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -479,8 +479,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -513,9 +512,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -587,8 +586,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3" data-basepath=".." data-revision="1402008022544">
|
||||
<div class="book" data-level="3" data-basepath=".." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -286,7 +286,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="../chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -481,8 +481,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -515,9 +514,9 @@
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="../chapter2/14_Initialization.html" title="构造函数" class="chapter done " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="../chapter2/15_Deinitialization.html" title="析构函数" class="chapter done " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -591,8 +590,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
<!DOC<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>TYPE html>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>jsREPL Sandbox</title>
|
||||
|
||||
36
index.html
36
index.html
@ -44,7 +44,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="0" data-basepath="." data-revision="1402008022544">
|
||||
<div class="book" data-level="0" data-basepath="." data-revision="1402050704574">
|
||||
<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>
|
||||
@ -284,7 +284,7 @@
|
||||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||||
|
||||
<a href="./chapter2/14_Initialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||||
<i class="fa fa-check"></i> <b>2.14.</b> 构造函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -293,7 +293,7 @@
|
||||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||||
|
||||
<a href="./chapter2/15_Deinitialization.html">
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||||
<i class="fa fa-check"></i> <b>2.15.</b> 析构函数
|
||||
</a>
|
||||
|
||||
|
||||
@ -479,8 +479,7 @@
|
||||
<li>
|
||||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||||
</li>
|
||||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@ -513,9 +512,9 @@
|
||||
|
||||
<a href="./chapter2/13_Inheritance.html" title="继承" class="chapter " data-progress="2.13" style="left: 24.324324324324323%;"></a>
|
||||
|
||||
<a href="./chapter2/14_Initialization.html" title="构造过程" class="chapter " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
<a href="./chapter2/14_Initialization.html" title="构造函数" class="chapter " data-progress="2.14" style="left: 27.027027027027028%;"></a>
|
||||
|
||||
<a href="./chapter2/15_Deinitialization.html" title="析构过程" class="chapter " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
<a href="./chapter2/15_Deinitialization.html" title="析构函数" class="chapter " data-progress="2.15" style="left: 29.72972972972973%;"></a>
|
||||
|
||||
<a href="./chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter " data-progress="2.16" style="left: 32.432432432432435%;"></a>
|
||||
|
||||
@ -574,7 +573,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_3">
|
||||
<section class="normal" id="section-gitbook_7">
|
||||
|
||||
<h1 id="swift-">Swift 编程语言</h1>
|
||||
<p>Swift 是苹果在 WWDC 2014 上发布的一款全新的编程语言,本书译自苹果官方的 Swift 教程《The Swift Programming Language》。</p>
|
||||
@ -596,8 +595,8 @@
|
||||
|
||||
|
||||
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/mode-javascript.js"></script>
|
||||
<script src="gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||||
<script src="gitbook/app.js"></script>
|
||||
|
||||
@ -625,5 +624,20 @@ require(["gitbook"], function(gitbook) {
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
||||
|
||||
>
|
||||
|
||||
<script src="gitbook/plugins/gitbook-plugin-mathjax/plugin.js"></script>
|
||||
|
||||
|
||||
<script>
|
||||
require(["gitbook"], function(gitbook) {
|
||||
var config = {};
|
||||
gitbook.start(config);
|
||||
});
|
||||
</script>
|
||||
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
CACHE MANIFEST
|
||||
# Revision 1402008022545
|
||||
# Revision 1402050704575
|
||||
|
||||
CACHE:
|
||||
index.html
|
||||
|
||||
File diff suppressed because one or more lines are too long
@ -257,7 +257,7 @@
|
||||
|
||||
有很多种创建闭包的方法。如果一个闭包的类型已知,比如作为一个回调函数,你可以忽略参数的类型和返回值。单个语句闭包会把它语句的值当做结果返回。
|
||||
|
||||
你可以获取参数的数量——这个方法在非常短的闭包中很有用。一个被作为最后一个参数传入函数的时候可以直接出现在括号后面。
|
||||
你可以通过参数位置而不是参数名字来引用参数——这个方法在非常短的闭包中非常有用。当一个闭包作为最后一个参数传给一个函数的时候,它可以直接跟在括号后面。
|
||||
|
||||
sort([1, 5, 3, 12, 2]) { $0 > $1 }
|
||||
|
||||
|
||||
@ -301,8 +301,10 @@ Swift 是类型安全的,会在编译你的代码时进行类型检查,如
|
||||
let integerPi = Int(pi)
|
||||
// integerPi 等于 3,所以被推测为 Int 类型
|
||||
|
||||
<<<<<<< HEAD
|
||||
当用这种方式来初始化一个新的整数值时,浮点值会被截断。也就是说`4.75`会变成`4`,`-3.9`会变成`-3`。
|
||||
=======
|
||||
|
||||
> 注意:结合数字类常量和变量不同于结合数字类原始值。原始值`3`可以直接和原始值`0.14159`相加,因为数字原始值本身没有明确的类型。它们的类型只在编译器需要求值的时候被推测。
|
||||
|
||||
======= | ||||