update 02/17 and 02/15
This commit is contained in:
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="1.1" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="1.1" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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 +576,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_595">
|
||||
<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>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="1.2" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="1.2" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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 +576,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_596">
|
||||
<section class="normal" id="section-gitbook_9">
|
||||
|
||||
<h1 id="swift-">Swift 初见</h1>
|
||||
<p>本页内容包括:</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="1" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="1" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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 +576,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_597">
|
||||
<section class="normal" id="section-gitbook_10">
|
||||
|
||||
<h1 id="-swift">欢迎使用 Swift</h1>
|
||||
<p>在本章中您将了解 Swift 的特性和开发历史,并对 Swift 有一个初步的了解。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.1" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="2.1" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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 +576,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_599">
|
||||
<section class="normal" id="section-gitbook_12">
|
||||
|
||||
<h1 id="-">基础部分</h1>
|
||||
<p>Swift 是 iOS 和 OS X 应用开发的一门新语言。然而,如果你有 C 或者 Objective-C 开发经验的话,你会发现 Swift 的很多内容都是你熟悉的。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.2" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="2.2" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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 +576,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_609">
|
||||
<section class="normal" id="section-gitbook_23">
|
||||
|
||||
<h1 id="-">基础运算符</h1>
|
||||
<p>运算符是检查, 改变, 合并值的特殊符号或短语. 例如, 加号 <code>+</code> 把计算两个数的和(如 <code>let i = 1 + 2</code>). 复杂些的运行算包括逻辑与<code>&&</code>(如 <code>if enteredDoorCode && passedRetinaScan</code>), 还有自增运算符 <code>++i</code> 这样让自身加一的便捷运算.</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.3" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="2.3" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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 +576,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_600">
|
||||
<section class="normal" id="section-gitbook_13">
|
||||
|
||||
<h1 id="-strings-and-characters-">字符串和字符 (Strings and Characters)</h1>
|
||||
<p>本页包含内容:</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.4" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="2.4" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.5" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="2.5" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.6" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="2.6" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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 +576,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_603">
|
||||
<section class="normal" id="section-gitbook_16">
|
||||
|
||||
<h1 id="-functions-">函数(Functions)</h1>
|
||||
<p>本页包含内容:</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.7" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="2.7" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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 +576,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_604">
|
||||
<section class="normal" id="section-gitbook_17">
|
||||
|
||||
<h1 id="-">闭包</h1>
|
||||
<p>本页内容包含:</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.8" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="2.8" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.9" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="2.9" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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 +576,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_606">
|
||||
<section class="normal" id="section-gitbook_19">
|
||||
|
||||
<h3 id="-">类和结构体</h3>
|
||||
<p>本页包含内容:</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.10" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="2.10" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.11" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="2.11" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.12" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="2.12" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.13" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="2.13" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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,7 +584,7 @@
|
||||
<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>
|
||||
|
||||
@ -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="1402189909808">
|
||||
<div class="book" data-level="2.14" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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,7 +584,7 @@
|
||||
<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>
|
||||
|
||||
@ -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="1402189909808">
|
||||
<div class="book" data-level="2.15" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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>
|
||||
|
||||
@ -576,12 +576,78 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_25">
|
||||
|
||||
<h1 id="-">析构过程</h1>
|
||||
<p>在一个类的实例被释放之前,析构函数被立即调用。用关键字deinit来标示析构函数,类似于初始化函数用init来标示。析构函数只适用于类类型。</p>
|
||||
<h2 id="-">析构过程原理</h2>
|
||||
<p>Swift会自动释放不再需要的实例以释放资源。如自动引用计数那一章描述,Swift通过自动引用计数(ARC)处理实例的内存管理。通常当你的实例被释放时不需要手动的去清理。但是,当使用自己的资源时,你可能需要进行一些额外的清理。例如,如果创建了一个自定义的类来打开一个文件,并写入一些数据,你可能需要在类实例被释放之前关闭该文件。</p>
|
||||
<p>在类的定义中,每个类最多只能有一个析构函数。析构函数不带任何参数,在写法上不带括号:</p>
|
||||
<pre><code>1 deinit {
|
||||
2 // 执行析构过程
|
||||
3 }
|
||||
</code></pre><p>析构函数是在实例释放发生前一步被自动调用。不允许主动调用自己的析构函数。子类继承了父类的析构函数,并且在子类析构函数实现的最后,父类的析构函数被自动调用。即使子类没有提供自己的析构函数,父类的析构函数也总是被调用。</p>
|
||||
<p>因为直到实例的析构函数被调用时,实例才会被释放,所以析构函数可以访问所有请求实例的属性,并且根据那些属性可以修改它的行为(比如查找一个需要被关闭的文件的名称)。</p>
|
||||
<h2 id="-">析构函数操作</h2>
|
||||
<p>这里是一个析构函数操作的例子。这个例子是一个简单的游戏,定义了两种新类型,Bank和Player。Bank结构体管理一个虚拟货币的流通,在这个流通中Bank永远不可能拥有超过10,000的硬币。在这个游戏中有且只能有一个Bank存在,因此Bank由带有静态属性和静态方法的结构体实现,从而存储和管理其当前的状态。</p>
|
||||
<pre><code>1 struct Bank {
|
||||
2 static var coinsInBank = 10_000
|
||||
3 static func vendCoins(var numberOfCoinsToVend: Int) -> Int {
|
||||
4 numberOfCoinsToVend = min(numberOfCoinsToVend, coinsInBank)
|
||||
5 coinsInBank -= numberOfCoinsToVend
|
||||
6 return numberOfCoinsToVend
|
||||
7 }
|
||||
8 static func receiveCoins(coins: Int) {
|
||||
9 coinsInBank += coins
|
||||
10 }
|
||||
11 }
|
||||
</code></pre><p>Bank根据它的coinsInBank属性来跟踪当前它拥有的硬币数量。银行还提供两个方法—vendCoins和receiveCoins,用来处理硬币的分发和收集。</p>
|
||||
<p>vendCoins方法在bank分发硬币之前检查是否有足够的硬币。如果没有足够多的硬币,bank返回一个比请求时小的数字(如果没有硬币留在bank中就返回0)。vendCoins方法声明numberOfCoinsToVend为一个变量参数,这样就可以在方法体的内部修改数字,而不需要定义一个新的变量。vendCoins方法返回一个整型值,表明了提供的硬币的实际数目。</p>
|
||||
<p>receiveCoins方法只是将bank的硬币存储和接收到的硬币数目相加,再保存回bank。</p>
|
||||
<p>Player类描述了游戏中的一个玩家。每一个player在任何时刻都有一定数量的硬币存储在他们的钱包中。这通过player的coinsInPurse属性来体现:</p>
|
||||
<pre><code> 1 class Player {
|
||||
2 var coinsInPurse: Int
|
||||
3 init(coins: Int) {
|
||||
4 coinsInPurse = Bank.vendCoins(coins)
|
||||
5 }
|
||||
6 func winCoins(coins: Int) {
|
||||
7 coinsInPurse += Bank.vendCoins(coins)
|
||||
8 }
|
||||
9 deinit {
|
||||
10 Bank.receiveCoins(coinsInPurse)
|
||||
11 }
|
||||
12 }
|
||||
</code></pre><p>每个Player实例都由一个指定数目硬币组成的启动额度初始化,这些硬币在bank初始化的过程中得到。如果没有足够的硬币可用,Player实例可能收到比指定数目少的硬币。</p>
|
||||
<p>Player类定义了一个winCoins方法,该方法从bank获取一定数量的硬币,并把它们添加到player的钱包。Player类还实现了一个析构函数,这个析构函数在Player实例释放前一步被调用。这里析构函数只是将player的所有硬币都返回给bank:</p>
|
||||
<pre><code>1 var playerOne: Player? = Player(coins: 100)
|
||||
2 println("A new player has joined the game with \ (playerOne!.coinsInPurse) coins")
|
||||
3 // 输出 "A new player has joined the game with 100 coins"
|
||||
4 println("There are now \(Bank.coinsInBank) coins left in the bank")
|
||||
5 // 输出 "There are now 9900 coins left in the bank"
|
||||
</code></pre><p>一个新的Player实例随着一个100个硬币(如果有)的请求而被创建。这个Player实例存储在一个名为playerOne的可选Player变量中。这里使用一个可选变量,是因为players可以随时离开游戏。设置为可选使得你可以跟踪当前是否有player在游戏中。</p>
|
||||
<p>因为playerOne是可选的,所以由一个感叹号(!)来修饰,每当其winCoins方法被调用时,coinsInPurse属性被访问并打印出它的默认硬币数目。</p>
|
||||
<pre><code>1 playerOne!.winCoins(2_000)
|
||||
2 println("PlayerOne won 2000 coins & now has \ (playerOne!.coinsInPurse) coins")
|
||||
3 // 输出 "PlayerOne won 2000 coins & now has 2100 coins"
|
||||
4 println("The bank now only has \(Bank.coinsInBank) coins left")
|
||||
5 // 输出 "The bank now only has 7900 coins left"
|
||||
</code></pre><p>这里,player已经赢得了2,000硬币。player的钱包现在有2,100硬币,bank只剩余7,900硬币。</p>
|
||||
<pre><code>1 playerOne = nil
|
||||
2 println("PlayerOne has left the game")
|
||||
3 // 输出 "PlayerOne has left the game"
|
||||
4 println("The bank now has \(Bank.coinsInBank) coins")
|
||||
5 // 输出 "The bank now has 10000 coins"
|
||||
</code></pre><p>player现在已经离开了游戏。这表明是要将可选的playerOne变量设置为nil,意思是"没有Player实例"。当这种情况发生的时候,playerOne变量对Player实例的引用被破坏了。没有其它属性或者变量引用Player实例,因此为了清空它占用的内存从而释放它。在这发生前一步,其析构函数被自动调用,其硬币被返回到bank。</p>
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</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>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.16" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="2.16" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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 +576,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_614">
|
||||
<section class="normal" id="section-gitbook_26">
|
||||
|
||||
<h1 id="-">自动引用计数</h1>
|
||||
<p>本页包含内容:</p>
|
||||
@ -608,7 +608,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>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.17" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="2.17" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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 +576,201 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_27">
|
||||
|
||||
<h1 id="optional-chaining">Optional Chaining</h1>
|
||||
<p>可选链(Optional Chaining)是一种可以请求和调用属性、方法及子脚本的过程,它的自判断性体现于请求或调用的目标当前可能为空(<code>nil</code>)。如果自判断的目标有值,那么调用就会成功;相反,如果选择的目标为空(<code>nil</code>),则这种调用将返回空(<code>nil</code>)。多次请求或调用可以被链接在一起形成一个链,如果任何一个节点为空(<code>nil</code>)将导致整个链失效。</p>
|
||||
<blockquote>
|
||||
<p>笔记:
|
||||
<code>Swift</code>的自判断链和Objective-C中的消息为空有些相像,但是<code>Swift</code>可以使用在任意类型中,并且失败与否可以被检测到。</p>
|
||||
</blockquote>
|
||||
<h2 id="-">可选链可替代强制解析</h2>
|
||||
<p>通过在想调用的属性、方法、或子脚本的自判断值(<code>optional value</code>)(非空)后面放一个问号,可以定义一个可选链。这一点很像在自判断值后面放一个声明符号来强制拆得其封包内的值。他们的主要的区别在于当自判断值为空时可选链即刻失败,然而一般的强制解析将会引发运行时错误。</p>
|
||||
<p>为了反映可选链可以调用空(<code>nil</code>),不论你调用的属性、方法、子脚本等返回的值是不是自判断值,它的返回结果都是一个自判断值。你可以利用这个返回值来检测你的可选链是否调用成功,有返回值即成功,返回nil则失败。</p>
|
||||
<p>调用可选链的返回结果与原本的返回结果具有相同的类型,但是原本的返回结果被包装成了一个自判断值,当可选链调用成功时,一个应该返回<code>Int</code>的属性将会返回<code>Int?</code>。</p>
|
||||
<p>下面几段代码将解释可选链和强制解析的不同。</p>
|
||||
<p>首先定义两个类<code>Person</code>和<code>Residence</code>。</p>
|
||||
<pre><code>class Person {
|
||||
var residence: Residence?
|
||||
}
|
||||
|
||||
class Residence {
|
||||
var numberOfRooms = 1
|
||||
}
|
||||
</code></pre><p><code>Residence</code>具有一个<code>Int</code>类型的<code>numberOfRooms</code>,其值为1。<code>Person</code>具有一个自判断<code>residence</code>属性,它的类型是<code>Residence?</code>。</p>
|
||||
<p>如果你创建一个新的Person实例,它的residence属性由于是被定义为自判断型的,此属性将默认初始化为空:</p>
|
||||
<pre><code>let john = Person()
|
||||
</code></pre><p>如果你想使用声明符!强制解析获得这个人<code>residence</code>属性<code>numberOfRooms</code>属性值,将会引发运行时错误,因为这时没有可以供解析的<code>residence</code>值。</p>
|
||||
<pre><code>let roomCount = john.residence!.numberOfRooms
|
||||
// this triggers a runtime error”
|
||||
//将导致运行时错误
|
||||
</code></pre><p>当<code>john.residence</code>不是<code>nil</code>时,会运行通过,且会将<code>roomCount</code> 设置为一个<code>int</code>类型的合理值。然而,如上所述,当<code>residence</code>为空时,这个代码将会导致运行时错误。</p>
|
||||
<p>可选链提供了一种另一种获得numberOfRooms的方法。利用可选链,使用问号来代替原来<code>!</code>的位置:</p>
|
||||
<pre><code>if let roomCount = john.residence?.numberOfRooms {
|
||||
println("John's residence has \(roomCount) room(s).")
|
||||
} else {
|
||||
println("Unable to retrieve the number of rooms.")
|
||||
}
|
||||
// 打印 "Unable to retrieve the number of rooms.
|
||||
</code></pre><p>这告诉Swift来链接自判断<code>residence?</code>属性,如果<code>residence</code>存在则取回<code>numberOfRooms</code>的值。</p>
|
||||
<p>因为这种尝试获得<code>numberOfRooms</code>的操作有可能失败,可选链会返回<code>Int?</code>类型值,或者称作“自判断<code>Int</code>”。当<code>residence</code>是空的时候(上例),选择<code>Int</code>将会为空,因此会出先无法访问<code>numberOfRooms</code>的情况。</p>
|
||||
<p>要注意的是,即使numberOfRooms是非自判断<code>Int</code>(<code>Int?</code>)时这一点也成立。只要是通过可选链的请求就意味着最后<code>numberOfRooms</code>总是返回一个<code>Int?</code>而不是<code>Int</code>。</p>
|
||||
<p>你可以自己定义一个<code>Residence</code>实例给<code>john.residence</code>,这样它就不再为空了:</p>
|
||||
<pre><code>john.residence = Residence()
|
||||
</code></pre><p><code>john.residence</code> 现在有了实际存在的实例而不是nil了。如果你想使用和前面一样的可选链来获得<code>numberOfRoooms</code>,它将返回一个包含默认值1的<code>Int?</code>:</p>
|
||||
<pre><code>if let roomCount = john.residence?.numberOfRooms {
|
||||
println("John's residence has \(roomCount) room(s).")
|
||||
} else {
|
||||
println("Unable to retrieve the number of rooms.")
|
||||
}
|
||||
// 打印 "John's residence has 1 room(s)"。
|
||||
</code></pre><h2 id="-">为可选链定义模型类</h2>
|
||||
<p>你可以使用可选链来多层调用属性,方法,和子脚本。这让你可以利用它们之间的复杂模型来获取更底层的属性,并检查是否可以成功获取此类底层属性。</p>
|
||||
<p>后面的代码定义了四个将在后面使用的模型类,其中包括多层可选链。这些类是由上面的<code>Person</code>和<code>Residence</code>模型通过添加一个<code>Room</code>和一个<code>Address</code>类拓展来。</p>
|
||||
<p><code>Person</code>类定义与之前相同。</p>
|
||||
<pre><code>class Person {
|
||||
var residence: Residence?
|
||||
}
|
||||
</code></pre><p><code>Residence</code>类比之前复杂些。这次,它定义了一个变量 <code>rooms</code>,它被初始化为一个<code>Room[]</code>类型的空数组:</p>
|
||||
<pre><code>class Residence {
|
||||
var rooms = Room[]()
|
||||
var numberOfRooms: Int {
|
||||
return rooms.count
|
||||
}
|
||||
subscript(i: Int) -> Room {
|
||||
return rooms[i]
|
||||
}
|
||||
func printNumberOfRooms() {
|
||||
println("The number of rooms is \(numberOfRooms)")
|
||||
}
|
||||
var address: Address?
|
||||
}
|
||||
</code></pre><p>因为<code>Residence</code>存储了一个<code>Room</code>实例的数组,它的<code>numberOfRooms</code>属性值不是一个固定的存储值,而是通过计算而来的。<code>numberOfRooms</code>属性值是由返回<code>rooms</code>数组的<code>count</code>属性值得到的。</p>
|
||||
<p>为了能快速访问<code>rooms</code>数组,<code>Residence</code>定义了一个只读的子脚本,通过插入数组的元素角标就可以成功调用。如果该角标存在,子脚本则将该元素返回。</p>
|
||||
<p><code>Residence</code>中也提供了一个<code>printNumberOfRooms</code>的方法,即简单的打印房间个数。</p>
|
||||
<p>最后,<code>Residence</code>定义了一个自判断属性叫<code>address</code>(<code>address?</code>)。<code>Address</code>类的属性将在后面定义。
|
||||
用于<code>rooms</code>数组的<code>Room</code>类是一个很简单的类,它只有一个<code>name</code>属性和一个设定<code>room</code>名的初始化器。</p>
|
||||
<pre><code>class Room {
|
||||
let name: String
|
||||
init(name: String) { self.name = name }
|
||||
}
|
||||
</code></pre><p>这个模型中的最终类叫做<code>Address</code>。它有三个自判断属性他们额类型是<code>String?</code>。前面两个自判断属性<code>buildingName</code>和 <code>buildingNumber</code>作为地址的一部分,是定义某个建筑物的两种方式。第三个属性<code>street</code>,用于命名地址的街道名:</p>
|
||||
<pre><code>class Address {
|
||||
var buildingName: String?
|
||||
var buildingNumber: String?
|
||||
var street: String?
|
||||
func buildingIdentifier() -> String? {
|
||||
if buildingName {
|
||||
return buildingName
|
||||
} else if buildingNumber {
|
||||
return buildingNumber
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
</code></pre><p><code>Address</code>类还提供了一个<code>buildingIdentifier</code>的方法,它的返回值类型为<code>String?</code>。这个方法检查<code>buildingName</code>和<code>buildingNumber</code>的属性,如果<code>buildingName</code>有值则将其返回,或者如果<code>buildingNumber</code>有值则将其返回,再或如果没有一个属性有值,返回空。</p>
|
||||
<h2 id="-">通过可选链调用属性</h2>
|
||||
<p>正如上面“ <a href="">可选链可替代强制解析</a>”中所述,你可以利用可选链的自判断值获取属性,并且检查属性是否获取成功。然而,你不能使用可选链为属性赋值。</p>
|
||||
<p>使用上述定义的类来创建一个人实例,并再次尝试后去它的<code>numberOfRooms</code>属性:</p>
|
||||
<pre><code>let john = Person()
|
||||
if let roomCount = john.residence?.numberOfRooms {
|
||||
println("John's residence has \(roomCount) room(s).")
|
||||
} else {
|
||||
println("Unable to retrieve the number of rooms.")
|
||||
}
|
||||
// 打印 "Unable to retrieve the number of rooms。
|
||||
</code></pre><p>由于<code>john.residence</code>是空,所以这个可选链和之前一样失败了,但是没有运行时错误。</p>
|
||||
<h2 id="-">通过可选链调用方法</h2>
|
||||
<p>你可以使用可选链的来调用自判断值的方法并检查方法调用是否成功。即使这个方法没有返回值,你依然可以使用可选链来达成这一目的。</p>
|
||||
<p>Residence的printNumberOfRooms方法会打印numberOfRooms的当前值。方法如下:</p>
|
||||
<pre><code>func printNumberOfRooms(){
|
||||
println(“The number of rooms is \(numberOfRooms)”)
|
||||
}
|
||||
</code></pre><p>这个方法没有返回值。但是,没有返回值类型的函数和方法有一个隐式的返回值类型<code>Void</code>(参见Function Without Return Values)。</p>
|
||||
<p>如果你利用可选链调用此方法,这个方法的返回值类型将是<code>Void?</code>,而不是<code>Void</code>,因为当通过可选链调用方法时返回值总是自判断类型(optional type)。,即使是这个方法本是没有定义返回值,你也可以使用<code>if</code>语句来检查是否能成功调用<code>printNumberOfRooms</code>方法:如果方法通过可选链调用成功,<code>printNumberOfRooms</code>的隐式返回值将会是<code>Void</code>,如果没有成功,将返回<code>nil</code>:</p>
|
||||
<pre><code>if john.residence?.printNumberOfRooms() {
|
||||
println("It was possible to print the number of rooms.")
|
||||
} else {
|
||||
println("It was not possible to print the number of rooms.")
|
||||
}
|
||||
// 打印 "It was not possible to print the number of rooms."。
|
||||
</code></pre><h2 id="-">使用可选链调用子脚本</h2>
|
||||
<p>你可以使用可选链来尝试从子脚本获取值并检查子脚本的调用是否成功,然而,你不能通过可选链来设置子代码。</p>
|
||||
<blockquote>
|
||||
<p>注意:
|
||||
当你使用可选链来获取子脚本的时候,你应该将问号放在子脚本括号的前面而不是后面。可选链的问号一般直接跟在自判断表达语句的后面。</p>
|
||||
</blockquote>
|
||||
<p>下面这个例子用在<code>Residence</code>类中定义的子脚本来获取<code>john.residence</code>数组中第一个房间的名字。因为<code>john.residence</code>现在是<code>nil</code>,子脚本的调用失败了。</p>
|
||||
<pre><code>if let firstRoomName = john.residence?[0].name {
|
||||
println("The first room name is \(firstRoomName).")
|
||||
} else {
|
||||
println("Unable to retrieve the first room name.")
|
||||
}
|
||||
// 打印 "Unable to retrieve the first room name."。
|
||||
</code></pre><p>在子代码调用中可选链的问号直接跟在<code>john.residence</code>的后面,在子脚本括号的前面,因为<code>john.residence</code>是可选链试图获得的自判断值。</p>
|
||||
<p>如果你创建一个<code>Residence</code>实例给<code>john.residence</code>,且在他的<code>rooms</code>数组中有一个或多个<code>Room</code>实例,那么你可以使用可选链通过<code>Residence</code>子脚本来获取在<code>rooms</code>数组中的实例了:</p>
|
||||
<pre><code>let johnsHouse = Residence()
|
||||
johnsHouse.rooms += Room(name: "Living Room")
|
||||
johnsHouse.rooms += Room(name: "Kitchen")
|
||||
john.residence = johnsHouse
|
||||
|
||||
if let firstRoomName = john.residence?[0].name {
|
||||
println("The first room name is \(firstRoomName).")
|
||||
} else {
|
||||
println("Unable to retrieve the first room name.")
|
||||
}
|
||||
// 打印 "The first room name is Living Room."。
|
||||
</code></pre><h2 id="-">连接多层链接</h2>
|
||||
<p>你可以将多层可选链连接在一起,可以掘取模型内更下层的属性方法和子脚本。然而多层可选链不能再添加比已经返回的自判断值更多的层。
|
||||
也就是说:</p>
|
||||
<p>如果你试图获得的类型不是自判断类型,由于使用了可选链它将变成自判断类型。
|
||||
如果你试图获得的类型已经是自判断类型,由于可选链它也不会提高自判断性。</p>
|
||||
<p>因此:</p>
|
||||
<p>如果你试图通过可选链获得<code>Int</code>值,不论使用了多少层链接返回的总是<code>Int?</code>。
|
||||
相似的,如果你试图通过可选链获得<code>Int?</code>值,不论使用了多少层链接返回的总是<code>Int?</code>。</p>
|
||||
<p>下面的例子试图获取<code>john</code>的<code>residence</code>属性里的<code>address</code>的<code>street</code>属性。这里使用了两层可选链来联系<code>residence</code>和<code>address</code>属性,他们两者都是自判断类型:</p>
|
||||
<pre><code>if let johnsStreet = john.residence?.address?.street {
|
||||
println("John's street name is \(johnsStreet).")
|
||||
} else {
|
||||
println("Unable to retrieve the address.")
|
||||
}
|
||||
// 打印 "Unable to retrieve the address.”。
|
||||
</code></pre><p><code>john.residence</code>的值现在包含一个<code>Residence</code>实例,然而<code>john.residence.address</code>现在是<code>nil</code>,因此<code>john.residence?.address?.street</code>调用失败。</p>
|
||||
<p>从上面的例子发现,你试图获得<code>street</code>属性值。这个属性的类型是<code>String?</code>。因此尽管在自判断类型属性前使用了两层可选链,<code>john.residence?.address?.street</code>的返回值类型也是<code>String?</code>。</p>
|
||||
<p>如果你为<code>Address</code>设定一个实例来作为<code>john.residence.address</code>的值,并为<code>address</code>的<code>street</code>属性设定一个实际值,你可以通过多层可选链来得到这个属性值。</p>
|
||||
<pre><code>let johnsAddress = Address()
|
||||
johnsAddress.buildingName = "The Larches"
|
||||
johnsAddress.street = "Laurel Street"
|
||||
john.residence!.address = johnsAddress
|
||||
|
||||
if let johnsStreet = john.residence?.address?.street {
|
||||
println("John's street name is \(johnsStreet).")
|
||||
} else {
|
||||
println("Unable to retrieve the address.")
|
||||
}
|
||||
// 打印 "John's street name is Laurel Street."。
|
||||
</code></pre><p>值得注意的是,“<code>!</code>”符的在定义<code>address</code>实例时的使用(<code>john.residence.address</code>)。<code>john.residence</code>属性是一个自判断类型,因此你需要在它获取<code>address</code>属性之前使用<code>!</code>解析以获得它的实际值。</p>
|
||||
<h2 id="-">链接自判断返回值的方法</h2>
|
||||
<p>前面的例子解释了如何通过可选链来获得自判断类型属性值。你也可以通过调用返回自判断类型值的方法并按需链接方法的返回值。</p>
|
||||
<p>下面的例子通过可选链调用了<code>Address</code>类中的<code>buildingIdentifier</code> 方法。这个方法的返回值类型是<code>String?</code>。如上所述,这个方法在可选链调用后最终的返回值类型依然是<code>String?</code>:</p>
|
||||
<pre><code>if let buildingIdentifier = john.residence?.address?.buildingIdentifier() {
|
||||
println("John's building identifier is \(buildingIdentifier).")
|
||||
}
|
||||
// 打印 "John's building identifier is The Larches."。
|
||||
</code></pre><p>如果你还想进一步对方法返回值执行可选链,将可选链问号符放在方法括号的后面:</p>
|
||||
<pre><code>if let upper = john.residence?.address?.buildingIdentifier()?.uppercaseString {
|
||||
println("John's uppercase building identifier is \(upper).")
|
||||
}
|
||||
// 打印 "John's uppercase building identifier is THE LARCHES."。
|
||||
</code></pre><blockquote>
|
||||
<p>注意:
|
||||
在上面的例子中,你将可选链问号符放在括号后面是因为你想要链接的自判断值是<code>buildingIdentifier</code>方法的返回值,不是<code>buildingIdentifier</code>方法本身。</p>
|
||||
</blockquote>
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.18" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="2.18" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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 +576,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_615">
|
||||
<section class="normal" id="section-gitbook_28">
|
||||
|
||||
<h1 id="-type-casting-">类型检查(Type Casting)</h1>
|
||||
<p>(ps:为了方便各位检验所以保留了英文,可删。)
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.19" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="2.19" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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 +576,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_616">
|
||||
<section class="normal" id="section-gitbook_29">
|
||||
|
||||
<h1 id="-">类型嵌套</h1>
|
||||
<p>本页包含内容:</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.20" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="2.20" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.21" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="2.21" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.22" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="2.22" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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 +576,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_619">
|
||||
<section class="normal" id="section-gitbook_32">
|
||||
|
||||
<h1 id="-">泛型</h1>
|
||||
<hr>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.23" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="2.23" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="2" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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 +576,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_623">
|
||||
<section class="normal" id="section-gitbook_36">
|
||||
|
||||
<h1 id="swift-">Swift 教程</h1>
|
||||
<p>本章介绍了 Swift 的各种特性及其使用方法,是全书的核心部分。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.1" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="3.1" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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 +576,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_624">
|
||||
<section class="normal" id="section-gitbook_37">
|
||||
|
||||
<h1 id="-">关于语言附注</h1>
|
||||
<p>本书的这一节描述了Swift编程语言的形式语法。这里描述的语法是为了帮助您更详细的了解该语言,而不是让您直接实现一个解析器或编译器。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.2" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="3.2" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.3" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="3.3" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.4" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="3.4" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.5" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="3.5" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.6" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="3.6" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.7" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="3.7" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.8" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="3.8" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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>
|
||||
|
||||
|
||||
@ -44,7 +44,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.9" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="3.9" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -513,9 +513,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>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3" data-basepath=".." data-revision="1402189909808">
|
||||
<div class="book" data-level="3" data-basepath=".." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -515,9 +515,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>
|
||||
|
||||
|
||||
12
index.html
12
index.html
@ -44,7 +44,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="0" data-basepath="." data-revision="1402189909808">
|
||||
<div class="book" data-level="0" data-basepath="." data-revision="1402202224392">
|
||||
<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>
|
||||
|
||||
|
||||
@ -513,9 +513,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 +574,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_594">
|
||||
<section class="normal" id="section-gitbook_7">
|
||||
|
||||
<h1 id="swift-">Swift 编程语言</h1>
|
||||
<p>Swift 是苹果在 WWDC 2014 上发布的一款全新的编程语言,本书译自苹果官方的 Swift 教程《The Swift Programming Language》。</p>
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
CACHE MANIFEST
|
||||
# Revision 1402189909809
|
||||
# Revision 1402202224393
|
||||
|
||||
CACHE:
|
||||
index.html
|
||||
@ -15,14 +15,14 @@ chapter2/06_Functions.html
|
||||
chapter2/07_Closures.html
|
||||
chapter2/08_Enumerations.html
|
||||
chapter2/09_Classes_and_Structures.html
|
||||
chapter2/10_Properties.html
|
||||
chapter2/11_Methods.html
|
||||
chapter2/12_Subscripts.html
|
||||
chapter2/02_Basic_Operators.html
|
||||
chapter2/10_Properties.html
|
||||
chapter2/14_Initialization.html
|
||||
chapter2/15_Deinitialization.html
|
||||
chapter2/17_Optional_Chaining.html
|
||||
chapter2/16_Automatic_Reference_Counting.html
|
||||
chapter2/17_Optional_Chaining.html
|
||||
chapter2/18_Type_Casting.html
|
||||
chapter2/19_Nested_Types.html
|
||||
chapter2/20_Extensions.html
|
||||
|
||||
File diff suppressed because one or more lines are too long
@ -17,8 +17,8 @@
|
||||
* [方法](chapter2/11_Methods.md)
|
||||
* [下标](chapter2/12_Subscripts.md)
|
||||
* [继承](chapter2/13_Inheritance.md)
|
||||
* [构造函数](chapter2/14_Initialization.md)
|
||||
* [析构函数](chapter2/15_Deinitialization.md)
|
||||
* [构造过程](chapter2/14_Initialization.md)
|
||||
* [析构过程](chapter2/15_Deinitialization.md)
|
||||
* [自动引用计数](chapter2/16_Automatic_Reference_Counting.md)
|
||||
* [可选链](chapter2/17_Optional_Chaining.md)
|
||||
* [类型检查](chapter2/18_Type_Casting.md)
|
||||
|
||||
@ -0,0 +1,88 @@
|
||||
#析构过程
|
||||
|
||||
在一个类的实例被释放之前,析构函数被立即调用。用关键字deinit来标示析构函数,类似于初始化函数用init来标示。析构函数只适用于类类型。
|
||||
|
||||
|
||||
##析构过程原理
|
||||
|
||||
Swift会自动释放不再需要的实例以释放资源。如自动引用计数那一章描述,Swift通过自动引用计数(ARC)处理实例的内存管理。通常当你的实例被释放时不需要手动的去清理。但是,当使用自己的资源时,你可能需要进行一些额外的清理。例如,如果创建了一个自定义的类来打开一个文件,并写入一些数据,你可能需要在类实例被释放之前关闭该文件。
|
||||
|
||||
在类的定义中,每个类最多只能有一个析构函数。析构函数不带任何参数,在写法上不带括号:
|
||||
|
||||
1 deinit {
|
||||
2 // 执行析构过程
|
||||
3 }
|
||||
|
||||
析构函数是在实例释放发生前一步被自动调用。不允许主动调用自己的析构函数。子类继承了父类的析构函数,并且在子类析构函数实现的最后,父类的析构函数被自动调用。即使子类没有提供自己的析构函数,父类的析构函数也总是被调用。
|
||||
|
||||
因为直到实例的析构函数被调用时,实例才会被释放,所以析构函数可以访问所有请求实例的属性,并且根据那些属性可以修改它的行为(比如查找一个需要被关闭的文件的名称)。
|
||||
|
||||
##析构函数操作
|
||||
|
||||
这里是一个析构函数操作的例子。这个例子是一个简单的游戏,定义了两种新类型,Bank和Player。Bank结构体管理一个虚拟货币的流通,在这个流通中Bank永远不可能拥有超过10,000的硬币。在这个游戏中有且只能有一个Bank存在,因此Bank由带有静态属性和静态方法的结构体实现,从而存储和管理其当前的状态。
|
||||
|
||||
1 struct Bank {
|
||||
2 static var coinsInBank = 10_000
|
||||
3 static func vendCoins(var numberOfCoinsToVend: Int) -> Int {
|
||||
4 numberOfCoinsToVend = min(numberOfCoinsToVend, coinsInBank)
|
||||
5 coinsInBank -= numberOfCoinsToVend
|
||||
6 return numberOfCoinsToVend
|
||||
7 }
|
||||
8 static func receiveCoins(coins: Int) {
|
||||
9 coinsInBank += coins
|
||||
10 }
|
||||
11 }
|
||||
|
||||
Bank根据它的coinsInBank属性来跟踪当前它拥有的硬币数量。银行还提供两个方法—vendCoins和receiveCoins,用来处理硬币的分发和收集。
|
||||
|
||||
vendCoins方法在bank分发硬币之前检查是否有足够的硬币。如果没有足够多的硬币,bank返回一个比请求时小的数字(如果没有硬币留在bank中就返回0)。vendCoins方法声明numberOfCoinsToVend为一个变量参数,这样就可以在方法体的内部修改数字,而不需要定义一个新的变量。vendCoins方法返回一个整型值,表明了提供的硬币的实际数目。
|
||||
|
||||
receiveCoins方法只是将bank的硬币存储和接收到的硬币数目相加,再保存回bank。
|
||||
|
||||
Player类描述了游戏中的一个玩家。每一个player在任何时刻都有一定数量的硬币存储在他们的钱包中。这通过player的coinsInPurse属性来体现:
|
||||
|
||||
1 class Player {
|
||||
2 var coinsInPurse: Int
|
||||
3 init(coins: Int) {
|
||||
4 coinsInPurse = Bank.vendCoins(coins)
|
||||
5 }
|
||||
6 func winCoins(coins: Int) {
|
||||
7 coinsInPurse += Bank.vendCoins(coins)
|
||||
8 }
|
||||
9 deinit {
|
||||
10 Bank.receiveCoins(coinsInPurse)
|
||||
11 }
|
||||
12 }
|
||||
|
||||
|
||||
每个Player实例都由一个指定数目硬币组成的启动额度初始化,这些硬币在bank初始化的过程中得到。如果没有足够的硬币可用,Player实例可能收到比指定数目少的硬币。
|
||||
|
||||
Player类定义了一个winCoins方法,该方法从bank获取一定数量的硬币,并把它们添加到player的钱包。Player类还实现了一个析构函数,这个析构函数在Player实例释放前一步被调用。这里析构函数只是将player的所有硬币都返回给bank:
|
||||
|
||||
1 var playerOne: Player? = Player(coins: 100)
|
||||
2 println("A new player has joined the game with \ (playerOne!.coinsInPurse) coins")
|
||||
3 // 输出 "A new player has joined the game with 100 coins"
|
||||
4 println("There are now \(Bank.coinsInBank) coins left in the bank")
|
||||
5 // 输出 "There are now 9900 coins left in the bank"
|
||||
|
||||
一个新的Player实例随着一个100个硬币(如果有)的请求而被创建。这个Player实例存储在一个名为playerOne的可选Player变量中。这里使用一个可选变量,是因为players可以随时离开游戏。设置为可选使得你可以跟踪当前是否有player在游戏中。
|
||||
|
||||
因为playerOne是可选的,所以由一个感叹号(!)来修饰,每当其winCoins方法被调用时,coinsInPurse属性被访问并打印出它的默认硬币数目。
|
||||
|
||||
1 playerOne!.winCoins(2_000)
|
||||
2 println("PlayerOne won 2000 coins & now has \ (playerOne!.coinsInPurse) coins")
|
||||
3 // 输出 "PlayerOne won 2000 coins & now has 2100 coins"
|
||||
4 println("The bank now only has \(Bank.coinsInBank) coins left")
|
||||
5 // 输出 "The bank now only has 7900 coins left"
|
||||
|
||||
这里,player已经赢得了2,000硬币。player的钱包现在有2,100硬币,bank只剩余7,900硬币。
|
||||
|
||||
1 playerOne = nil
|
||||
2 println("PlayerOne has left the game")
|
||||
3 // 输出 "PlayerOne has left the game"
|
||||
4 println("The bank now has \(Bank.coinsInBank) coins")
|
||||
5 // 输出 "The bank now has 10000 coins"
|
||||
|
||||
player现在已经离开了游戏。这表明是要将可选的playerOne变量设置为nil,意思是"没有Player实例"。当这种情况发生的时候,playerOne变量对Player实例的引用被破坏了。没有其它属性或者变量引用Player实例,因此为了清空它占用的内存从而释放它。在这发生前一步,其析构函数被自动调用,其硬币被返回到bank。
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,270 @@
|
||||
#Optional Chaining
|
||||
|
||||
可选链(Optional Chaining)是一种可以请求和调用属性、方法及子脚本的过程,它的自判断性体现于请求或调用的目标当前可能为空(`nil`)。如果自判断的目标有值,那么调用就会成功;相反,如果选择的目标为空(`nil`),则这种调用将返回空(`nil`)。多次请求或调用可以被链接在一起形成一个链,如果任何一个节点为空(`nil`)将导致整个链失效。
|
||||
|
||||
> 笔记:
|
||||
`Swift`的自判断链和Objective-C中的消息为空有些相像,但是`Swift`可以使用在任意类型中,并且失败与否可以被检测到。
|
||||
|
||||
## 可选链可替代强制解析
|
||||
|
||||
通过在想调用的属性、方法、或子脚本的自判断值(`optional value`)(非空)后面放一个问号,可以定义一个可选链。这一点很像在自判断值后面放一个声明符号来强制拆得其封包内的值。他们的主要的区别在于当自判断值为空时可选链即刻失败,然而一般的强制解析将会引发运行时错误。
|
||||
|
||||
为了反映可选链可以调用空(`nil`),不论你调用的属性、方法、子脚本等返回的值是不是自判断值,它的返回结果都是一个自判断值。你可以利用这个返回值来检测你的可选链是否调用成功,有返回值即成功,返回nil则失败。
|
||||
|
||||
调用可选链的返回结果与原本的返回结果具有相同的类型,但是原本的返回结果被包装成了一个自判断值,当可选链调用成功时,一个应该返回`Int`的属性将会返回`Int?`。
|
||||
|
||||
下面几段代码将解释可选链和强制解析的不同。
|
||||
|
||||
首先定义两个类`Person`和`Residence`。
|
||||
|
||||
class Person {
|
||||
var residence: Residence?
|
||||
}
|
||||
|
||||
class Residence {
|
||||
var numberOfRooms = 1
|
||||
}
|
||||
|
||||
`Residence`具有一个`Int`类型的`numberOfRooms`,其值为1。`Person`具有一个自判断`residence`属性,它的类型是`Residence?`。
|
||||
|
||||
如果你创建一个新的Person实例,它的residence属性由于是被定义为自判断型的,此属性将默认初始化为空:
|
||||
|
||||
let john = Person()
|
||||
|
||||
如果你想使用声明符!强制解析获得这个人`residence`属性`numberOfRooms`属性值,将会引发运行时错误,因为这时没有可以供解析的`residence`值。
|
||||
|
||||
let roomCount = john.residence!.numberOfRooms
|
||||
// this triggers a runtime error”
|
||||
//将导致运行时错误
|
||||
当`john.residence`不是`nil`时,会运行通过,且会将`roomCount` 设置为一个`int`类型的合理值。然而,如上所述,当`residence`为空时,这个代码将会导致运行时错误。
|
||||
|
||||
可选链提供了一种另一种获得numberOfRooms的方法。利用可选链,使用问号来代替原来`!`的位置:
|
||||
|
||||
if let roomCount = john.residence?.numberOfRooms {
|
||||
println("John's residence has \(roomCount) room(s).")
|
||||
} else {
|
||||
println("Unable to retrieve the number of rooms.")
|
||||
}
|
||||
// 打印 "Unable to retrieve the number of rooms.
|
||||
|
||||
|
||||
这告诉Swift来链接自判断`residence?`属性,如果`residence`存在则取回`numberOfRooms`的值。
|
||||
|
||||
因为这种尝试获得`numberOfRooms`的操作有可能失败,可选链会返回`Int?`类型值,或者称作“自判断`Int`”。当`residence`是空的时候(上例),选择`Int`将会为空,因此会出先无法访问`numberOfRooms`的情况。
|
||||
|
||||
要注意的是,即使numberOfRooms是非自判断`Int`(`Int?`)时这一点也成立。只要是通过可选链的请求就意味着最后`numberOfRooms`总是返回一个`Int?`而不是`Int`。
|
||||
|
||||
你可以自己定义一个`Residence`实例给`john.residence`,这样它就不再为空了:
|
||||
|
||||
john.residence = Residence()
|
||||
|
||||
`john.residence` 现在有了实际存在的实例而不是nil了。如果你想使用和前面一样的可选链来获得`numberOfRoooms`,它将返回一个包含默认值1的`Int?`:
|
||||
|
||||
if let roomCount = john.residence?.numberOfRooms {
|
||||
println("John's residence has \(roomCount) room(s).")
|
||||
} else {
|
||||
println("Unable to retrieve the number of rooms.")
|
||||
}
|
||||
// 打印 "John's residence has 1 room(s)"。
|
||||
|
||||
|
||||
##为可选链定义模型类
|
||||
|
||||
你可以使用可选链来多层调用属性,方法,和子脚本。这让你可以利用它们之间的复杂模型来获取更底层的属性,并检查是否可以成功获取此类底层属性。
|
||||
|
||||
后面的代码定义了四个将在后面使用的模型类,其中包括多层可选链。这些类是由上面的`Person`和`Residence`模型通过添加一个`Room`和一个`Address`类拓展来。
|
||||
|
||||
`Person`类定义与之前相同。
|
||||
|
||||
class Person {
|
||||
var residence: Residence?
|
||||
}
|
||||
|
||||
`Residence`类比之前复杂些。这次,它定义了一个变量 `rooms`,它被初始化为一个`Room[]`类型的空数组:
|
||||
|
||||
class Residence {
|
||||
var rooms = Room[]()
|
||||
var numberOfRooms: Int {
|
||||
return rooms.count
|
||||
}
|
||||
subscript(i: Int) -> Room {
|
||||
return rooms[i]
|
||||
}
|
||||
func printNumberOfRooms() {
|
||||
println("The number of rooms is \(numberOfRooms)")
|
||||
}
|
||||
var address: Address?
|
||||
}
|
||||
|
||||
因为`Residence`存储了一个`Room`实例的数组,它的`numberOfRooms`属性值不是一个固定的存储值,而是通过计算而来的。`numberOfRooms`属性值是由返回`rooms`数组的`count`属性值得到的。
|
||||
|
||||
为了能快速访问`rooms`数组,`Residence`定义了一个只读的子脚本,通过插入数组的元素角标就可以成功调用。如果该角标存在,子脚本则将该元素返回。
|
||||
|
||||
`Residence`中也提供了一个`printNumberOfRooms`的方法,即简单的打印房间个数。
|
||||
|
||||
最后,`Residence`定义了一个自判断属性叫`address`(`address?`)。`Address`类的属性将在后面定义。
|
||||
用于`rooms`数组的`Room`类是一个很简单的类,它只有一个`name`属性和一个设定`room`名的初始化器。
|
||||
|
||||
class Room {
|
||||
let name: String
|
||||
init(name: String) { self.name = name }
|
||||
}
|
||||
|
||||
|
||||
这个模型中的最终类叫做`Address`。它有三个自判断属性他们额类型是`String?`。前面两个自判断属性`buildingName`和 `buildingNumber`作为地址的一部分,是定义某个建筑物的两种方式。第三个属性`street`,用于命名地址的街道名:
|
||||
|
||||
class Address {
|
||||
var buildingName: String?
|
||||
var buildingNumber: String?
|
||||
var street: String?
|
||||
func buildingIdentifier() -> String? {
|
||||
if buildingName {
|
||||
return buildingName
|
||||
} else if buildingNumber {
|
||||
return buildingNumber
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
`Address`类还提供了一个`buildingIdentifier`的方法,它的返回值类型为`String?`。这个方法检查`buildingName`和`buildingNumber`的属性,如果`buildingName`有值则将其返回,或者如果`buildingNumber`有值则将其返回,再或如果没有一个属性有值,返回空。
|
||||
|
||||
##通过可选链调用属性
|
||||
|
||||
正如上面“ [可选链可替代强制解析]()”中所述,你可以利用可选链的自判断值获取属性,并且检查属性是否获取成功。然而,你不能使用可选链为属性赋值。
|
||||
|
||||
使用上述定义的类来创建一个人实例,并再次尝试后去它的`numberOfRooms`属性:
|
||||
|
||||
let john = Person()
|
||||
if let roomCount = john.residence?.numberOfRooms {
|
||||
println("John's residence has \(roomCount) room(s).")
|
||||
} else {
|
||||
println("Unable to retrieve the number of rooms.")
|
||||
}
|
||||
// 打印 "Unable to retrieve the number of rooms。
|
||||
|
||||
由于`john.residence`是空,所以这个可选链和之前一样失败了,但是没有运行时错误。
|
||||
|
||||
##通过可选链调用方法
|
||||
|
||||
你可以使用可选链的来调用自判断值的方法并检查方法调用是否成功。即使这个方法没有返回值,你依然可以使用可选链来达成这一目的。
|
||||
|
||||
Residence的printNumberOfRooms方法会打印numberOfRooms的当前值。方法如下:
|
||||
|
||||
func printNumberOfRooms(){
|
||||
println(“The number of rooms is \(numberOfRooms)”)
|
||||
}
|
||||
|
||||
这个方法没有返回值。但是,没有返回值类型的函数和方法有一个隐式的返回值类型`Void`(参见Function Without Return Values)。
|
||||
|
||||
如果你利用可选链调用此方法,这个方法的返回值类型将是`Void?`,而不是`Void`,因为当通过可选链调用方法时返回值总是自判断类型(optional type)。,即使是这个方法本是没有定义返回值,你也可以使用`if`语句来检查是否能成功调用`printNumberOfRooms`方法:如果方法通过可选链调用成功,`printNumberOfRooms`的隐式返回值将会是`Void`,如果没有成功,将返回`nil`:
|
||||
|
||||
if john.residence?.printNumberOfRooms() {
|
||||
println("It was possible to print the number of rooms.")
|
||||
} else {
|
||||
println("It was not possible to print the number of rooms.")
|
||||
}
|
||||
// 打印 "It was not possible to print the number of rooms."。
|
||||
|
||||
##使用可选链调用子脚本
|
||||
|
||||
你可以使用可选链来尝试从子脚本获取值并检查子脚本的调用是否成功,然而,你不能通过可选链来设置子代码。
|
||||
|
||||
> 注意:
|
||||
当你使用可选链来获取子脚本的时候,你应该将问号放在子脚本括号的前面而不是后面。可选链的问号一般直接跟在自判断表达语句的后面。
|
||||
|
||||
下面这个例子用在`Residence`类中定义的子脚本来获取`john.residence`数组中第一个房间的名字。因为`john.residence`现在是`nil`,子脚本的调用失败了。
|
||||
|
||||
if let firstRoomName = john.residence?[0].name {
|
||||
println("The first room name is \(firstRoomName).")
|
||||
} else {
|
||||
println("Unable to retrieve the first room name.")
|
||||
}
|
||||
// 打印 "Unable to retrieve the first room name."。
|
||||
|
||||
在子代码调用中可选链的问号直接跟在`john.residence`的后面,在子脚本括号的前面,因为`john.residence`是可选链试图获得的自判断值。
|
||||
|
||||
如果你创建一个`Residence`实例给`john.residence`,且在他的`rooms`数组中有一个或多个`Room`实例,那么你可以使用可选链通过`Residence`子脚本来获取在`rooms`数组中的实例了:
|
||||
|
||||
let johnsHouse = Residence()
|
||||
johnsHouse.rooms += Room(name: "Living Room")
|
||||
johnsHouse.rooms += Room(name: "Kitchen")
|
||||
john.residence = johnsHouse
|
||||
|
||||
if let firstRoomName = john.residence?[0].name {
|
||||
println("The first room name is \(firstRoomName).")
|
||||
} else {
|
||||
println("Unable to retrieve the first room name.")
|
||||
}
|
||||
// 打印 "The first room name is Living Room."。
|
||||
|
||||
##连接多层链接
|
||||
|
||||
你可以将多层可选链连接在一起,可以掘取模型内更下层的属性方法和子脚本。然而多层可选链不能再添加比已经返回的自判断值更多的层。
|
||||
也就是说:
|
||||
|
||||
如果你试图获得的类型不是自判断类型,由于使用了可选链它将变成自判断类型。
|
||||
如果你试图获得的类型已经是自判断类型,由于可选链它也不会提高自判断性。
|
||||
|
||||
因此:
|
||||
|
||||
如果你试图通过可选链获得`Int`值,不论使用了多少层链接返回的总是`Int?`。
|
||||
相似的,如果你试图通过可选链获得`Int?`值,不论使用了多少层链接返回的总是`Int?`。
|
||||
|
||||
下面的例子试图获取`john`的`residence`属性里的`address`的`street`属性。这里使用了两层可选链来联系`residence`和`address`属性,他们两者都是自判断类型:
|
||||
|
||||
if let johnsStreet = john.residence?.address?.street {
|
||||
println("John's street name is \(johnsStreet).")
|
||||
} else {
|
||||
println("Unable to retrieve the address.")
|
||||
}
|
||||
// 打印 "Unable to retrieve the address.”。
|
||||
|
||||
`john.residence`的值现在包含一个`Residence`实例,然而`john.residence.address`现在是`nil`,因此`john.residence?.address?.street`调用失败。
|
||||
|
||||
从上面的例子发现,你试图获得`street`属性值。这个属性的类型是`String?`。因此尽管在自判断类型属性前使用了两层可选链,`john.residence?.address?.street`的返回值类型也是`String?`。
|
||||
|
||||
如果你为`Address`设定一个实例来作为`john.residence.address`的值,并为`address`的`street`属性设定一个实际值,你可以通过多层可选链来得到这个属性值。
|
||||
|
||||
let johnsAddress = Address()
|
||||
johnsAddress.buildingName = "The Larches"
|
||||
johnsAddress.street = "Laurel Street"
|
||||
john.residence!.address = johnsAddress
|
||||
|
||||
if let johnsStreet = john.residence?.address?.street {
|
||||
println("John's street name is \(johnsStreet).")
|
||||
} else {
|
||||
println("Unable to retrieve the address.")
|
||||
}
|
||||
// 打印 "John's street name is Laurel Street."。
|
||||
|
||||
值得注意的是,“`!`”符的在定义`address`实例时的使用(`john.residence.address`)。`john.residence`属性是一个自判断类型,因此你需要在它获取`address`属性之前使用`!`解析以获得它的实际值。
|
||||
|
||||
##链接自判断返回值的方法
|
||||
|
||||
前面的例子解释了如何通过可选链来获得自判断类型属性值。你也可以通过调用返回自判断类型值的方法并按需链接方法的返回值。
|
||||
|
||||
下面的例子通过可选链调用了`Address`类中的`buildingIdentifier` 方法。这个方法的返回值类型是`String?`。如上所述,这个方法在可选链调用后最终的返回值类型依然是`String?`:
|
||||
|
||||
if let buildingIdentifier = john.residence?.address?.buildingIdentifier() {
|
||||
println("John's building identifier is \(buildingIdentifier).")
|
||||
}
|
||||
// 打印 "John's building identifier is The Larches."。
|
||||
|
||||
如果你还想进一步对方法返回值执行可选链,将可选链问号符放在方法括号的后面:
|
||||
|
||||
if let upper = john.residence?.address?.buildingIdentifier()?.uppercaseString {
|
||||
println("John's uppercase building identifier is \(upper).")
|
||||
}
|
||||
// 打印 "John's uppercase building identifier is THE LARCHES."。
|
||||
|
||||
> 注意:
|
||||
在上面的例子中,你将可选链问号符放在括号后面是因为你想要链接的自判断值是`buildingIdentifier`方法的返回值,不是`buildingIdentifier`方法本身。
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user