fix some terms
This commit is contained in:
14
README.md
14
README.md
@ -23,19 +23,19 @@
|
||||
* 基础部分(完成 By numbbbbb, lyuka, JaySurplus)
|
||||
* 基本操作符(完成 By @xielingwang)
|
||||
* 字符串和字符(完成 By @wh1100717)
|
||||
* 集合类型(@WilliamZang 认领)
|
||||
* 集合类型(认领)
|
||||
* 控制流(@vclwei 认领)
|
||||
* 函数(完成 By @honghaoz)
|
||||
* 闭包(完成 By @wh1100717)
|
||||
* 枚举(@yankuangshi 认领)
|
||||
* 枚举(完成 By @yankuangshi)
|
||||
* 类和结构体(@JaySurplus 认领)
|
||||
* 属性(@shinyzhu 认领)
|
||||
* 方法(@pp-prog 认领)
|
||||
* 下标(@siemenliu 认领)
|
||||
* 方法(完成 By @pp-prog)
|
||||
* 下标(完成 By @siemenliu)
|
||||
* 继承(完成 By @Hawstein)
|
||||
* 构造过程(@lifedim 认领)
|
||||
* 析构过程(完成)
|
||||
* 自动引用计数(@TimothyYe 认领)
|
||||
* 自动引用计数(完成 By @TimothyYe)
|
||||
* 可选链(完成)
|
||||
* 类型检查(完成 By @xiehurricane)
|
||||
* 嵌套类型(完成 By @Lin-H)
|
||||
@ -48,9 +48,9 @@
|
||||
* 词法结构(@superkam 认领)
|
||||
* 类型(@lyuka 认领)
|
||||
* 表达式(@sg552 认领)
|
||||
* 语句(@coverxit 认领)
|
||||
* 语句(完成 By @coverxit)
|
||||
* 声明(@marsprince 认领)
|
||||
* 特性(@Hawstein 认领)
|
||||
* 特性(完成 By @Hawstein)
|
||||
* 模式(@honghaoz 认领)
|
||||
* 泛型参数(@fd5788 认领)
|
||||
* 语法总结(@StanZhai 认领)
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="1.1" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="1.1" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
@ -587,10 +587,10 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_5">
|
||||
<section class="normal" id="section-gitbook_118">
|
||||
|
||||
<h1 id="-swift">关于 Swift</h1>
|
||||
<p>Swift 是一种新的编程语言,用于编写 iOS 和 OS X 应用程序。Swift 结合了 C 和 Objective-C 的优点并且不受C的兼容性的限制。Swift 使用安全的编程模式并添加了很多新特性,这将使编程更简单,扩展性更强,也更有趣。除此之外,Swift 还支持人见人爱的 Cocoa 和 Cocoa Touch 框架。拥有了这些特性,Swift将重新定义软件开发。</p>
|
||||
<p>Swift 是一种新的编程语言,用于编写 iOS 和 OS X 应用。Swift 结合了 C 和 Objective-C 的优点并且不受C的兼容性的限制。Swift 使用安全的编程模式并添加了很多新特性,这将使编程更简单,扩展性更强,也更有趣。除此之外,Swift 还支持人见人爱的 Cocoa 和 Cocoa Touch 框架。拥有了这些特性,Swift将重新定义软件开发。</p>
|
||||
<p>Swift 的开发从很久之前就开始了。为了给 Swift 打好基础,苹果公司改进了编译器,调试器和框架结构。我们使用自动引用计数(Automatic Reference Counting, ARC)来简化内存管理。我们在 Foundation 和 Cocoa的基础上构建框架栈并将其标准化。Objective-C 本身支持块、集合语法和模块,所以框架可以轻松支持现代编程语言技术。得益于这些基础工作,我们现在可以发布一个新语言,用于未来的苹果软件的开发。</p>
|
||||
<p>Objective-C 开发者对于 Swift 并不会感到陌生。它采用了 Objective-C 的命名参数以及动态对象模型,可以无缝对接到现有的 Cocoa 框架,并且可以兼容 Objective-C 代码。在此基础之上,Swift 还有许多新特性并且支持过程式编程和面向对象编程。</p>
|
||||
<p>Swift 对于初学者来说也很友好。它是第一个既满足工业标准又像脚本语言一样充满表现力和趣味的编程语言。它支持代码预览,这个革命性的特性可以允许程序员在不编译和运行应用程序的前提下运行 Swift 代码并实时查看结果。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="1.2" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="1.2" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_6">
|
||||
<section class="normal" id="section-gitbook_119">
|
||||
|
||||
<h1 id="swift-">Swift 初见</h1>
|
||||
<p>本页内容包括:</p>
|
||||
@ -601,7 +601,7 @@
|
||||
<li>泛型</li>
|
||||
</ul>
|
||||
<p>通常来说,编程语言教程中的第一个程序应该在屏幕上打印“Hello, world”。在 Swift 中,可以用一行代码实现:</p>
|
||||
<pre><code>println("hello, world")
|
||||
<pre><code>println("Hello, world")
|
||||
</code></pre><p>如果你写过 C 或者 Objective-C 代码,那你应该很熟悉这种形式——在 Swift 中,这行代码就是一个完整的程序。你不需要为了输入输出或者字符串处理导入一个单独的库。全局作用域中的代码会被自动当做程序的入口点,所以你也不需要<code>main</code>函数。你同样不需要在每个语句结尾写上分号。</p>
|
||||
<p>这个教程会通过一系列编程例子来让你对 Swift 有初步了解,如果你有什么不理解的地方也不用担心——任何本章介绍的内容都会在后面的章节中详细讲解。</p>
|
||||
<blockquote>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="1" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="1" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_7">
|
||||
<section class="normal" id="section-gitbook_120">
|
||||
|
||||
<h1 id="-swift">欢迎使用 Swift</h1>
|
||||
<p>在本章中您将了解 Swift 的特性和开发历史,并对 Swift 有一个初步的了解。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.1" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="2.1" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_9">
|
||||
<section class="normal" id="section-gitbook_122">
|
||||
|
||||
<h1 id="-">基础部分</h1>
|
||||
<p>Swift 是 iOS 和 OS X 应用开发的一门新语言。然而,如果你有 C 或者 Objective-C 开发经验的话,你会发现 Swift 的很多内容都是你熟悉的。</p>
|
||||
@ -978,7 +978,7 @@ assert(age >= 0, "A person's age cannot be less than zero")
|
||||
<a href="../chapter2/chapter2.html" class="navigation navigation-prev " aria-label="Previous page: Swift 教程"><i class="fa fa-angle-left"></i></a>
|
||||
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" class="navigation navigation-next " aria-label="Next page: 基本操作符"><i class="fa fa-angle-right"></i></a>
|
||||
<a href="../chapter2/02_Basic_Operators.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/01_The_Basics.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.2" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="2.2" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_31">
|
||||
<section class="normal" id="section-gitbook_144">
|
||||
|
||||
<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="1402238318316">
|
||||
<div class="book" data-level="2.3" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_10">
|
||||
<section class="normal" id="section-gitbook_123">
|
||||
|
||||
<h1 id="-strings-and-characters-">字符串和字符 (Strings and Characters)</h1>
|
||||
<p>本页包含内容:</p>
|
||||
@ -877,7 +877,7 @@ print("\n")
|
||||
</div>
|
||||
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" class="navigation navigation-prev " aria-label="Previous page: 基本操作符"><i class="fa fa-angle-left"></i></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" class="navigation navigation-prev " aria-label="Previous page: 基本运算符"><i class="fa fa-angle-left"></i></a>
|
||||
|
||||
|
||||
<a href="../chapter2/04_Collection_Types.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.4" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="2.4" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.5" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="2.5" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.6" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="2.6" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_24">
|
||||
<section class="normal" id="section-gitbook_137">
|
||||
|
||||
<h1 id="-functions-">函数(Functions)</h1>
|
||||
<p>本页包含内容:</p>
|
||||
@ -760,8 +760,8 @@
|
||||
<pre><code> join(string: "hello", toString: "world", withJoiner: "-")
|
||||
// returns "hello-world
|
||||
</code></pre><p>当这个函数被调用时,如果<code>joiner</code>的值没有被指定,函数会使用默认值(" "):</p>
|
||||
<pre><code> join(string: "hello", toString: "world", withJoiner: "-")
|
||||
// returns "hello-world
|
||||
<pre><code> join(string: "hello", toString:"world")
|
||||
// returns "hello world"
|
||||
</code></pre><h3 id="-external-names-for-parameters-with-default-values-">默认值参数的外部参数名(External Names for Parameters with Default Values)</h3>
|
||||
<p>在大多数情况下,给带默认值的参数起一个外部参数名是很有用的。这样可以保证当函数被调用且带默认值的参数被提供值时,实参的意图是明显的。</p>
|
||||
<p>为了使定义外部参数名更加简单,当你未给带默认值的参数提供外部参数名时,Swift会自动提供外部名字。此时外部参数名与局部名字是一样的,就像你已经在局部参数名前写了<code>井号(#)</code>一样。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.7" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="2.7" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_25">
|
||||
<section class="normal" id="section-gitbook_138">
|
||||
|
||||
<h1 id="-">闭包</h1>
|
||||
<p>本页内容包含:</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.8" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="2.8" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_26">
|
||||
<section class="normal" id="section-gitbook_139">
|
||||
|
||||
<h1 id="-">枚举</h1>
|
||||
<p>本页内容包含:</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.9" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="2.9" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_27">
|
||||
<section class="normal" id="section-gitbook_140">
|
||||
|
||||
<h3 id="-">类和结构体</h3>
|
||||
<p>本页包含内容:</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.10" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="2.10" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.11" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="2.11" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
@ -587,6 +587,209 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_142">
|
||||
|
||||
<h1 id="-methods-">方法(Methods)</h1>
|
||||
<p><strong>方法</strong>是与某些特定类型相关联的功能/函数。类、结构体、枚举都可以定义实例方法;实例方法为指定类型的实例封装了特定的任务与功能。类、结构体、枚举也可以定义类(型)方法(type itself);类型方法与类型自身相关联。类型方法与Objective-C中的类方法(class methods)相似。</p>
|
||||
<p>在Swift中,结构体和枚举能够定义方法;事实上这是Swift与C/Objective-C的主要区别之一。在Objective-C中,类是唯一能定义方法的类型。在Swift中,你能够选择是否定义一个类/结构体/枚举,并且你仍然享有在你创建的类型(类/结构体/枚举)上定义方法的灵活性。</p>
|
||||
<h3 id="-instance-methods-">实例方法(Instance Methods)</h3>
|
||||
<p><strong>实例方法</strong>是某个特定类、结构体或者枚举类型的实例的方法。实例方法支撑实例的功能: 或者提供方法,以访问和修改实例属性;或者提供与实例的目的相关的功能。实例方法的语法与函数完全一致,参考<a href="functions.html" title="函数说明">函数说明</a>。</p>
|
||||
<p>实例方法要写在它所属的类型的前后括号之间。实例方法能够访问他所属类型的所有的其他实例方法和属性。实例方法只能被它所属的类的特定实例调用。实例方法不能被孤立于现存的实例而被调用。</p>
|
||||
<p>下面是定义一个很简单的类<code>Counter</code>的例子(<code>Counter</code>能被用来对一个动作发生的次数进行计数):</p>
|
||||
<pre><code>class Counter {
|
||||
var count = 0
|
||||
func increment() {
|
||||
count++
|
||||
}
|
||||
func incrementBy(amount: Int) {
|
||||
count += amount
|
||||
}
|
||||
func reset() {
|
||||
count = 0
|
||||
}
|
||||
}
|
||||
</code></pre><p><code>Counter</code>类定理了三个实例方法:</p>
|
||||
<ul>
|
||||
<li><code>increment</code>让计数器按一递增;</li>
|
||||
<li><code>incrementBy(amount: Int)</code>让计数器按一个指定的整数值递增;</li>
|
||||
<li><code>reset</code>将计数器重置为0。</li>
|
||||
</ul>
|
||||
<p><code>Counter</code>这个类还声明了一个可变属性<code>count</code>,用它来保持对当前计数器值的追踪。</p>
|
||||
<p>和调用属性一样,用点语法(dot syntax)调用实例方法:</p>
|
||||
<pre><code> let counter = Counter()
|
||||
// the initial counter value is 0
|
||||
counter.increment()
|
||||
// the counter's value is now 1
|
||||
counter.incrementBy(5)
|
||||
// the counter's value is now 6
|
||||
counter.reset()
|
||||
// the counter's value is now 0
|
||||
</code></pre><h3 id="-local-and-external-parameter-names-for-methods-">方法的局部参数名称和外部参数名称(Local and External Parameter Names for Methods)</h3>
|
||||
<p>函数参数有一个局部名称(在函数体内部使用)和一个外部名称(在调用函数时使用),参考<a href="external_parameter_names.html">External Parameter Names</a>。对于方法参数也是这样,因为方法就是函数(只是这个函数与某个类型相关联了)。但是,方法和函数的局部名称和外部名称的默认行为是不一样的。</p>
|
||||
<p>Swift中的方法和Objective-C中的方法极其相似。像在Objective-C中一样,Swift中方法的名称通常用一个介词指向方法的第一个参数,比如:<code>with</code>,<code>for</code>,<code>by</code>等等。前面的<code>Counter</code>类的例子中<code>incrementBy</code>方法就是这样的。介词的使用让方法在被调用时能像一个句子一样被解读。Swift这种方法命名约定很容易落实,因为它是用不同的默认处理方法参数的方式,而不是用函数参数(来实现的)。</p>
|
||||
<p>具体来说,Swift默认仅给方法的第一个参数名称一个局部参数名称;但是默认同时给第二个和后续的参数名称局部参数名称和外部参数名称。
|
||||
这个约定与典型的命名和调用约定相匹配,这与你在写Objective-C的方法时很相似。这个约定还让expressive method调用不需要再检查/限定参数名。</p>
|
||||
<p>看看下面这个<code>Counter</code>的替换版本(它定义了一个更复杂的<code>incrementBy</code>方法):</p>
|
||||
<pre><code>class Counter {
|
||||
var count: Int = 0
|
||||
func incrementBy(amount: Int, numberOfTimes: Int) {
|
||||
count += amount * numberOfTimes
|
||||
}
|
||||
}
|
||||
</code></pre><p><code>incrementBy</code>方法有两个参数: <code>amount</code>和<code>numberOfTimes</code>。默认地,Swift只把<code>amount</code>当作一个局部名称,但是把<code>numberOfTimes</code>即看作本地名称又看作外部名称。下面调用这个方法:</p>
|
||||
<pre><code>let counter = Counter()
|
||||
counter.incrementBy(5, numberOfTimes: 3)
|
||||
// counter value is now 15
|
||||
</code></pre><p>你不必为第一个参数值再定义一个外部变量名:因为从函数名<code>incrementBy</code>已经能很清楚地看出它的目的/作用。但是第二个参数,就要被一个外部参数名称所限定,以便在方法被调用时让他目的/作用明确。</p>
|
||||
<p>这种默认的行为能够有效的检查方法,比如你在参数numberOfTimes前写了个井号( <code>#</code> )时:</p>
|
||||
<pre><code>func incrementBy(amount: Int, #numberOfTimes: Int) {
|
||||
count += amount * numberOfTimes
|
||||
}
|
||||
</code></pre><p>这种默认行为使上面代码意味着:在Swift中定义方法使用了与Objective-C同样的语法风格,并且方法将以自然表达式的方式被调用。</p>
|
||||
<h3 id="-modifying-external-parameter-name-behavior-for-methods-">修改外部参数名称(Modifying External Parameter Name Behavior for Methods)</h3>
|
||||
<p>有时为方法的第一个参数提供一个外部参数名称是非常有用的,尽管这不是默认的行为。你可以自己添加一个明确的外部名称;你也可以用一个hash符号作为第一个参数的前缀,然后用这个局部名字作为外部名字。</p>
|
||||
<p>相反,如果你不想为方法的第二个及后续的参数提供一个外部名称,你可以通过使用下划线(<code>_</code>)作为该参数的显式外部名称来覆盖默认行为。</p>
|
||||
<h3 id="-self-the-self-property-"><code>self</code>属性(The self Property)</h3>
|
||||
<p>类型的每一个实例都有一个隐含属性叫做<code>self</code>,它完全等同于这个实力变量本身。你可以在一个实例的实例方法中使用这个隐含的<code>self</code>属性来引用当前实例。</p>
|
||||
<p>上面例子中的<code>increment</code>方法可以被写成这样:</p>
|
||||
<pre><code>func increment() {
|
||||
self.count++
|
||||
}
|
||||
</code></pre><p>实际上,你不必在你的代码里面经常写<code>self</code>。不论何时,在一个方法中使用一个已知的属性或者方法名称,如果你没有明确的写<code>self</code>,Swift假定你是指当前实例的属性或者方法。这种假定在上面的<code>Counter</code>中已经示范了:<code>Counter</code>中的三个实例方法中都使用的是<code>count</code>(而不是<code>self.count</code>)</p>
|
||||
<p>这条规则的主要例外发生在当实例方法的某个参数名称与实例的某个属性名称相同时。
|
||||
在这种情况下,参数名称享有优先权,并且在引用属性时必须使用一种更恰当(被限定更严格)的方式。
|
||||
你可以使用隐藏的<code>self</code>属性来区分参数名称和属性名称。</p>
|
||||
<p>下面的例子演示了<code>self</code>消除方法参数<code>x</code>和实例属性<code>x</code>之间的歧义:</p>
|
||||
<pre><code>struct Point {
|
||||
var x = 0.0, y = 0.0
|
||||
func isToTheRightOfX(x: Double) -> Bool {
|
||||
return self.x > x
|
||||
}
|
||||
}
|
||||
let somePoint = Point(x: 4.0, y: 5.0)
|
||||
if somePoint.isToTheRightOfX(1.0) {
|
||||
println("This point is to the right of the line where x == 1.0")
|
||||
}
|
||||
// prints "This point is to the right of the line where x == 1.0"
|
||||
</code></pre><p>如果不使用<code>self</code>前缀,Swift就认为两次使用的<code>x</code>都指的是名称为<code>x</code>的函数参数。</p>
|
||||
<h3 id="-modifying-value-types-from-within-instance-methods-">在实例方法中修改值类型(Modifying Value Types from Within Instance Methods)</h3>
|
||||
<p>结构体和枚举是<strong>值类型</strong><a href=""#"">Structures and Enumerations Are Value Types</a>。一般情况下,值类型的属性不能在他的实例方法中被修改。</p>
|
||||
<p>但是,如果你确实需要在某个具体的方法中修改结构体或者枚举的属性,你可以选择<code>变异(mutating)</code>这个方法。方法可以从内部变异它的属性;并且它做的任何改变在方法结束时都会回写到原始结构。方法会给它隐含的<code>self</code>属性赋值一个全新的实例,这个新实例在方法结束后将替换原来的实例。</p>
|
||||
<p>要<code>变异</code>方法, 将关键字<code>mutating</code> 放到方法的<code>func</code>关键字之前就可以了:</p>
|
||||
<pre><code>struct Point {
|
||||
var x = 0.0, y = 0.0
|
||||
mutating func moveByX(deltaX: Double, y deltaY: Double) {
|
||||
x += deltaX
|
||||
y += deltaY
|
||||
}
|
||||
}
|
||||
var somePoint = Point(x: 1.0, y: 1.0)
|
||||
somePoint.moveByX(2.0, y: 3.0)
|
||||
println("The point is now at (\(somePoint.x), \(somePoint.y))")
|
||||
// prints "The point is now at (3.0, 4.0)"
|
||||
</code></pre><p>上面的Point结构体定义了一个变异方法(mutating method)<code>moveByX</code>,<code>moveByX</code>用来移动一个point。<code>moveByX</code>方法在被调用时修改了这个point,而不是返回一个新的point。方法定义是加上那个了<code>mutating</code>关键字,所以方法可以修改值类型的属性了。</p>
|
||||
<p>注意:不能在结构体类型的常量上调用变异方法,因为常量的属性不能被改变,就算你想改变的是常量的可变属性也不行,参考<a href=""#"">Stored Properties of Constant Structure Instances</a></p>
|
||||
<pre><code>let fixedPoint = Point(x: 3.0, y: 3.0)
|
||||
fixedPoint.moveByX(2.0, y: 3.0)
|
||||
// this will report an error
|
||||
</code></pre><h3 id="-self-assigning-to-self-within-a-mutating-method-">在变异方法中给self赋值(Assigning to self Within a Mutating Method)</h3>
|
||||
<p>变异方法能够赋给隐含属性<code>self</code>一个全新的实例。上面<code>Point</code>的例子可以用下面的方式改写:</p>
|
||||
<pre><code>struct Point {
|
||||
var x = 0.0, y = 0.0
|
||||
mutating func moveByX(deltaX: Double, y deltaY: Double) {
|
||||
self = Point(x: x + deltaX, y: y + deltaY)
|
||||
}
|
||||
}
|
||||
</code></pre><p>新版的变异方法<code>moveByX</code>创建了一个新的分支结构(他的x和y的值都被设定为目标值了)。调用这个版本的方法和调用上个版本的最终结果是一样的。</p>
|
||||
<p>枚举的变异方法可以让<code>self</code>从相同的枚举设置为不同的成员。</p>
|
||||
<pre><code>enum TriStateSwitch {
|
||||
case Off, Low, High
|
||||
mutating func next() {
|
||||
switch self {
|
||||
case Off:
|
||||
self = Low
|
||||
case Low:
|
||||
self = High
|
||||
case High:
|
||||
self = Off
|
||||
}
|
||||
}
|
||||
}
|
||||
var ovenLight = TriStateSwitch.Low
|
||||
ovenLight.next()
|
||||
// ovenLight is now equal to .High
|
||||
ovenLight.next()
|
||||
// ovenLight is now equal to .Off
|
||||
</code></pre><p>上面的例子中定义了一个三态开关的枚举。每次调用<code>next</code>方法时,开关在不同的电源状态(<code>Off</code>,<code>Low</code>,<code>High</code>)之前循环切换。</p>
|
||||
<h3 id="-type-methods-">类型方法(Type Methods)</h3>
|
||||
<p>实例方法是被类型的某个实例调用的方法。你也可以定义类列本身调用的方法,这种方法就叫做<strong>类型方法</strong>。声明类的类型方法,在方法的<code>func</code>关键字之前加上关键字<code>class</code>;声明结构体和枚举的类型方法,在方法的<code>func</code>关键字之前加上关键字<code>static</code>。</p>
|
||||
<blockquote>
|
||||
<p>注意:</p>
|
||||
<p>在Objective-C里面,你只能为Objective-C的类定义类型方法(type-level methods)。在Swift中,你可以为所有的类、结构体和枚举定义类型方法:Each type method is explicitly scoped to the type it supports.</p>
|
||||
</blockquote>
|
||||
<p>类型方法和实例方法一样用点语法调用。但是,你是在类型上调用这个方法,而不是在实例上调用。下面是如何在SomeClass类上调用类型方法的例子:</p>
|
||||
<pre><code>class SomeClass {
|
||||
class func someTypeMethod() {
|
||||
// type method implementation goes here
|
||||
}
|
||||
}
|
||||
SomeClass.someTypeMethod()
|
||||
</code></pre><p>在类型方法的方法体(body)中,<code>self</code>指向这个类型本身,而不是类型的某个实例。对于结构体和枚举来说,这意味着你可以用<code>self</code>来消除静态属性和静态方法参数之间的二意性(类似于我们在前面处理实例属性和实例方法参数时做的那样)。</p>
|
||||
<p>一般地,在类型方法里面所使用的任何未限定的方法和属性名称,将会指向其他的类型级别的方法和属性。一个类型方法可以用另一个类型方法的名称调用踏,而无需在方法名称前面加上类型名称的前缀。同样,结构体和枚举的类型方法也能够直接通过静态属性的名称访问静态属性,而不需要类型名称前缀。</p>
|
||||
<p>下面的例子定义了一个名为<code>LevelTracker</code>结构体。它监测玩家的发展情况(游戏的不同层次或阶段)。这是一个单人游戏,但也可以用作多玩家游戏中单个设备上的信息存储。</p>
|
||||
<p>游戏初始时,所有的游戏等级(除了等级1)都被锁定。每次有玩家完成一个等级,这个等级就对这个设备上的所有玩家解锁。<code>LevelTracker</code>结构体用静态属性和方法监测游戏的哪个等级已经被解锁。他还监测每个玩家的当前等级。</p>
|
||||
<pre><code>struct LevelTracker {
|
||||
static var highestUnlockedLevel = 1
|
||||
static func unlockLevel(level: Int) {
|
||||
if level > highestUnlockedLevel { highestUnlockedLevel = level }
|
||||
}
|
||||
static func levelIsUnlocked(level: Int) -> Bool {
|
||||
return level <= highestUnlockedLevel
|
||||
}
|
||||
var currentLevel = 1
|
||||
mutating func advanceToLevel(level: Int) -> Bool {
|
||||
if LevelTracker.levelIsUnlocked(level) {
|
||||
currentLevel = level
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
</code></pre><p><code>LevelTracker</code>监测玩家的已解锁的最高等级。这个值被存储在静态属性<code>highestUnlockedLevel</code>中。</p>
|
||||
<p><code>LevelTracker</code>还定义了两个类型方法与<code>highestUnlockedLevel</code>配合工作。第一个类型方法是<code>unlockLevel</code>:一旦新等级被解锁,它会更新<code>highestUnlockedLevel</code>的值。第二个类型方法是<code>levelIsUnlocked</code>:如果某个给定的等级已经被解锁,他返回<code>true</code>。(注意:我们没用使用<code>LevelTracker.highestUnlockedLevel</code>,这个类型方法还是能够访问静态属性<code>highestUnlockedLevel</code>)</p>
|
||||
<p>除了静态属性和类型方法,<code>LevelTracker</code>还监测每个玩家的进度。它用实例属性<code>currentLevel</code>来监测玩家当前正在进行的等级。</p>
|
||||
<p>为了便于管理<code>currentLevel</code>属性,<code>LevelTracker</code>定义了实例方法<code>advanceToLevel</code>。这个方法会在更新<code>currentLevel</code>之前检查所请求的新等级是否已经解锁。<code>advanceToLevel</code>方法返回布尔值以指示是否确实能够设置<code>currentLevel</code>了。</p>
|
||||
<p>下面,<code>Player</code>类使用<code>LevelTracker</code>来监测和更新每个玩家的发展进度:</p>
|
||||
<pre><code>class Player {
|
||||
var tracker = LevelTracker()
|
||||
let playerName: String
|
||||
func completedLevel(level: Int) {
|
||||
LevelTracker.unlockLevel(level + 1)
|
||||
tracker.advanceToLevel(level + 1)
|
||||
}
|
||||
init(name: String) {
|
||||
playerName = name
|
||||
}
|
||||
}
|
||||
</code></pre><p><code>Player</code>类创建一个新的<code>LevelTracker</code>实例来检测这个用户的发展进度。他提供了<code>completedLevel</code>方法:一旦玩家完成某个指定等级就调用它。这个方法为所有玩家解锁下一等级,并且将当前玩家的进度更新为下一等级。(我们忽略了<code>advanceToLevel</code>返回的布尔值,因为之前调用<code>LevelTracker.unlockLevel</code>时就知道了这个等级已经被解锁了)</p>
|
||||
<p>你还可以为一个新的玩家创建一个<code>Player</code>的实例,然后看这个玩家完成等级一时发生了什么:</p>
|
||||
<pre><code>var player = Player(name: "Argyrios")
|
||||
player.completedLevel(1)
|
||||
println("highest unlocked level is now \(LevelTracker.highestUnlockedLevel)")
|
||||
// prints "highest unlocked level is now 2"
|
||||
</code></pre><p>如果你创建了第二个玩家,并尝试让他开始一个没有被任何玩家解锁的等级,你试图去设置玩家当前等级时会失败的:</p>
|
||||
<pre><code>player = Player(name: "Beto")
|
||||
if player.tracker.advanceToLevel(6) {
|
||||
println("player is now on level 6")
|
||||
} else {
|
||||
println("level 6 has not yet been unlocked")
|
||||
}
|
||||
// prints "level 6 has not yet been unlocked"
|
||||
</code></pre>
|
||||
|
||||
</section>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -595,7 +798,7 @@
|
||||
<a href="../chapter2/10_Properties.html" class="navigation navigation-prev " aria-label="Previous page: 属性"><i class="fa fa-angle-left"></i></a>
|
||||
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" class="navigation navigation-next " aria-label="Next page: 下标"><i class="fa fa-angle-right"></i></a>
|
||||
<a href="../chapter2/12_Subscripts.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/11_Methods.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.12" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="2.12" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
@ -587,6 +587,113 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_143">
|
||||
|
||||
<h1 id="-subscripts-">下标 (Subscripts)</h1>
|
||||
<p>下标可以定义在类(Class)、结构体(structures)和枚举(enumerations)这些目标中,可以认为是访问对象、集合或序列的快捷方式。举例来说,用下标访问一个数组(Array)实例中的元素可以这样写 <code>someArray[index]</code> ,访问字典(Dictionary)实例中的元素可以这样写 <code>someDictionary[key]</code>,而不需要再调用实例的某个方法来获得元素的值。</p>
|
||||
<p>对于同一个目标可以定义多个下标,通过索引值类型的不同来进行重载,而且索引值的个数可以是多个。</p>
|
||||
<blockquote>
|
||||
<p>译者:这里下标重载在本小节中原文并没有任何演示</p>
|
||||
</blockquote>
|
||||
<h2 id="-">下标语法</h2>
|
||||
<p>下标允许你通过在实例后面的方括号中传入一个或者多个的索引值来对实例进行访问和赋值。语法类似于实例方法和实例属性的混合。与定义实例方法类似,定义下标使用<code>subscript</code>关键字,显式声明入参(一个或多个)和返回类型。与实例方法不同的是下标可以设定为读写或只读。这种方式又有点像实例属性的getter和setter:</p>
|
||||
<pre><code>subscript(index: Int) -> Int {
|
||||
get {
|
||||
// 返回与入参匹配的Int类型的值
|
||||
}
|
||||
|
||||
set(newValue) {
|
||||
// 执行赋值操作
|
||||
}
|
||||
}
|
||||
</code></pre><p><code>newValue</code>的类型必须和下标定义的返回类型相同。与实例属性相同的是set的入参声明<code>newValue</code>就算不写,在set代码块中依然可以使用<code>newValue</code>这个变量来访问新赋的值。</p>
|
||||
<p>与只读实例属性一样,可以直接将原本应该写在get代码块中的代码写在subscript中即可:</p>
|
||||
<pre><code>subscript(index: Int) -> Int {
|
||||
// 返回与入参匹配的Int类型的值
|
||||
}
|
||||
</code></pre><p>下面代码演示了一个在TimesTable结构体中使用只读下标的用法,该结构体用来展示传入整数的N倍。</p>
|
||||
<pre><code>struct TimesTable {
|
||||
let multiplier: Int
|
||||
subscript(index: Int) -> Int {
|
||||
return multiplier * index
|
||||
}
|
||||
}
|
||||
let threeTimesTable = TimesTable(multiplier: 3)
|
||||
println("3的6倍是\(threeTimesTable[6])")
|
||||
// 输出 "3的6倍是18"
|
||||
</code></pre><p>在上例中,通过TimesTable结构体创建了一个用来表示索引值三倍的实例。数值3作为结构体构造函数入参表示这个值将成为实例成员multiplier的值。</p>
|
||||
<p>你可以通过下标来来得到结果,比如<code>threeTimesTable[6]</code>。这句话访问了threeTimesTable的第六个元素,返回18或者6的3倍。</p>
|
||||
<blockquote>
|
||||
<p><b>提示</b></p>
|
||||
<p>TimesTable例子是基于一个固定的数学公式。它并不适合开放写权限来对threeTimesTable[someIndex]进行赋值操作,这也是为什么下标只定义为只读的原因。</p>
|
||||
</blockquote>
|
||||
<h2 id="-">下标用法</h2>
|
||||
<p>下标根据使用场景不同也具有不同的含义。通常下标是用来访问集合(collection),列表(list)或序列(sequence)中元素的快捷方式。你可以为特定的类或结构体中自由的实现下标来提供合适的功能。</p>
|
||||
<p>例如,Swift的字典(Dictionary)实现了通过下标来对其实例中存放的值进行存取操作。在字典中设值可以通过给字典提供一个符合字典索引类型的索引值的表达式赋一个与字典存放值类型匹配的值来做到:</p>
|
||||
<pre><code>var numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
|
||||
numberOfLegs["bird"] = 2
|
||||
</code></pre><p>上例定义一个名为numberOfLegs的变量并用一个字典表达式初始化出了包含三对键值的字典实例。numberOfLegs的字典存放值类型推断为<code>Dictionary<String, Int></code>。字典实例创建完成之后通过下标的方式将整型值<code>2</code>赋值到字典实例的索引为<code>bird</code>的位置中。</p>
|
||||
<p>更多关于字典(Dictionary)下标的信息请参考<a href="#">字典的访问与修改</a></p>
|
||||
<blockquote>
|
||||
<p><b>提示</b></p>
|
||||
<p>Swift中Dictionary的下标实现中,在get部分返回值是<code>Int?</code>,也就是说不是每个字典的索引都能得到一个整型值,对于没有设过值的索引的访问返回的结果就是<code>nil</code>;同样想要从字典实例中删除某个索引下的值也只需要给这个索引赋值为<code>nil</code>即可。</p>
|
||||
</blockquote>
|
||||
<h2 id="-">下标选项</h2>
|
||||
<p>下标允许任意数量的入参索引,并且每个入参类型也没有限制。下标的返回值也可以是任何类型。下标可以使用变量参数和可变参数,但使用in-out参数或给参数设置默认值都是不允许的。</p>
|
||||
<p>一个类或结构体可以根据自身需要提供多个下标实现,在定义下标时通过入参个类型进行区分,使用下标时会自动匹配合适的下标实现运行,这就是下标的重载。</p>
|
||||
<p>一个下标入参是最常见的情况,但只要有合适的场景也可以定义多个下标入参。如下例定义了一个Matrix结构体,将呈现一个Double类型的二维数组。Matrix结构体的下标需要两个整型参数:</p>
|
||||
<pre><code>struct Matrix {
|
||||
let rows: Int, columns: Int
|
||||
var grid: Double[]
|
||||
init(rows: Int, columns: Int) {
|
||||
self.rows = rows
|
||||
self.columns = columns
|
||||
grid = Array(count: rows * columns, repeatedValue: 0.0)
|
||||
}
|
||||
func indexIsValidForRow(row: Int, column: Int) -> Bool {
|
||||
return row >= 0 && row < rows && column >= 0 && column < columns
|
||||
}
|
||||
subscript(row: Int, column: Int) -> Double {
|
||||
get {
|
||||
assert(indexIsValidForRow(row, column: column), "Index out of range")
|
||||
return grid[(row * columns) + column]
|
||||
}
|
||||
set {
|
||||
assert(indexIsValidForRow(row, column: column), "Index out of range")
|
||||
grid[(row * columns) + columns] = newValue
|
||||
}
|
||||
}
|
||||
}
|
||||
</code></pre><p>Matrix提供了一个两个入参的构造方法,入参分别是<code>rows</code>和<code>columns</code>,创建了一个足够容纳rows * columns个数的Double类型数组。为了存储,将数组的大小和数组每个元素初始值0.0,都传入数组的构造方法中来创建一个正确大小的新数组。关于数组的构造方法和析构方法请参考<a href="#">Creating and Initializing an Array</a>。</p>
|
||||
<p>你可以通过传入合适的row和column的数量来构造一个新的Matrix实例:</p>
|
||||
<pre><code>var matrix = Matrix(rows: 2, columns: 2)
|
||||
</code></pre><p>上例中创建了一个新的两行两列的Matrix实例。在阅读顺序从左上到右下的Matrix实例中的数组实例grid是矩阵二维数组的扁平化存储:</p>
|
||||
<pre><code>// 示意图
|
||||
grid = [0.0, 0.0, 0.0, 0.0]
|
||||
|
||||
col0 col1
|
||||
row0 [0.0, 0.0,
|
||||
row1 0.0, 0.0]
|
||||
</code></pre><p>将值赋给带有row和column下标的matrix实例表达式可以完成赋值操作,下标入参使用逗号分割</p>
|
||||
<pre><code>matrix[0, 1] = 1.5
|
||||
matrix[1, 0] = 3.2
|
||||
</code></pre><p>上面两句话分别让matrix的右上值为1.5,坐下值为3.2:</p>
|
||||
<pre><code>[0.0, 1.5,
|
||||
3.2, 0.0]
|
||||
</code></pre><p>Matrix下标的getter和setter中同时调用了下标入参的row和column是否有效的判断。为了方便进行断言,Matrix包含了一个名为indexIsValid的成员方法,用来确认入参的row或column值是否会造成数组越界:</p>
|
||||
<pre><code>func indexIsValidForRow(row: Int, column: Int) -> Bool {
|
||||
return row >= 0 && row < rows && column >= 0 && column < columns
|
||||
}
|
||||
</code></pre><p>断言在下标越界时触发:</p>
|
||||
<pre><code>let someValue = matrix[2, 2]
|
||||
// 断言将会触发,因为 [2, 2] 已经超过了matrix的最大长度
|
||||
</code></pre><blockquote>
|
||||
<p>译者:这里有个词Computed Properties 这里统一翻译为实例属性了 微软术语引擎里没有这个词</p>
|
||||
</blockquote>
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.13" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="2.13" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_8">
|
||||
<section class="normal" id="section-gitbook_121">
|
||||
|
||||
<h1 id="-">继承</h1>
|
||||
<p>一个类可以继承另一个类的方法,属性和其它特性。当一个类继承其它类,继承类叫子类,被继承类叫超类(或父类)。在Swift中,继承是区分「类」与其它类型的一个基本特征。</p>
|
||||
@ -755,7 +755,7 @@ println("AutomaticCar: \(automatic.description())")
|
||||
</div>
|
||||
|
||||
|
||||
<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/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>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.14" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="2.14" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.15" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="2.15" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_33">
|
||||
<section class="normal" id="section-gitbook_146">
|
||||
|
||||
<h1 id="-">析构过程</h1>
|
||||
<p>在一个类的实例被释放之前,析构函数被立即调用。用关键字deinit来标示析构函数,类似于初始化函数用init来标示。析构函数只适用于类类型。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.16" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="2.16" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_34">
|
||||
<section class="normal" id="section-gitbook_147">
|
||||
|
||||
<h1 id="-">自动引用计数</h1>
|
||||
<p>本页包含内容:</p>
|
||||
@ -607,10 +607,320 @@
|
||||
<h2 id="-">自动引用计数的工作机制</h2>
|
||||
<p>当你每次创建一个类的新的实例的时候,ARC会分配一大块内存用来储存实例的信息。内存中会包含实例的类型信息,以及这个实例所有相关属性的值。此外,当实例不再被使用时,ARC释放实例所占用的内存,并让释放的内存能挪作他用。这确保了不再被使用的实例,不会一直占用内存空间。</p>
|
||||
<p>然而,当ARC收回和释放了正在被使用中的实例,该实例的属性和方法将不能再被访问和调用。实际上,如果你试图访问这个实例,你的应用程序很可能会崩溃。</p>
|
||||
<p>为了确保使用中的实例不会被回收,ARC会跟踪和计算每一个类实例正在被多少属性,常量和变量所引用。哪怕实例的引用数为一,ARC都不会回收这个实例。</p>
|
||||
<p>为了使之成为可能,无论你将类实例赋值给属性,常量或者是变量,属性,常量或者变量,都会对此实例创建强引用。之所以称之为强引用,是因为它会将实例牢牢的保持住,只要强引用还在,实例是不允许被释放的。</p>
|
||||
<h2 id="-">自动引用计数实践</h2>
|
||||
<p>为了确保使用中的实例不会被销毁,ARC会跟踪和计算每一个实例正在被多少属性,常量和变量所引用。哪怕实例的引用数为一,ARC都不会销毁这个实例。</p>
|
||||
<p>为了使之成为可能,无论你将实例赋值给属性,常量或者是变量,属性,常量或者变量,都会对此实例创建强引用。之所以称之为强引用,是因为它会将实例牢牢的保持住,只要强引用还在,实例是不允许被销毁的。</p>
|
||||
<h2 id="-">自动引用计数实战</h2>
|
||||
<p>下面的例子展示了自动引用计数的工作机制。例子以一个简单的Person类开始,并定义了一个叫name的常量属性:</p>
|
||||
<pre><code> class Person {
|
||||
let name: String
|
||||
|
||||
init(name: String) {
|
||||
self.name = name
|
||||
println("\(name) is being initialized")
|
||||
}
|
||||
|
||||
deinit {
|
||||
println("\(name) is being deinitialized")
|
||||
}
|
||||
}
|
||||
</code></pre><p>Person类有一个构造函数,此构造函数为实例的name属性赋值并打印出信息,以表明初始化过程生效。Person类同时也拥有析构函数,同样会在实例被销毁的时候打印出信息。</p>
|
||||
<p>接下来的代码片段定义了三个类型为Person?的变量,用来按照代码片段中的顺序,为新的Person实例建立多个引用。由于这些变量是被定义为可选类型(Person?,而不是Person),它们的值会被自动初始化为nil,目前还不会引用到Person类的实例。</p>
|
||||
<pre><code> var reference1: Person?
|
||||
var reference2: Person?
|
||||
var reference3: Person?
|
||||
</code></pre><p>现在你可以创建Person类的新实例,并且将它赋值给三个变量其中的一个:</p>
|
||||
<pre><code> reference1 = Person(name: "John Appleseed")
|
||||
// prints "John Appleseed is being initialized”
|
||||
</code></pre><p>应当注意到当你调用Person类的构造函数的时候,"John Appleseed is being initialized”会被打印出来。由此可以确定构造函数被执行。</p>
|
||||
<p>由于Person类的新实例被赋值给了reference1变量,所以reference1到Person类的新实例之间建立了一个强引用。正是因为这个强引用,ARC会保证Person实例被保持在内存中不被销毁。</p>
|
||||
<p>如果你将同样的Person实例也赋值给其他两个变量,该实例又会多出两个强引用:</p>
|
||||
<pre><code> reference2 = reference1
|
||||
reference3 = reference1
|
||||
</code></pre><p>现在这个Person实例已经有三个强引用了。</p>
|
||||
<p>如果你通过给两个变量赋值nil的方式断开两个强引用(包括最先的那个强引用),只留下一个强引用,Person实例不会被销毁:</p>
|
||||
<pre><code>reference2 = reference1
|
||||
reference3 = reference1
|
||||
</code></pre><p>ARC会在第三个,也即最后一个强引用被断开的时候,销毁Person实例,这也意味着你不再使用这个Person实例:</p>
|
||||
<pre><code>reference3 = nil
|
||||
// prints "John Appleseed is being deinitialized"
|
||||
</code></pre><h2 id="-">类实例之间的循环强引用</h2>
|
||||
<p>在上面的例子中,ARC会跟踪你所新创建的Person实例的引用数量,并且会在Person实例不再被需要时销毁它。</p>
|
||||
<p>然而,我们可能会写出这样的代码,一个类永远不会有0个强引用。这种情况发生在两个类实例互相保持对方的强引用,并让对方不被销毁。这就是所谓的循环强引用。</p>
|
||||
<p>你可以通过定义类之间的关系为弱引用或者无主引用,以此替代强引用,从而解决循环强引用的问题。具体的过程在<a href="http://numbbbbb.github.io/the-swift-programming-language-in-chinese/chapter2/16_Automatic_Reference_Counting.html#1" target="_blank">解决类实例之间的循环强引用</a>中有描述。不管怎样,在你学习怎样解决循环强引用之前,很有必要了解一下它是怎样产生的。</p>
|
||||
<p>下面展示了一个不经意产生循环强引用的例子。例子定义了两个类:Person和Apartment,用来建模公寓和它其中的居民:</p>
|
||||
<pre><code>class Person {
|
||||
let name: String
|
||||
init(name: String) { self.name = name }
|
||||
var apartment: Apartment?
|
||||
deinit { println("\(name) is being deinitialized") }
|
||||
}
|
||||
|
||||
class Apartment {
|
||||
let number: Int
|
||||
init(number: Int) { self.number = number }
|
||||
var tenant: Person?
|
||||
deinit { println("Apartment #\(number) is being deinitialized") }
|
||||
}
|
||||
</code></pre><p>每一个Person实例有一个类型为String,名字为name的属性,并有一个可选的初始化为nil的apartment属性。apartment属性是可选的,因为一个人并不总是拥有公寓。</p>
|
||||
<p>类似的,每个Apartment实例有一个叫number,类型为Int的属性,并有一个可选的初始化为nil的tenant属性。tenant属性是可选的,因为一栋公寓并不总是有居民。</p>
|
||||
<p>这两个类都定义了析构函数,用以在类实例被析构的时候输出信息。这让你能够知晓Person和Apartment的实例是否像预期的那样被销毁。</p>
|
||||
<p>接下来的代码片段定义了两个可选类型的变量john和number73,并分别被设定为下面的Apartment和Person的实例。这两个变量都被初始化为nil,并为可选的:</p>
|
||||
<pre><code>var john: Person?
|
||||
var number73: Apartment?
|
||||
</code></pre><p>现在你可以创建特定的Person和Apartment实例并将类实例赋值给john和number73变量:</p>
|
||||
<pre><code>john = Person(name: "John Appleseed")
|
||||
number73 = Apartment(number: 73)
|
||||
</code></pre><p>在两个实例被创建和赋值后,下图表面了强引用的关系。变量john现在有一个指向Person实例的强引用,而变量number73有一个指向Apartment实例的强引用:</p>
|
||||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/referenceCycle01_2x.png" alt=""></p>
|
||||
<p>现在你能够将这两个实例关联在一起,这样人就能有公寓住了,而公寓也有了房客。注意感叹号是用来展开和访问可选变量john和number73中的实例,这样实例的属性才能被赋值:</p>
|
||||
<pre><code>john!.apartment = number73
|
||||
number73!.tenant = john
|
||||
</code></pre><p>在将两个实例联系在一起之后,强引用的关系如图所示:</p>
|
||||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/referenceCycle02_2x.png" alt=""></p>
|
||||
<p>不幸的是,将这两个实例关联在一起之后,一个循环强引用被创建了。Person实例现在有了一个指向Apartment实例的强引用,而Apartment实例也有了一个指向Person实例的强引用。因此,当你断开john和number73变量所持有的强引用时,引用计数并不会降为0,实例也不会被ARC销毁:</p>
|
||||
<pre><code>john = nil
|
||||
number73 = nil
|
||||
</code></pre><p>注意,当你把这两个变量设为nil时,没有任何一个析构函数被调用。强引用循环阻止了Person和Apartment类实例的销毁,并在你的应用程序中造成了内存泄漏。</p>
|
||||
<p>在你将john和number73赋值为nil后,强引用关系如下图:</p>
|
||||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/referenceCycle03_2x.png" alt=""></p>
|
||||
<p>Person和Apartment实例之间的强引用关系保留了下来并且不会被断开。</p>
|
||||
<h2 id="-">解决实例之间的循环强引用</h2>
|
||||
<p>Swift提供了两种办法用来解决你在使用类的属性时所遇到的循环强引用问题:弱引用(weak reference)和无主引用(unowned reference)。</p>
|
||||
<p>弱引用和无主引用允许循环引用中的一个实例引用另外一个实例而不保持强引用。这样实例能够互相引用而不产生循环强引用。</p>
|
||||
<p>对于生命周期中会变为nil的实例使用弱引用。相反的,对于初始化赋值后再也不会被赋值为nil的实例,使用无主引用。</p>
|
||||
<h2 id="-">弱引用</h2>
|
||||
<p>弱引用不会牢牢保持住引用的实例,并且不会阻止ARC销毁被引用的实例。这种行为阻止了引用变为循环强引用。声明属性或者变量时,在前面加上weak关键字表明这是一个弱引用。</p>
|
||||
<p>在实例的生命周期中,如果某些时候引用没有值,那么弱引用可以阻止循环强引用。如果引用总是有值,则可以使用无主引用,在<a href="http://numbbbbb.github.io/the-swift-programming-language-in-chinese/chapter2/16_Automatic_Reference_Counting.html#2" target="_blank">无主引用</a>中有描述。在上面Apartment的例子中,一个公寓的生命周期中,有时是没有“居民”的,因此适合使用弱引用来解决循环强引用。</p>
|
||||
<blockquote>
|
||||
<p>注意: 弱引用必须被声明为变量,表明其值能在运行时被修改。弱引用不能被声明为常量。</p>
|
||||
</blockquote>
|
||||
<p>因为弱引用可以没有值,你必须将每一个弱引用声明为可选类型。可选类型是在Swift语言中推荐的用来表示可能没有值的类型。</p>
|
||||
<p>因为弱引用不会保持所引用的实例,即使引用存在,实例也有可能被销毁。因此,ARC会在引用的实例被销毁后自动将其赋值为nil。你可以像其他可选值一样,检查弱引用的值是否存在,你永远也不会遇到被销毁了而不存在的实例。</p>
|
||||
<p>下面的例子跟上面Person和Apartment的例子一致,但是有一个重要的区别。这一次,Apartment的tenant属性被声明为弱引用:</p>
|
||||
<pre><code>class Person {
|
||||
let name: String
|
||||
init(name: String) { self.name = name }
|
||||
var apartment: Apartment?
|
||||
deinit { println("\(name) is being deinitialized") }
|
||||
}
|
||||
|
||||
class Apartment {
|
||||
let number: Int
|
||||
init(number: Int) { self.number = number }
|
||||
weak var tenant: Person?
|
||||
deinit { println("Apartment #\(number) is being deinitialized") }
|
||||
}
|
||||
</code></pre><p>然后跟之前一样,建立两个变量(john和number73)之间的强引用,并关联两个实例:</p>
|
||||
<pre><code>var john: Person?
|
||||
var number73: Apartment?
|
||||
|
||||
john = Person(name: "John Appleseed")
|
||||
number73 = Apartment(number: 73)
|
||||
|
||||
john!.apartment = number73
|
||||
number73!.tenant = john
|
||||
</code></pre><p>现在,两个关联在一起的实例的引用关系如下图所示:</p>
|
||||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/weakReference01_2x.png" alt=""></p>
|
||||
<p>Person实例依然保持对Apartment实例的强引用,但是Apartment实例只是对Person实例的弱引用。这意味着当你断开john变量所保持的强引用时,再也没有指向Person实例的强引用了:</p>
|
||||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/weakReference02_2x.png" alt=""></p>
|
||||
<p>由于再也没有指向Person实例的强引用,该实例会被销毁:</p>
|
||||
<pre><code>john = nil
|
||||
// prints "John Appleseed is being deinitialized"
|
||||
</code></pre><p>唯一剩下的指向Apartment实例的强引用来自于变量number73。如果你断开这个强引用,再也没有指向Apartment实例的强引用了:</p>
|
||||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/weakReference03_2x.png" alt=""></p>
|
||||
<p>由于再也没有指向Apartment实例的强引用,该实例也会被销毁:</p>
|
||||
<pre><code>number73 = nil
|
||||
// prints "Apartment #73 is being deinitialized"
|
||||
</code></pre><p>上面的两段代码展示了变量john和number73在被赋值为nil后,Person实例和Apartment实例的析构函数都打印出“销毁”的信息。这证明了引用循环被打破了。</p>
|
||||
<h2 id="-">无主引用</h2>
|
||||
<p>和弱引用类似,无主引用不会牢牢保持住引用的实例。和弱引用不同的是,无主引用是永远有值的。因此,无主引用总是被定义为非可选类型(non-optional type)。你可以在声明属性或者变量时,在前面加上关键字unowned表示这是一个无主引用。</p>
|
||||
<p>由于无主引用是非可选类型,你不需要在使用它的时候将它展开。无主引用总是可以被直接访问。不过ARC无法在实例被销毁后将无主引用设为nil,因为非可选类型的变量不允许被赋值为nil。</p>
|
||||
<blockquote>
|
||||
<p>注意: 如果你试图在实例被销毁后,访问该实例的无主引用,会触发运行时错误。使用无主引用,你必须确保引用始终指向一个未销毁的实例。</p>
|
||||
<p>还需要注意的是如果你试图访问实例已经被销毁的无主引用,程序会直接崩溃,而不会发生无法预期的行为。所以你应当避免这样的事情发生。</p>
|
||||
</blockquote>
|
||||
<p>下面的例子定义了两个类,Customer和CreditCard,模拟了银行客户和客户的信用卡。这两个类中,每一个都将另外一个类的实例作为自身的属性。这种关系会潜在的创造循环强引用。</p>
|
||||
<p>Customer和CreditCard之间的关系与前面弱引用例子中Apartment和Person的关系截然不同。在这个数据模型中,一个客户可能有或者没有信用卡,但是一张信用卡总是关联着一个客户。为了表示这种关系,Customer类有一个可选类型的card属性,但是CreditCard类有一个非可选类型的customer属性。</p>
|
||||
<p>此外,只能通过将一个number值和customer实例传递给CreditCard构造函数的方式来创建CreditCard实例。这样可以确保当创建CreditCard实例时总是有一个customer实例与之关联。</p>
|
||||
<p>由于信用卡总是关联着一个客户,因此将customer属性定义为无主引用,用以避免循环强引用:</p>
|
||||
<pre><code>class Customer {
|
||||
let name: String
|
||||
var card: CreditCard?
|
||||
init(name: String) {
|
||||
self.name = name
|
||||
}
|
||||
deinit { println("\(name) is being deinitialized") }
|
||||
}
|
||||
|
||||
class CreditCard {
|
||||
let number: Int
|
||||
unowned let customer: Customer
|
||||
init(number: Int, customer: Customer) {
|
||||
self.number = number
|
||||
self.customer = customer
|
||||
}
|
||||
deinit { println("Card #\(number) is being deinitialized") }
|
||||
}
|
||||
</code></pre><p>下面的代码片段定义了一个叫john的可选类型Customer变量,用来保存某个特定客户的引用。由于是可选类型,所以变量被初始化为nil。</p>
|
||||
<pre><code>var john: Customer?
|
||||
</code></pre><p>现在你可以创建Customer类的实例,用它初始化CreditCard实例,并将新创建的CreditCard实例赋值为客户的card属性。</p>
|
||||
<pre><code>john = Customer(name: "John Appleseed")
|
||||
john!.card = CreditCard(number: 1234_5678_9012_3456, customer: john!)
|
||||
</code></pre><p>在你关联两个实例后,他们的引用关系如下图所示:</p>
|
||||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/unownedReference01_2x.png" alt=""></p>
|
||||
<p>Customer实例持有对CreditCard实例的强引用,而CreditCard实例持有对Customer实例的无主引用。</p>
|
||||
<p>由于customer的无主引用,当你断开john变量持有的强引用时,再也没有指向Customer实例的强引用了:</p>
|
||||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/unownedReference02_2x.png" alt=""></p>
|
||||
<p>由于再也没有指向Customer实例的强引用,该实例被销毁了。其后,再也没有指向CreditCard实例的强引用,该实例也随之被销毁了:</p>
|
||||
<pre><code>john = nil
|
||||
// prints "John Appleseed is being deinitialized"
|
||||
// prints "Card #1234567890123456 is being deinitialized"
|
||||
</code></pre><p>最后的代码展示了在john变量被设为nil后Customer实例和CreditCard实例的构造函数都打印出了“销毁”的信息。</p>
|
||||
<h2 id="-">无主引用以及显式展开的可选属性</h2>
|
||||
<p>上面弱引用和无主引用的例子涵盖了两种常用的需要打破循环强引用的场景。</p>
|
||||
<p>Person和Apartment的例子展示了两个属性的值都允许为nil,并会潜在的产生循环强引用。这种场景最适合用弱引用来解决。</p>
|
||||
<p>Customer和CreditCard的例子展示了一个属性的值允许为nil,而另一个属性的值不允许为nil,并会潜在的产生循环强引用。这种场景最适合通过无主引用来解决。</p>
|
||||
<p>然而,存在着第三种场景,在这种场景中,两个属性都必须有值,并且初始化完成后不能为nil。在这种场景中,需要一个类使用无主属性,而另外一个类使用显示展开的可选属性。</p>
|
||||
<p>这使两个属性在初始化完成后能被直接访问(不需要可选展开),同事避免了循环引用。这一节将为你展示如何建立这种关系。</p>
|
||||
<p>下面的例子定义了两个类,Country和City,每个类将另外一个类的实例保存为属性。在这个模型中,每个国家必须有首都,而每一个城市必须属于一个国家。为了实现这种关系,Country类拥有一个capitalCity属性,而City类有一个country属性:</p>
|
||||
<pre><code>class Country {
|
||||
let name: String
|
||||
let capitalCity: City!
|
||||
init(name: String, capitalName: String) {
|
||||
self.name = name
|
||||
self.capitalCity = City(name: capitalName, country: self)
|
||||
}
|
||||
}
|
||||
|
||||
class City {
|
||||
let name: String
|
||||
unowned let country: Country
|
||||
init(name: String, country: Country) {
|
||||
self.name = name
|
||||
self.country = country
|
||||
}
|
||||
}
|
||||
</code></pre><p>为了建立两个类的依赖关系,City的构造函数有一个Country实例的参数,并且将实例保存为country属性。</p>
|
||||
<p>Country的构造函数调用了City的构造函数。然而,只有Country的实例完全初始化完后,Country的构造函数才能把self传给City的构造函数。(<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-XID_288" target="_blank">在两阶段构造函数中有具体描述</a>)</p>
|
||||
<p>为了满足这种需求,通过在类型结尾处加上感叹号(City!)的方式,将Country的capitalCity属性声明为显示展开的可选类型属性。这表示像其他可选类型一样,capitalCity属性的默认值为nil,但是不需要展开他的值就能访问它。(<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_436" target="_blank">在显示展开的可选类型中有描述</a>)</p>
|
||||
<p>由于capitalCity默认值为nil,一旦Country的实例在构造函数中给name属性赋值后,整个初始化过程就完成了。这代表一旦name属性被后,Country的构造函数就能引用并传递显式的self。Country的构造函数在赋值capitalCity时,就能将self作为参数传递给City的构造函数。</p>
|
||||
<p>以上的意义在于你可以通过一条语句同时创建Country和City的实例,而不产生循环强引用,并且capitalCity的属性能被直接访问,而不需要通过感叹号来展开它的可选值:</p>
|
||||
<pre><code>var country = Country(name: "Canada", capitalName: "Ottawa")
|
||||
println("\(country.name)'s capital city is called \(country.capitalCity.name)")
|
||||
// prints "Canada's capital city is called Ottawa"
|
||||
</code></pre><p>在上面的例子中,使用显示展开可选值的意义在于满足了两个类构造函数的需求。capitalCity属性在初始化完成后,能作为非可选值使用同事还避免了循环强引用。</p>
|
||||
<h2 id="-">闭包引起的循环强引用</h2>
|
||||
<p>前面我们看到了循环强引用环是在两个类实例属性互相保持对方的强引用时产生的,还知道了如何用弱引用和无主引用来打破循环强引用。</p>
|
||||
<p>循环强引用还会发生在当你将一个闭包赋值给类实例的某个属性,并且这个闭包体中又使用了实例。这个闭包体中可能访问了实例的某个属性,例如self.someProperty,或者闭包中调用了实例的某个方法,例如self.someMethod。这两种情况都导致了闭包 “捕获" self,从而产生了循环强引用。</p>
|
||||
<p>循环强引用的产生,是因为闭包和类相似,都是引用类型。当你把一个闭包赋值给某个属性时,你也把一个引用赋值给了这个闭包。实质上,这跟之前的问题是一样的-两个强引用让彼此一直有效。但是,和两个类实例不同,这次一个是类实例,另一个是闭包。</p>
|
||||
<p>Swift提供了一种优雅的方法来解决这个问题,称之为闭包占用列表(closuer capture list)。同样的,在学习如何用闭包占用列表破坏循环强引用之前,先来了解一下循环强引用是如何产生的,这对我们是很有帮助的。</p>
|
||||
<p>下面的例子为你展示了当一个闭包引用了self后是如何产生一个循环强引用的。例子中定义了一个叫HTMLElement的类,用一种简单的模型表示HTML中的一个单独的元素:</p>
|
||||
<pre><code>class HTMLElement {
|
||||
|
||||
let name: String
|
||||
let text: String?
|
||||
|
||||
@lazy var asHTML: () -> String = {
|
||||
if let text = self.text {
|
||||
return "<\(self.name)>\(text)</\(self.name)>"
|
||||
} else {
|
||||
return "<\(self.name) />"
|
||||
}
|
||||
}
|
||||
|
||||
init(name: String, text: String? = nil) {
|
||||
self.name = name
|
||||
self.text = text
|
||||
}
|
||||
|
||||
deinit {
|
||||
println("\(name) is being deinitialized")
|
||||
}
|
||||
|
||||
}
|
||||
</code></pre><p>HTMLElement类定义了一个name属性来表示这个元素的名称,例如代表段落的"p",或者代表换行的"br"。HTMLElement还定义了一个可选属性text,用来设置和展现HTML元素的文本。</p>
|
||||
<p>除了上面的两个属性,HTMLElement还定义了一个lazy属性asHTML。这个属性引用了一个闭包,将name和text组合成HTML字符串片段。该属性是() -> String类型,或者可以理解为“一个没有参数,返回String的函数”。</p>
|
||||
<p>默认情况下,闭包赋值给了asHTML属性,这个闭包返回一个代表HTML标签的字符串。如果text值存在,该标签就包含可选值text;如果text不存在,该标签就不包含文本。对于段落元素,根据text是"some text"还是nil,闭包会返回"<code><p>some text</p></code>"或者"<code><p /></code>"。</p>
|
||||
<p>可以像实例方法那样去命名、使用asHTML属性。然而,由于asHTML是闭包而不是实例方法,如果你想改变特定元素的HTML处理的话,可以用自定义的闭包来取代默认值。</p>
|
||||
<blockquote>
|
||||
<p>注意: asHTML声明为lazy属性,因为只有当元素确实需要处理为HTML输出的字符串时,才需要使用asHTML。也就是说,在默认的闭包中可以使用self,因为只有当初始化完成以及self确实存在后,才能访问lazy属性。</p>
|
||||
</blockquote>
|
||||
<p>HTMLElement类只提供一个构造函数,通过name和text(如果有的话)参数来初始化一个元素。该类也定义了一个析构函数,当HTMLElement实例被销毁时,打印一条消息。</p>
|
||||
<p>下面的代码展示了如何用HTMLElement类创建实例并打印消息。</p>
|
||||
<pre><code>var paragraph: HTMLElement? = HTMLElement(name: "p", text: "hello, world")
|
||||
println(paragraph!.asHTML())
|
||||
// prints"hello, world"
|
||||
</code></pre><blockquote>
|
||||
<p>注意: 上面的paragraph变量定义为可选HTMLElement,因此我们可以赋值nil给它来演示循环强引用。 </p>
|
||||
</blockquote>
|
||||
<p>不幸的是,上面写的HTMLElement类产生了类实例和asHTML默认值的闭包之间的循环强引用。循环强引用如下图所示:</p>
|
||||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/closureReferenceCycle01_2x.png" alt=""></p>
|
||||
<p>实例的asHTML属性持有闭包的强引用。但是,闭包在其闭包体内使用了self(引用了self.name和self.text),因此闭包占有了self,这意味着闭包又反过来持有了HTMLElement实例的强引用。这样两个对象就产生了循环强引用。(更多关于闭包占有值的信息,请参考<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html#//apple_ref/doc/uid/TP40014097-CH11-XID_129" target="_blank">Capturing Values</a>)。</p>
|
||||
<blockquote>
|
||||
<p>注意: 虽然闭包多次使用了self,它只占有HTMLElement实例的一个强引用。</p>
|
||||
</blockquote>
|
||||
<p>如果设置paragraph变量为nil,打破它持有的HTMLElement实例的强引用,HTMLElement实例和它的闭包都不会被销毁,也是因为循环强引用:</p>
|
||||
<pre><code>paragraph = nil
|
||||
</code></pre><p>注意HTMLElementdeinitializer中的消息并没有别打印,证明了HTMLElement实例并没有被销毁。</p>
|
||||
<h2 id="-">解决闭包引起的循环强引用</h2>
|
||||
<p>在定义闭包时同时定义占有列表作为闭包的一部分,通过这种方式可以解决闭包和类实例之间的循环强引用。占有列表定义了闭包体内占有一个或者多个引用类型的规则。跟解决两个类实例间的循环强引用一样,声明每个占有的引用为弱引用或无主引用,而不是强引用。应当根据代码关系来决定使用弱引用还是无主引用。</p>
|
||||
<blockquote>
|
||||
<p>注意: Swift有如下要求:只要在闭包内使用self的成员,就要用self.someProperty或者self.someMethod(而不只是someProperty或someMethod)。这提醒你可能会不小心就占有了self。</p>
|
||||
</blockquote>
|
||||
<h2 id="-">定义占有列表</h2>
|
||||
<p>占有列表中的每个元素都是由weak或者unowned关键字和实例的引用(如self或someInstance)成对组成。每一对都在花括号中,通过逗号分开。</p>
|
||||
<p>占有列表放置在闭包参数列表和返回类型之前:</p>
|
||||
<pre><code>@lazy var someClosure: (Int, String) -> String = {
|
||||
[unowned self] (index: Int, stringToProcess: String) -> String in
|
||||
// closure body goes here
|
||||
}
|
||||
</code></pre><p>如果闭包没有指定参数列表或者返回类型,则可以通过上下文推断,那么可以占有列表放在闭包开始的地方,跟着是关键字in:</p>
|
||||
<pre><code>@lazy var someClosure: () -> String = {
|
||||
[unowned self] in
|
||||
// closure body goes here
|
||||
}
|
||||
</code></pre><h2 id="-">弱引用和无主引用</h2>
|
||||
<p>当闭包和占有的实例总是互相引用时并且总是同时销毁时,将闭包内的占有定义为无主引用。</p>
|
||||
<p>相反的,当占有引用有时可能会是nil时,将闭包内的占有定义为弱引用。弱引用总是可选类型,并且当引用的实例被销毁后,弱引用的值会自动置为nil。这使我们可以在闭包内检查他们是否存在。</p>
|
||||
<blockquote>
|
||||
<p>注意: 如果占有的引用绝对不会置为nil,应该用无主引用,而不是弱引用。</p>
|
||||
</blockquote>
|
||||
<p>前面的HTMLElement例子中,无主引用是正确的解决循环强引用的方法。这样这样编写HTMLElement类来避免循环强引用:</p>
|
||||
<pre><code>class HTMLElement {
|
||||
|
||||
let name: String
|
||||
let text: String?
|
||||
|
||||
@lazy var asHTML: () -> String = {
|
||||
[unowned self] in
|
||||
if let text = self.text {
|
||||
return "<\(self.name)>\(text)</\(self.name)>"
|
||||
} else {
|
||||
return "<\(self.name) />"
|
||||
}
|
||||
}
|
||||
|
||||
init(name: String, text: String? = nil) {
|
||||
self.name = name
|
||||
self.text = text
|
||||
}
|
||||
|
||||
deinit {
|
||||
println("\(name) is being deinitialized")
|
||||
}
|
||||
|
||||
}
|
||||
</code></pre><p>上面的HTMLElement实现和之前的实现一致,只是在asHTML闭包中多了一个占有列表。这里,占有列表是[unowned self],表示“用无主引用而不是强引用来占有self”。</p>
|
||||
<p>和之前一样,我们可以创建并打印HTMLElement实例:</p>
|
||||
<pre><code>var paragraph: HTMLElement? = HTMLElement(name: "p", text: "hello, world")
|
||||
println(paragraph!.asHTML())
|
||||
// prints "<p>hello, world</p>"
|
||||
</code></pre><p>使用占有列表后引用关系如下图所示:</p>
|
||||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/closureReferenceCycle02_2x.png" alt=""></p>
|
||||
<p>这一次,闭包以无主引用的形式占有self,并不会持有HTMLElement实例的强引用。如果将paragraph赋值为nil,HTMLElement实例将会被销毁,并能看到它的析构函数打印出的消息。</p>
|
||||
<pre><code>paragraph = nil
|
||||
// prints "p is being deinitialized"
|
||||
</code></pre>
|
||||
|
||||
</section>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.17" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="2.17" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_35">
|
||||
<section class="normal" id="section-gitbook_148">
|
||||
|
||||
<h1 id="optional-chaining">Optional Chaining</h1>
|
||||
<p>可选链(Optional Chaining)是一种可以请求和调用属性、方法及子脚本的过程,它的自判断性体现于请求或调用的目标当前可能为空(<code>nil</code>)。如果自判断的目标有值,那么调用就会成功;相反,如果选择的目标为空(<code>nil</code>),则这种调用将返回空(<code>nil</code>)。多次请求或调用可以被链接在一起形成一个链,如果任何一个节点为空(<code>nil</code>)将导致整个链失效。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.18" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="2.18" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_36">
|
||||
<section class="normal" id="section-gitbook_149">
|
||||
|
||||
<h1 id="-type-casting-">类型检查(Type Casting)</h1>
|
||||
<p>(ps:为了方便各位检验所以保留了英文,可删。)
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.19" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="2.19" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_37">
|
||||
<section class="normal" id="section-gitbook_150">
|
||||
|
||||
<h1 id="-">类型嵌套</h1>
|
||||
<p>本页包含内容:</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.20" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="2.20" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_38">
|
||||
<section class="normal" id="section-gitbook_151">
|
||||
|
||||
<h1 id="-extensions-">扩展(Extensions)</h1>
|
||||
<hr>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.21" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="2.21" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.22" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="2.22" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_41">
|
||||
<section class="normal" id="section-gitbook_154">
|
||||
|
||||
<h1 id="-">泛型</h1>
|
||||
<hr>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.23" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="2.23" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="2" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_45">
|
||||
<section class="normal" id="section-gitbook_158">
|
||||
|
||||
<h1 id="swift-">Swift 教程</h1>
|
||||
<p>本章介绍了 Swift 的各种特性及其使用方法,是全书的核心部分。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.1" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="3.1" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_47">
|
||||
<section class="normal" id="section-gitbook_160">
|
||||
|
||||
<h1 id="-">关于语言附注</h1>
|
||||
<p>本书的这一节描述了Swift编程语言的形式语法。这里描述的语法是为了帮助您更详细的了解该语言,而不是让您直接实现一个解析器或编译器。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.2" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="3.2" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.3" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="3.3" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.4" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="3.4" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.6" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="3.6" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.7" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="3.7" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
@ -587,6 +587,79 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_159">
|
||||
|
||||
<h1 id="-">特性</h1>
|
||||
<p>特性提供了关于声明和类型的更多信息。在Swift中有两类特性,用于修饰声明的以及用于修饰类型的。例如,<code>required</code>特性,当应用于一个类的指定或便利初始化器声明时,表明它的每个子类都必须实现那个初始化器。再比如<code>noreturn</code>特性,当应用于函数或方法类型时,表明该函数或方法不会返回到它的调用者。</p>
|
||||
<p>通过以下方式指定一个特性:符号<code>@</code>后面跟特性名,如果包含参数,则把参数带上:</p>
|
||||
<pre><code>@attribute name
|
||||
@attribute name(attribute arguments)
|
||||
</code></pre><p>有些声明特性通过接收参数来指定特性的更多信息以及它是如何修饰一个特定的声明的。这些特性的参数写在小括号内,它们的格式由它们所属的特性来定义。</p>
|
||||
<h2 id="-">声明特性</h2>
|
||||
<p>声明特性只能应用于声明。然而,你也可以将<code>noreturn</code>特性应用于函数或方法类型。</p>
|
||||
<p><code>assignment</code></p>
|
||||
<p>该特性用于修饰重载了复合赋值运算符的函数。重载了复合赋值运算符的函数必需将它们的初始输入参数标记为<code>inout</code>。如何使用<code>assignment</code>特性的一个例子,请见:<a href="">复合赋值运算符</a>。</p>
|
||||
<p><code>class_protocol</code></p>
|
||||
<p>该特性用于修饰一个协议表明该协议只能被类类型采用[待改:adopted]。</p>
|
||||
<p>如果你用<code>objc</code>特性修饰一个协议,<code>class_protocol</code>特性就会隐式地应用到该协议,因此无需显式地用<code>class_protocol</code>特性标记该协议。</p>
|
||||
<p><code>exported</code></p>
|
||||
<p>该特性用于修饰导入声明,以此来导出已导入的模块,子模块,或当前模块的声明。如果另一个模块导入了当前模块,那么那个模块可以访问当前模块的导出项。</p>
|
||||
<p><code>final</code></p>
|
||||
<p>该特性用于修饰一个类或类中的属性,方法,以及下标成员。如果用它修饰一个类,那么这个类则不能被继承。如果用它修饰类中的属性,方法或下标,则表示在子类中,它们不能被重写。</p>
|
||||
<p><code>lazy</code></p>
|
||||
<p>该特性用于修饰类或结构体中的存储型变量属性,表示该属性的初始值最多只被计算和存储一次,且发生在第一次访问它时。如何使用<code>lazy</code>特性的一个例子,请见:<a href="">惰性存储型属性</a>。</p>
|
||||
<p><code>noreturn</code></p>
|
||||
<p>该特性用于修饰函数或方法声明,表明该函数或方法的对应类型,<code>T</code>,是<code>@noreturn T</code>。你可以用这个特性修饰函数或方法的类型,这样一来,函数或方法就不会返回到它的调用者中去。</p>
|
||||
<p>对于一个没有用<code>noreturn</code>特性标记的函数或方法,你可以将它重写(override)为用该特性标记的。相反,对于一个已经用<code>noreturn</code>特性标记的函数或方法,你则不可以将它重写为没使用该特性标记的。相同的规则试用于当你在一个comforming类型中实现一个协议方法时。</p>
|
||||
<p><code>NSCopying</code></p>
|
||||
<p>该特性用于修饰一个类的存储型变量属性。该特性将使属性的setter与属性值的一个副本合成,由<code>copyWithZone</code>方法返回,而不是属性本身的值。该属性的类型必需遵循<code>NSCopying</code>协议。</p>
|
||||
<p><code>NSCopying</code>特性的行为与Objective-C中的<code>copy</code>特性相似。</p>
|
||||
<p><code>NSManaged</code></p>
|
||||
<p>该特性用于修饰<code>NSManagedObject</code>子类中的存储型变量属性,表明属性的存储和实现由Core Data在运行时基于相关实体描述动态提供。</p>
|
||||
<p><code>objc</code></p>
|
||||
<p>该特性用于修饰任意可以在Objective-C中表示的声明,比如,非嵌套类,协议,类和协议中的属性和方法(包含getter和setter),初始化器,析构器,以下下标。<code>objc</code>特性告诉编译器该声明可以在Objective-C代码中使用。</p>
|
||||
<p>如果你将<code>objc</code>特性应用于一个类或协议,它也会隐式地应用于那个类或协议的成员。对于标记了<code>objc</code>特性的类,编译器会隐式地为它的子类添加<code>objc</code>特性。标记了<code>objc</code>特性的协议不能继承自没有标记<code>objc</code>的协议。</p>
|
||||
<p><code>objc</code>特性有一个可选的参数,由标记符组成。当你想把<code>objc</code>所修饰的实体以一个不同的名字暴露给Objective-C,你就可以使用这个特性参数。你可以使用这个参数来命名类,协议,方法,getters,setters,以及初始化器。下面的例子把<code>ExampleClass</code>中<code>enabled</code>属性的getter暴露给Objective-C,名字是<code>isEnabled</code>,而不是它原来的属性名。</p>
|
||||
<pre><code>@objc
|
||||
class ExampleClass {
|
||||
var enabled: Bool {
|
||||
@objc(isEnabled) get {
|
||||
// Return the appropriate value
|
||||
}
|
||||
}
|
||||
}
|
||||
</code></pre><p><code>optional</code></p>
|
||||
<p>用该特性修饰协议的属性,方法或下标成员,表示实现这些成员并不需要一致性类型(conforming type)。</p>
|
||||
<p>你只能用<code>optional</code>特性修饰那些标记了<code>objc</code>特性的协议。因此,只有类类型可以adopt和comform to那些包含可选成员需求的协议。更多关于如何使用<code>optional</code>特性以及如何访问可选协议成员的指导,例如,当你不确定一个conforming类型是否实现了它们,请见:<a href="">可选协议需求</a>。</p>
|
||||
<p><code>required</code></p>
|
||||
<p>用该特性修饰一个类的指定或便利初始化器,表示该类的所有子类都必需实现该初始化器。</p>
|
||||
<p>加了该特性的指定初始化器必需显式地实现,而便利初始化器既可显式地实现,也可以在子类实现了超类所有指定初始化器后继承而来(或者当子类使用便利初始化器重写了指定初始化器)。</p>
|
||||
<h3 id="interface-builder-">Interface Builder使用的声明特性</h3>
|
||||
<p>Interface Builder特性是Interface Builder用来与Xcode同步的声明特性。Swift提供了以下的Interface Builder特性:<code>IBAction</code>,<code>IBDesignable</code>,<code>IBInspectable</code>,以及<code>IBOutlet</code>。这些特性与Objective-C中对应的特性在概念上是相同的。</p>
|
||||
<p><code>IBOutlet</code>和<code>IBInspectable</code>用于修饰一个类的属性声明;<code>IBAction</code>特性用于修饰一个类的方法声明;<code>IBDesignable</code>用于修饰类的声明。</p>
|
||||
<h2 id="-">类型特性</h2>
|
||||
<p>类型特性只能用于修饰类型。然而,你也可以用<code>noreturn</code>特性去修饰函数或方法声明。</p>
|
||||
<p><code>auto_closure</code></p>
|
||||
<p>这个特性通过自动地将表达式封闭到一个无参数闭包中来延迟表达式的求值。使用该特性修饰无参的函数或方法类型,返回表达式的类型。一个如何使用<code>auto_closure</code>特性的例子,见<a href="">函数类型</a></p>
|
||||
<p><code>noreturn</code></p>
|
||||
<p>该特性用于修饰函数或方法的类型,表明该函数或方法不会返回到它的调用者中去。你也可以用它标记函数或方法的声明,表示函数或方法的相应类型,<code>T</code>,是<code>@noreturn T</code>。</p>
|
||||
<blockquote>
|
||||
<p>特性的语法
|
||||
attribute -> @ <a href="">attribute-name</a> <a href="">attribute-argument-clause</a>opt
|
||||
attribute-name -> <a href="">identifier</a>
|
||||
attribute-argument-clause -> ( <a href="">balanced-tokens</a>opt )
|
||||
attributes -> <a href="">attribute</a> <a href="">attributes</a>opt
|
||||
balanced-tokens -> <a href="">balanced-token</a> <a href="">balanced-tokens</a>opt
|
||||
balanced-token -> ( <a href="">balanced-tokens</a>opt )
|
||||
balanced-token -> [ <a href="">balanced-tokens</a>opt ]
|
||||
balanced-token -> { <a href="">balanced-tokens</a>opt }
|
||||
balanced-token -> 任意标识符,关键字,字面量,或运算符
|
||||
balanced-token -> 任意标点符号,除了(, ), [, ], {, 或 }</p>
|
||||
</blockquote>
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.8" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="3.8" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.9" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="3.9" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
|
||||
@ -44,7 +44,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.10" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="3.10" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -176,7 +176,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -266,7 +266,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -518,7 +518,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -534,7 +534,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.5" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="3.5" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
@ -587,6 +587,262 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_168">
|
||||
|
||||
<h1 id="-">语句</h1>
|
||||
<p>在 Swift 中,有两种类型的语句:简单语句和控制流语句。简单语句是最常见的,用于构造表达式和声明。控制流语句则用于控制程序执行的流程,Swift 中有三种类型的控制流语句:循环语句、分支语句和控制传递语句。</p>
|
||||
<p>循环语句用于重复执行代码块;分支语句用于执行满足特定条件的代码块;控制传递语句则用于修改代码的执行顺序。在稍后的叙述中,将会详细地介绍每一种类型的控制流语句。</p>
|
||||
<p>是否将分号(;)添加到语句的结尾处是可选的。但若要在同一行内写多条独立语句,请务必使用分号。</p>
|
||||
<blockquote>
|
||||
<p>GRAMMAR OF A STATEMENT</p>
|
||||
<p><em>statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a><strong>;</strong> <em>opt</em></p>
|
||||
<p><em>statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration" target="_blank"><em>declaration</em></a><strong>;</strong> <em>opt</em></p>
|
||||
<p><em>statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/loop-statement" target="_blank"><em>loop-statement</em></a><strong>;</strong> <em>opt</em></p>
|
||||
<p><em>statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/branch-statement" target="_blank"><em>branch-statement</em></a><strong>;</strong> <em>opt</em></p>
|
||||
<p><em>statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/labeled-statement" target="_blank"><em>labeled-statement</em></a></p>
|
||||
<p><em>statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/control-transfer-statement" target="_blank"><em>control-transfer-statement</em></a><strong>;</strong> <em>opt</em></p>
|
||||
<p><em>statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement" target="_blank"><em>statment</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements" target="_blank"><em>statements</em></a><strong>;</strong> <em>opt</em></p>
|
||||
</blockquote>
|
||||
<h2 id="-">循环语句</h2>
|
||||
<p>取决于特定的循环条件,循环语句允许重复执行代码块。Swift 提供四种类型的循环语句:<code>for</code>语句、<code>for-in</code>语句、<code>while</code>语句和<code>do-while</code>语句。</p>
|
||||
<p>通过<code>break</code>语句和<code>continue</code>语句可以改变循环语句的控制流。有关这两条语句,请参考<a href=""><em>Break 语句</em><code>待添加链接</code></a>和<a href=""><em>Continue 语句</em><code>待添加链接</code></a>。</p>
|
||||
<blockquote>
|
||||
<p>GRAMMAR OF A LOOP STATEMENT</p>
|
||||
<p><em>loop-statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-statement" target="_blank"><em>for-statement</em></a></p>
|
||||
<p><em>loop-statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-in-statement" target="_blank"><em>for-in-statement</em></a></p>
|
||||
<p><em>loop-statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-statement" target="_blank"><em>while-statement</em></a></p>
|
||||
<p><em>loop-statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/do-while-statement" target="_blank"><em>do-while-statement</em></a></p>
|
||||
</blockquote>
|
||||
<h3 id="for-">For 语句</h3>
|
||||
<p><code>for</code>语句允许在重复执行代码块的同时,递增一个计数器。</p>
|
||||
<p><code>for</code>语句的形式如下:</p>
|
||||
<pre><code class="lang-swift">for `initialzation`; `condition`; `increment` {
|
||||
`statements`
|
||||
}
|
||||
</code></pre>
|
||||
<p><em>initialzation</em>、<em>condition</em>和<em>increment</em>之间的分号,以及包围循环体<em>statements</em>的大括号都是不可省略的。</p>
|
||||
<p><code>for</code>语句的执行流程如下:</p>
|
||||
<ol>
|
||||
<li><p><em>initialzation</em>只会被执行一次,通常用于声明和初始化在接下来的循环中需要使用的变量。</p>
|
||||
</li>
|
||||
<li><p>计算<em>condition</em>表达式:
|
||||
如果为真(<code>true</code>),<em>statements</em>将会被执行,然后转到第3步。如果为假(<code>false</code>),<em>statements</em>和<em>increment</em>都不会被执行,<code>for</code>至此执行完毕。</p>
|
||||
</li>
|
||||
<li><p>计算<em>increment</em>表达式,然后转到第2步。</p>
|
||||
</li>
|
||||
</ol>
|
||||
<p>定义在<em>initialzation</em>中的变量仅在<code>for</code>语句的作用域以内有效。<em>condition</em>表达式的值的类型必须符合<code>LogicValue</code>协议。</p>
|
||||
<blockquote>
|
||||
<p>GRAMMAR OF A FOR STATEMENT</p>
|
||||
<p><em>for-statement</em> → <strong>for</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-init" target="_blank"><em>for-init</em></a> <em>opt</em> <strong>;</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> <em>opt</em> <strong>;</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> <em>opt</em> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target="_blank"><em>code-block</em></a></p>
|
||||
<p><em>for-statement</em> → <strong>for ( </strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-init" target="_blank"><em>for-init</em></a> <em>opt</em> <strong>;</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> <em>opt</em> <strong>;</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> <em>opt</em> <strong>)</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target="_blank"><em>code-block</em></a></p>
|
||||
<p><em>for-statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-declaration" target="_blank"><em>variable-declaration</em></a> | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression-list" target="_blank"><em>expression-list</em></a></p>
|
||||
</blockquote>
|
||||
<h3 id="for-in-">For-In 语句</h3>
|
||||
<p><code>for-in</code>语句允许在重复执行代码块的同时,迭代集合(或符合<code>Sequence</code>协议的任意类型)中的每一项。</p>
|
||||
<p><code>for-in</code>语句的形式如下:</p>
|
||||
<pre><code class="lang-swift">for `item` in `collection` {
|
||||
`statements`
|
||||
}
|
||||
</code></pre>
|
||||
<p><code>for-in</code>语句在循环开始前会调用<em>collection</em>表达式的<code>generate</code>方法来获取一个生成器类型(这是一个符合<code>Generator</code>协议的类型)的值。接下来循环开始,调用<em>collection</em>表达式的<code>next</code>方法。如果其返回值不是<code>None</code>,它将会被赋给<em>item</em>,然后执行<em>statements</em>,执行完毕后回到循环开始处;否则,将不会赋值给<em>item</em>也不会执行<em>statements</em>,<code>for-in</code>至此执行完毕。</p>
|
||||
<blockquote>
|
||||
<p>GRAMMAR OF A FOR-IN STATEMENT</p>
|
||||
<p><em>for-in-statement</em> → <strong>for</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern" target="_blank"><em>pattern</em></a> <strong>in</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target="_blank"><em>code-block</em></a></p>
|
||||
</blockquote>
|
||||
<h3 id="while-">While 语句</h3>
|
||||
<p><code>while</code>语句允许重复执行代码块。</p>
|
||||
<p><code>while</code>语句的形式如下:</p>
|
||||
<pre><code class="lang-swift">while `condition` {
|
||||
`statements`
|
||||
}
|
||||
</code></pre>
|
||||
<p><code>while</code>语句的执行流程如下:</p>
|
||||
<ol>
|
||||
<li><p>计算<em>condition</em>表达式:
|
||||
如果为真(<code>true</code>),转到第2步。如果为假(<code>false</code>),<code>while</code>至此执行完毕。</p>
|
||||
</li>
|
||||
<li><p>执行<em>statements</em>,然后转到第1步。</p>
|
||||
</li>
|
||||
</ol>
|
||||
<p>由于<em>condition</em>的值在<em>statements</em>执行前就已计算出,因此<code>while</code>语句中的<em>statements</em>可能会被执行若干次,也可能不会被执行。</p>
|
||||
<p><em>condition</em>表达式的值的类型必须符合<code>LogicValue</code>协议。同时,<em>condition</em>表达式也可以使用可选绑定,请参考<a href="">可选绑定<code>待添加链接</code></a>。</p>
|
||||
<blockquote>
|
||||
<p>GRAMMAR OF A WHILE STATEMENT</p>
|
||||
<p><em>while-statement</em> → <strong>while</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-condition" target="_blank"><em>while-condition</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target="_blank"><em>code-block</em></a></p>
|
||||
<p><em>while-condition</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration" target="_blank"><em>declaration</em></a></p>
|
||||
</blockquote>
|
||||
<h3 id="do-while-">Do-While 语句</h3>
|
||||
<p><code>do-while</code>语句允许代码块被执行一次或多次。</p>
|
||||
<p><code>do-while</code>语句的形式如下:</p>
|
||||
<pre><code class="lang-swift">do {
|
||||
`statements`
|
||||
} while `condition`
|
||||
</code></pre>
|
||||
<p><code>do-while</code>语句的执行流程如下:</p>
|
||||
<ol>
|
||||
<li><p>执行<em>statements</em>,然后转到第2步。</p>
|
||||
</li>
|
||||
<li><p>计算<em>condition</em>表达式:
|
||||
如果为真(<code>true</code>),转到第1步。如果为假(<code>false</code>),<code>do-while</code>至此执行完毕。</p>
|
||||
</li>
|
||||
</ol>
|
||||
<p>由于<em>condition</em>表达式的值是在<em>statements</em>表达式执行后才计算出,因此<em>do-while</em>语句中的<em>statements</em>至少会被执行一次。</p>
|
||||
<p><em>condition</em>表达式的值的类型必须符合<code>LogicValue</code>协议。同时,<em>condition</em>表达式也可以使用可选绑定,请参考<a href="">可选绑定<code>待添加链接</code></a>。</p>
|
||||
<blockquote>
|
||||
<p>GRAMMAR OF A DO-WHILE STATEMENT</p>
|
||||
<p><em>do-while-statement</em> → <strong>do</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target="_blank"><em>code-block</em></a> <strong>while</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-condition" target="_blank"><em>while-condition</em></a> </p>
|
||||
</blockquote>
|
||||
<h2 id="-">分支语句</h2>
|
||||
<p>取决于一个或者多个条件的值,分支语句允许程序执行指定部分的代码。显然,分支语句中条件的值将会决定如何分支以及执行哪一块代码。Swift 提供两种类型的分支语句:<code>if</code>语句和<code>switch</code>语句。</p>
|
||||
<p><code>switch</code>语句中的控制流可以用<code>break</code>语句修改,请参考<a href="">Break 语句<code>待添加链接</code></a>。</p>
|
||||
<blockquote>
|
||||
<p>GRAMMAR OF A BRANCH STATEMENT</p>
|
||||
<p><em>branch-statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-statement" target="_blank"><em>if-statement</em></a></p>
|
||||
<p><em>branch-statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-statement" target="_blank"><em>switch-statement</em></a></p>
|
||||
</blockquote>
|
||||
<h3 id="if-">If 语句</h3>
|
||||
<p>取决于一个或多个条件的值,<code>if</code>语句将决定执行哪一块代码。</p>
|
||||
<p><code>if</code>语句有两种标准形式,在这两种形式里都必须有大括号。</p>
|
||||
<p>第一种形式是当且仅当条件为真时执行代码,像下面这样:</p>
|
||||
<pre><code class="lang-swift">if `condition` {
|
||||
`statements`
|
||||
}
|
||||
</code></pre>
|
||||
<p>第二种形式是在第一种形式的基础上添加<em>else语句</em>,当只有一个<em>else语句</em>时,像下面这样:</p>
|
||||
<pre><code class="lang-swift">if `condition` {
|
||||
`statements to execute if condition is true`
|
||||
} else {
|
||||
`statements to execute if condition is false`
|
||||
}
|
||||
</code></pre>
|
||||
<p>同时,<em>else语句</em>也可包含<code>if</code>语句,从而形成一条链来测试更多的条件,像下面这样:</p>
|
||||
<pre><code class="lang-swift">if `condition 1` {
|
||||
`statements to execute if condition 1 is true`
|
||||
} else if `condition 2` {
|
||||
`statements to execute if condition 2 is true`
|
||||
}
|
||||
else {
|
||||
`statements to execute if both conditions are false`
|
||||
}
|
||||
</code></pre>
|
||||
<p><code>if</code>语句中条件的值的类型必须符合<code>LogicValue</code>协议。同时,条件也可以使用可选绑定,请参考<a href="">可选绑定<code>待添加链接</code></a>。</p>
|
||||
<blockquote>
|
||||
<p>GRAMMAR OF AN IF STATEMENT</p>
|
||||
<p><em>if-statement</em> → <strong>if</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-condition" target="_blank"><em>if-condition</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target="_blank"><em>code-block</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/else-clause" target="_blank"><em>else-clause</em></a> <em>opt</em></p>
|
||||
<p><em>if-condition</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration" target="_blank"><em>declaration</em></a></p>
|
||||
<p><em>else-clause</em> → <strong>else</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target="_blank"><em>code-block</em></a> | <strong>else</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-statement" target="_blank"><em>if-statement</em></a> <em>opt</em></p>
|
||||
</blockquote>
|
||||
<h3 id="switch-">Switch 语句</h3>
|
||||
<p>取决于<code>switch</code>语句的控制表达式(<em>control expression</em>),<code>switch</code>语句将决定执行哪一块代码。</p>
|
||||
<p><code>switch</code>语句的形式如下:</p>
|
||||
<pre><code class="lang-swift">switch `control expression` {
|
||||
case `pattern 1`:
|
||||
`statements`
|
||||
case `pattern 2` where `condition`:
|
||||
`statements`
|
||||
case `pattern 3` where `condition`,
|
||||
`pattern 4` where `condition`:
|
||||
`statements`
|
||||
default:
|
||||
`statements`
|
||||
}
|
||||
</code></pre>
|
||||
<p><code>switch</code>语句的<em>控制表达式(control expression)</em>会首先被计算,然后与每一个<em>case</em>的模式(pattern)进行匹配。如果匹配成功,程序将会执行对应的<em>case</em>块里的<em>statements</em>。另外,每一个<em>case</em>块都不能为空,也就是说在每一个<em>case</em>块中至少有一条语句。如果你不想在匹配到的<em>case</em>块中执行代码,只需在块里写一条<code>break</code>语句即可。</p>
|
||||
<p>可以用作控制表达式的值是十分灵活的,除了标量类型(scalar types,如<code>Int</code>、<code>Character</code>)外,你可以使用任何类型的值,包括浮点数、字符串、元组、自定义类的实例和可选(optional)类型,甚至是枚举类型中的成员值和指定的范围(range)等。关于在<code>switch</code>语句中使用这些类型,请参考<a href="">控制流<code>待添加链接</code></a>一章的<a href="">Switch<code>待添加链接</code></a>。</p>
|
||||
<p>你可以在模式后面添加一个起保护作用的表达式(guard expression)。<em>起保护作用的表达式</em>是这样构成的:关键字<code>where</code>后面跟着一个作为额外测试条件的表达式。因此,当且仅当<em>控制表达式</em>匹配一个<em>case</em>的某个模式且起保护作用的表达式为真时,对应<em>case</em>块中的<em>statements</em>才会被执行。在下面的例子中,<em>控制表达式</em>只会匹配含两个相等元素的元组,如<code>(1, 1)</code>:</p>
|
||||
<pre><code class="lang-swift">case let (x, y) where x == y:
|
||||
}
|
||||
</code></pre>
|
||||
<p>正如上面这个例子,也可以在模式中使用<code>let</code>(或<code>var</code>)语句来绑定常量(或变量)。这些常量(或变量)可以在其对应的起保护作用的表达式和其对应的<em>case</em>块里的代码中引用。但是,如果<em>case</em>中有多个模式匹配控制表达式,那么这些模式都不能绑定常量(或变量)。</p>
|
||||
<p><code>switch</code>语句也可以包含默认(<code>default</code>)块,只有其它<em>case</em>块都无法匹配控制表达式时,默认块中的代码才会被执行。一个<code>switch</code>语句只能有一个默认块,而且必须在<code>switch</code>语句的最后面。</p>
|
||||
<p>尽管模式匹配操作实际的执行顺序,特别是模式的计算顺序是不可知的,但是 Swift 规定<code>switch</code>语句中的模式匹配的顺序和书写源代码的顺序保持一致。因此,当多个模式含有相同的值且能够匹配控制表达式时,程序只会执行源代码中第一个匹配的<em>case</em>块中的代码。</p>
|
||||
<h4 id="switch-">Switch 语句必须是完备的</h4>
|
||||
<p>在 Swift 中,<code>switch</code>语句中控制表达式的每一个可能的值都必须至少有一个<code>case</code>块与之对应。在某些情况下(例如,表达式的类型是<code>Int</code>),你可以使用默认块满足该要求。</p>
|
||||
<h4 id="-fall-through-">不存在隐式的贯穿(fall through)</h4>
|
||||
<p>当匹配的<em>case</em>块中的代码执行完毕后,程序会终止<code>switch</code>语句,而不会继续执行下一个<em>case</em>块。这就意味着,如果你想执行下一个<em>case</em>块,需要显式地在你需要的<em>case</em>块里使用<code>fallthrough</code>语句。关于<code>fallthrough</code>语句的更多信息,请参考<a href="">Fallthrough 语句<code>待添加链接</code></a>。</p>
|
||||
<blockquote>
|
||||
<p>GRAMMAR OF A SWITCH STATEMENT</p>
|
||||
<p><em>switch-statement</em> → <strong>switch</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> <strong>{</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-cases" target="_blank"><em>switch-cases</em></a> <em>opt</em> <strong>}</strong></p>
|
||||
<p><em>switch-cases</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-case" target="_blank"><em>switch-case</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-cases" target="_blank"><em>switch-cases</em></a> <em>opt</em></p>
|
||||
<p><em>switch-case</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-label" target="_blank"><em>case-label</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements" target="_blank"><em>statement</em></a> | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/default-label" target="_blank"><em>default-label</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements" target="_blank"><em>statements</em></a></p>
|
||||
<p><em>switch-case</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-label" target="_blank"><em>case-label</em></a> <strong>;</strong> | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/default-label" target="_blank"><em>default-label</em></a> <strong>;</strong></p>
|
||||
<p><em>case-label</em> → <strong>case</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-item-list" target="_blank"><em>case-item-list</em></a> <strong>:</strong></p>
|
||||
<p><em>case-item-list</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern" target="_blank"><em>pattern</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-clause" target="_blank"><em>guard-clause</em></a> <em>opt</em> | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern" target="_blank"><em>pattern</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-clause" target="_blank"><em>guard-clause</em></a> <em>opt</em>, <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-item-list" target="_blank"><em>case-item-list</em></a></p>
|
||||
<p><em>default-label</em> → <strong>default :</strong></p>
|
||||
<p><em>guard-clause</em> → <strong>where</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-expression" target="_blank"><em>guard-expression</em></a></p>
|
||||
<p><em>guard-expression</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a></p>
|
||||
</blockquote>
|
||||
<h2 id="-">带标签的语句</h2>
|
||||
<p>你可以在循环语句或<code>switch</code>语句前面加上<em>标签</em>,它由标签名和紧随其后的冒号(:)组成。在<code>break</code>和<code>continue</code>后面跟上标签名可以显式地在循环语句或<code>switch</code>语句中更改控制流,把控制权传递给指定标签标记的语句。关于这两条语句用法,请参考<a href="">Break 语句<code>待添加链接</code></a>和<a href="">Continue 语句<code>待添加链接</code></a>。</p>
|
||||
<p>标签的作用域是该标签所标记的语句之后的所有语句。你可以不使用带标签的语句,但只要使用它,标签名就必唯一。</p>
|
||||
<p>关于使用带标签的语句的例子,请参考<a href="">控制流<code>待添加链接</code></a>一章的<a href="">带标签的语句<code>待添加链接</code></a>。</p>
|
||||
<blockquote>
|
||||
<p>GRAMMAR OF A LABELED STATEMENT</p>
|
||||
<p><em>labeled-statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement-label" target="_blank"><em>statement-label</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/loop-statement" target="_blank"><em>loop-statement</em></a> | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement-label" target="_blank"><em>statement-label</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-statement" target="_blank"><em>switch-statement</em></a></p>
|
||||
<p><em>statement-label</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name" target="_blank"><em>label-name</em></a> <strong>:</strong></p>
|
||||
<p><em>label-name</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier" target="_blank"><em>identifier</em></a></p>
|
||||
</blockquote>
|
||||
<h2 id="-">控制传递语句</h2>
|
||||
<p>通过无条件地把控制权从一片代码传递到另一片代码,控制传递语句能够改变代码执行的顺序。Swift 提供四种类型的控制传递语句:<code>break</code>语句、<code>continue</code>语句、<code>fallthrough</code>语句和<code>return</code>语句。</p>
|
||||
<blockquote>
|
||||
<p>GRAMMAR OF A CONTROL TRANSER STATEMENT</p>
|
||||
<p><em>control-transfer-statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/break-statement" target="_blank"><em>break-statement</em></a></p>
|
||||
<p><em>control-transfer-statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/continue-statement" target="_blank"><em>continue-statement</em></a></p>
|
||||
<p><em>control-transfer-statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/fallthrough-statement" target="_blank"><em>fallthrough-statement</em></a></p>
|
||||
<p><em>control-transfer-statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/return-statement" target="_blank"><em>return-statement</em></a></p>
|
||||
</blockquote>
|
||||
<h3 id="break-">Break 语句</h3>
|
||||
<p><code>break</code>语句用于终止循环或<code>switch</code>语句的执行。使用<code>break</code>语句时,可以只写<code>break</code>这个关键词,也可以在<code>break</code>后面跟上标签名(label name),像下面这样:</p>
|
||||
<pre><code class="lang-swift">break
|
||||
break `label name`
|
||||
</code></pre>
|
||||
<p>当<code>break</code>语句后面带标签名时,可用于终止由这个标签标记的循环或<code>switch</code>语句的执行。</p>
|
||||
<p>而当只写<code>break</code>时,则会终止<code>switch</code>语句或上下文中包含<code>break</code>语句的最内层循环的执行。</p>
|
||||
<p>在这两种情况下,控制权都会被传递给循环或<code>switch</code>语句外面的第一行语句。</p>
|
||||
<p>关于使用<code>break</code>语句的例子,请参考<a href="">控制流<code>待添加链接</code></a>一章的<a href="">Break<code>待添加链接</code></a>和<a href="">带标签的语句<code>待添加链接</code></a>。</p>
|
||||
<blockquote>
|
||||
<p>GRAMMAR OF A BREAK STATEMENT</p>
|
||||
<p><em>break-statement</em> → <strong>break</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name" target="_blank"><em>label-name</em></a> <em>opt</em></p>
|
||||
</blockquote>
|
||||
<h3 id="continue-">Continue 语句</h3>
|
||||
<p><code>continue</code>语句用于终止循环中当前迭代的执行,但不会终止该循环的执行。使用<code>continue</code>语句时,可以只写<code>continue</code>这个关键词,也可以在<code>continue</code>后面跟上标签名(label name),像下面这样:</p>
|
||||
<pre><code class="lang-swift">continue
|
||||
continue `label name`
|
||||
</code></pre>
|
||||
<p>当<code>continue</code>语句后面带标签名时,可用于终止由这个标签标记的循环中当前迭代的执行。</p>
|
||||
<p>而当只写<code>break</code>时,可用于终止上下文中包含<code>continue</code>语句的最内层循环中当前迭代的执行。</p>
|
||||
<p>在这两种情况下,控制权都会被传递给循环外面的第一行语句。</p>
|
||||
<p>在<code>for</code>语句中,<code>continue</code>语句执行后,<em>increment</em>表达式还是会被计算,这是因为每次循环体执行完毕后<em>increment</em>表达式都会被计算。</p>
|
||||
<p>关于使用<code>continue</code>语句的例子,请参考<a href="">控制流<code>待添加链接</code></a>一章的<a href="">Continue<code>待添加链接</code></a>和<a href="">带标签的语句<code>待添加链接</code></a>。</p>
|
||||
<blockquote>
|
||||
<p>GRAMMAR OF A CONTINUE STATEMENT</p>
|
||||
<p><em>continue-statement</em> → <strong>continue</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name" target="_blank"><em>label-name</em></a> <em>opt</em></p>
|
||||
</blockquote>
|
||||
<h3 id="fallthrough-">Fallthrough 语句</h3>
|
||||
<p><code>fallthrough</code>语句用于在<code>switch</code>语句中传递控制权。<code>fallthrough</code>语句会把控制权从<code>switch</code>语句中的一个<code>case</code>传递给下一个<code>case</code>。这种传递是无条件的,即使下一个<code>case</code>的值与<code>switch</code>语句的控制表达式的值不匹配。</p>
|
||||
<p><code>fallthrough</code>语句可出现在<code>switch</code>语句中的任意<code>case</code>里,但不能出现在最后一个'case'块。同时,<code>fallthrough</code>语句也不能把控制权传递给使用了可选绑定的<code>case</code>块。</p>
|
||||
<p>关于在<code>switch</code>语句中使用<code>fallthrough</code>语句的例子,请参考<a href="">控制流<code>待添加链接</code></a>一章的<a href="">控制传递语句<code>待添加链接</code></a>。</p>
|
||||
<blockquote>
|
||||
<p>GRAMMAR OF A FALLTRHOUGH STATEMENT</p>
|
||||
<p><em>continue-statement</em> → <strong>fallthrough</strong></p>
|
||||
</blockquote>
|
||||
<h3 id="return-">Return 语句</h3>
|
||||
<p><code>return</code>语句用于在函数或方法的实现中将控制权传递给调用者,接着程序将会从调用者的位置继续向下执行。</p>
|
||||
<p>使用<code>return</code>语句时,可以只写<code>return</code>这个关键词,也可以在<code>return</code>后面跟上表达式,像下面这样:</p>
|
||||
<pre><code class="lang-swift">return
|
||||
return `表达式`
|
||||
</code></pre>
|
||||
<p>当<code>return</code>语句后面带表达式时,表达式的值将会返回给调用者。如果表达式值的类型与调用者期望的类型不匹配,Swift 则会在返回表达式的值之前将表达式值的类型转换为调用者期望的类型。</p>
|
||||
<p>而当只写<code>return</code>时,仅仅是将控制权从该函数或方法传递给调用者,而不返回一个值。(这就是说,该函数或方法的返回类型为<code>Void</code>或<code>()</code>)</p>
|
||||
<blockquote>
|
||||
<p>GRAMMAR OF A RETURN STATEMENT</p>
|
||||
<p><em>return-statement</em> → <strong>return</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> <em>opt</em></p>
|
||||
</blockquote>
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3" data-basepath=".." data-revision="1402238318316">
|
||||
<div class="book" data-level="3" data-basepath=".." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -178,7 +178,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -268,7 +268,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@
|
||||
|
||||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="../chapter2/12_Subscripts.html" title="下标" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -536,7 +536,7 @@
|
||||
|
||||
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
|
||||
12
index.html
12
index.html
@ -44,7 +44,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="0" data-basepath="." data-revision="1402238318316">
|
||||
<div class="book" data-level="0" data-basepath="." data-revision="1402300498179">
|
||||
<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>
|
||||
@ -176,7 +176,7 @@
|
||||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||||
|
||||
<a href="./chapter2/02_Basic_Operators.html">
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本操作符
|
||||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||||
</a>
|
||||
|
||||
|
||||
@ -266,7 +266,7 @@
|
||||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||||
|
||||
<a href="./chapter2/12_Subscripts.html">
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 下标
|
||||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||||
</a>
|
||||
|
||||
|
||||
@ -518,7 +518,7 @@
|
||||
|
||||
<a href="./chapter2/11_Methods.html" title="方法" class="chapter " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||||
|
||||
<a href="./chapter2/12_Subscripts.html" title="下标" class="chapter " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
<a href="./chapter2/12_Subscripts.html" title="附属脚本" class="chapter " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||||
|
||||
<a href="./chapter2/13_Inheritance.html" title="继承" class="chapter " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||||
|
||||
@ -534,7 +534,7 @@
|
||||
|
||||
<a href="./chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||||
|
||||
<a href="./chapter2/02_Basic_Operators.html" title="基本操作符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
<a href="./chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||||
|
||||
<a href="./chapter2/20_Extensions.html" title="扩展" class="chapter " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||||
|
||||
@ -585,7 +585,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_4">
|
||||
<section class="normal" id="section-gitbook_117">
|
||||
|
||||
<h1 id="swift-">Swift 编程语言</h1>
|
||||
<p>Swift 是苹果在 WWDC 2014 上发布的一款全新的编程语言,本书译自苹果官方的 Swift 教程《The Swift Programming Language》。</p>
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
CACHE MANIFEST
|
||||
# Revision 1402238318317
|
||||
# Revision 1402300498180
|
||||
|
||||
CACHE:
|
||||
index.html
|
||||
@ -15,8 +15,8 @@ chapter2/06_Functions.html
|
||||
chapter2/07_Closures.html
|
||||
chapter2/08_Enumerations.html
|
||||
chapter2/09_Classes_and_Structures.html
|
||||
chapter2/11_Methods.html
|
||||
chapter2/10_Properties.html
|
||||
chapter2/11_Methods.html
|
||||
chapter2/12_Subscripts.html
|
||||
chapter2/02_Basic_Operators.html
|
||||
chapter2/14_Initialization.html
|
||||
|
||||
File diff suppressed because one or more lines are too long
@ -5,7 +5,7 @@
|
||||
* [Swift 初见](chapter1/02_a_swift_tour.md)
|
||||
* [Swift 教程](chapter2/chapter2.md)
|
||||
* [基础部分](chapter2/01_The_Basics.md)
|
||||
* [基本操作符](chapter2/02_Basic_Operators.md)
|
||||
* [基本运算符](chapter2/02_Basic_Operators.md)
|
||||
* [字符串和字符](chapter2/03_Strings_and_Characters.md)
|
||||
* [集合类型](chapter2/04_Collection_Types.md)
|
||||
* [控制流](chapter2/05_Control_Flow.md)
|
||||
@ -15,7 +15,7 @@
|
||||
* [类和结构体](chapter2/09_Classes_and_Structures.md)
|
||||
* [属性](chapter2/10_Properties.md)
|
||||
* [方法](chapter2/11_Methods.md)
|
||||
* [下标](chapter2/12_Subscripts.md)
|
||||
* [附属脚本](chapter2/12_Subscripts.md)
|
||||
* [继承](chapter2/13_Inheritance.md)
|
||||
* [构造过程](chapter2/14_Initialization.md)
|
||||
* [析构过程](chapter2/15_Deinitialization.md)
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
# 关于 Swift
|
||||
|
||||
Swift 是一种新的编程语言,用于编写 iOS 和 OS X 应用程序。Swift 结合了 C 和 Objective-C 的优点并且不受C的兼容性的限制。Swift 使用安全的编程模式并添加了很多新特性,这将使编程更简单,扩展性更强,也更有趣。除此之外,Swift 还支持人见人爱的 Cocoa 和 Cocoa Touch 框架。拥有了这些特性,Swift将重新定义软件开发。
|
||||
Swift 是一种新的编程语言,用于编写 iOS 和 OS X 应用。Swift 结合了 C 和 Objective-C 的优点并且不受C的兼容性的限制。Swift 使用安全的编程模式并添加了很多新特性,这将使编程更简单,扩展性更强,也更有趣。除此之外,Swift 还支持人见人爱的 Cocoa 和 Cocoa Touch 框架。拥有了这些特性,Swift将重新定义软件开发。
|
||||
|
||||
Swift 的开发从很久之前就开始了。为了给 Swift 打好基础,苹果公司改进了编译器,调试器和框架结构。我们使用自动引用计数(Automatic Reference Counting, ARC)来简化内存管理。我们在 Foundation 和 Cocoa的基础上构建框架栈并将其标准化。Objective-C 本身支持块、集合语法和模块,所以框架可以轻松支持现代编程语言技术。得益于这些基础工作,我们现在可以发布一个新语言,用于未来的苹果软件的开发。
|
||||
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
|
||||
通常来说,编程语言教程中的第一个程序应该在屏幕上打印“Hello, world”。在 Swift 中,可以用一行代码实现:
|
||||
|
||||
println("hello, world")
|
||||
println("Hello, world")
|
||||
|
||||
如果你写过 C 或者 Objective-C 代码,那你应该很熟悉这种形式——在 Swift 中,这行代码就是一个完整的程序。你不需要为了输入输出或者字符串处理导入一个单独的库。全局作用域中的代码会被自动当做程序的入口点,所以你也不需要`main`函数。你同样不需要在每个语句结尾写上分号。
|
||||
|
||||
|
||||
@ -35,7 +35,7 @@ Swift统一的函数语法足够灵活,可以用来表示任何函数,包括
|
||||
// prints "Hello, Anna!"
|
||||
println(sayHello("Brian"))
|
||||
// prints "Hello, Brian!
|
||||
|
||||
|
||||
调用`sayHello`函数时,在圆括号中传给它一个`String`类型的实参。因为这个函数返回一个`String`类型的值,`sayHello`可以被包含在`println`的调用中,用来输出这个函数的返回值,正如上面所示。
|
||||
|
||||
在`sayHello`的函数体中,先定义了一个新的名为`greeting`的`String`常量,同时赋值了给`personName`的一个简单问候消息。然后用`return`关键字把这个问候返回出去。一旦`return greeting`被调用,该函数结束它的执行并返回`greeting`的当前值。
|
||||
@ -65,7 +65,7 @@ Swift统一的函数语法足够灵活,可以用来表示任何函数,包括
|
||||
}
|
||||
println(halfOpenRangeLength(1, 10))
|
||||
// prints "9
|
||||
|
||||
|
||||
### 无参函数(Functions Without Parameters)
|
||||
|
||||
函数可以没有参数。下面这个函数就是一个无参函数,当被调用时,它返回固定的`String`消息:
|
||||
@ -106,7 +106,7 @@ Swift统一的函数语法足够灵活,可以用来表示任何函数,包括
|
||||
// prints "hello, world" and returns a value of 12
|
||||
printWithoutCounting("hello, world")
|
||||
// prints "hello, world" but does not return a value
|
||||
|
||||
|
||||
第一个函数`printAndCount`,输出一个字符串并返回`Int`类型的字符数。第二个函数`printWithoutCounting`调用了第一个函数,但是忽略了它的返回值。当第二个函数被调用时,消息依然会由第一个函数输出,但是返回值不会被用到。
|
||||
|
||||
> 注意:
|
||||
@ -133,13 +133,13 @@ Swift统一的函数语法足够灵活,可以用来表示任何函数,包括
|
||||
}
|
||||
return (vowels, consonants, others)
|
||||
}
|
||||
|
||||
|
||||
你可以用`count`函数来处理任何一个字符串,返回的值将是一个包含三个`Int`型值的元组(tuple):
|
||||
|
||||
let total = count("some arbitrary string!")
|
||||
println("\(total.vowels) vowels and \(total.consonants) consonants")
|
||||
// prints "6 vowels and 13 consonants
|
||||
|
||||
|
||||
需要注意的是,元组的成员不需要在函数中返回时命名,因为它们的名字已经在函数返回类型有有了定义。
|
||||
|
||||
## 函数参数名(Function Parameter Names)
|
||||
@ -163,7 +163,7 @@ Swift统一的函数语法足够灵活,可以用来表示任何函数,包括
|
||||
// function body goes here, and can use localParameterName
|
||||
// to refer to the argument value for that parameter
|
||||
}
|
||||
|
||||
|
||||
> 注意:
|
||||
> 如果你提供了外部参数名,那么函数在被调用时,必须使用外部参数名。
|
||||
|
||||
@ -172,25 +172,25 @@ Swift统一的函数语法足够灵活,可以用来表示任何函数,包括
|
||||
func join(s1: String, s2: String, joiner: String) -> String {
|
||||
return s1 + joiner + s2
|
||||
}
|
||||
|
||||
|
||||
当你调用这个函数时,这三个字符串的用途是不清楚的:
|
||||
|
||||
join("hello", "world", ", ")
|
||||
// returns "hello, world
|
||||
|
||||
|
||||
为了让这些字符串的用途更为明显,我们为`join`函数添加外部参数名:
|
||||
|
||||
func join(string s1: String, toString s2: String, withJoiner joiner: String) -> String {
|
||||
return s1 + joiner + s2
|
||||
}
|
||||
|
||||
|
||||
在这个版本的`join`函数中,第一个参数有一个叫`string`的外部参数名和`s1`的局部参数名,第二个参数有一个叫`toString`的外部参数名和`s2`的局部参数名,第三个参数有一个叫`withJoiner`的外部参数名和`joiner`的局部参数名。
|
||||
|
||||
现在,你可以使用这些外部参数名以一种清晰地方式来调用函数了:
|
||||
|
||||
join(string: "hello", toString: "world", withJoiner: ", ")
|
||||
// returns "hello, world
|
||||
|
||||
|
||||
使用外部参数名让第二个版本的`join`函数的调用更为有表现力,更为通顺,同时还保持了函数体是可读的和有明确意图的。
|
||||
|
||||
> 注意:
|
||||
@ -210,7 +210,7 @@ Swift统一的函数语法足够灵活,可以用来表示任何函数,包括
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
这样定义参数名,使得函数体更为可读,清晰,同时也可以以一个不含糊的方式被调用:
|
||||
|
||||
let containsAVee = containsCharacter(string: "aardvark", characterToFind: "v")
|
||||
@ -237,9 +237,9 @@ Swift统一的函数语法足够灵活,可以用来表示任何函数,包括
|
||||
|
||||
当这个函数被调用时,如果`joiner`的值没有被指定,函数会使用默认值(" "):
|
||||
|
||||
join(string: "hello", toString: "world", withJoiner: "-")
|
||||
// returns "hello-world
|
||||
|
||||
join(string: "hello", toString:"world")
|
||||
// returns "hello world"
|
||||
|
||||
### 默认值参数的外部参数名(External Names for Parameters with Default Values)
|
||||
|
||||
在大多数情况下,给带默认值的参数起一个外部参数名是很有用的。这样可以保证当函数被调用且带默认值的参数被提供值时,实参的意图是明显的。
|
||||
@ -280,7 +280,7 @@ Swift统一的函数语法足够灵活,可以用来表示任何函数,包括
|
||||
// returns 3.0, which is the arithmetic mean of these five numbers
|
||||
arithmeticMean(3, 8, 19)
|
||||
// returns 10.0, which is the arithmetic mean of these three numbers
|
||||
|
||||
|
||||
> 注意:
|
||||
> 一个函数至多能有一个可变参数,而且它必须是参数表中最后的一个。这样做是为了避免函数调用时出现歧义。
|
||||
|
||||
@ -305,7 +305,7 @@ Swift统一的函数语法足够灵活,可以用来表示任何函数,包括
|
||||
let paddedString = alignRight(originalString, 10, "-")
|
||||
// paddedString is equal to "-----hello"
|
||||
// originalString is still equal to "hello”
|
||||
|
||||
|
||||
这个例子中定义了一个新的叫做`alignRight`的函数,用来右对齐输入的字符串到一个长的输出字符串中。左侧空余的地方用指定的填充字符填充。这个例子中,字符串`"hello"`被转换成了`"-----hello"`。
|
||||
|
||||
`alignRight`函数将参数`string`定义为变量参数。这意味着`string`现在可以作为一个局部变量,用传入的字符串值初始化,并且可以在函数体中进行操作。
|
||||
@ -333,7 +333,7 @@ Swift统一的函数语法足够灵活,可以用来表示任何函数,包括
|
||||
a = b
|
||||
b = temporaryA
|
||||
}
|
||||
|
||||
|
||||
这个`swapTwoInts`函数仅仅交换`a`与`b`的值。该函数先将`a`的值存到一个暂时常量`temporaryA`中,然后将`b`的值赋给`a`,最后将`temporaryA`幅值给`b`。
|
||||
|
||||
你可以用两个`Int`型的变量来调用`swapTwoInts`。需要注意的是,`someInt`和`anotherInt`在传入`swapTwoInts`函数前,都加了`&`的前缀:
|
||||
@ -361,7 +361,7 @@ Swift统一的函数语法足够灵活,可以用来表示任何函数,包括
|
||||
func multiplyTwoInts(a: Int, b: Int) -> Int {
|
||||
return a * b
|
||||
}
|
||||
|
||||
|
||||
这个例子中定义了两个简单的数学函数:`addTwoInts`和`multiplyTwoInts`。这两个函数都传入两个`Int`类型, 返回一个合适的`Int`值。
|
||||
|
||||
这两个函数的类型是`(Int, Int) -> Int`,可以读作“这个函数类型,它有两个`Int`型的参数并返回一个`Int`型的值。”。
|
||||
@ -371,7 +371,7 @@ Swift统一的函数语法足够灵活,可以用来表示任何函数,包括
|
||||
func printHelloWorld() {
|
||||
println("hello, world")
|
||||
}
|
||||
|
||||
|
||||
这个函数的类型是:`() -> ()`,或者叫“没有参数,并返回`Void`类型的函数。”。没有指定返回类型的函数总返回 `Void`。在Swift中,`Void`与空的元组是一样的。
|
||||
|
||||
### 使用函数类型(Using Function Types)
|
||||
@ -396,7 +396,7 @@ Swift统一的函数语法足够灵活,可以用来表示任何函数,包括
|
||||
mathFunction = multiplyTwoInts
|
||||
println("Result: \(mathFunction(2, 3))")
|
||||
// prints "Result: 6"
|
||||
|
||||
|
||||
就像其他类型一样,当赋值一个函数给常量或变量时,你可以让Swift来推测其函数类型:
|
||||
|
||||
let anotherMathFunction = addTwoInts
|
||||
@ -444,7 +444,7 @@ Swift统一的函数语法足够灵活,可以用来表示任何函数,包括
|
||||
var currentValue = 3
|
||||
let moveNearerToZero = chooseStepFunction(currentValue > 0)
|
||||
// moveNearerToZero now refers to the stepBackward() function
|
||||
|
||||
|
||||
上面这个例子中计算出从`currentValue`逐渐接近到`0`是需要向正数走还是向负数走。`currentValue`的初始值是`3`,这意味着`currentValue > 0`是真的(`true`),这将使得`chooseStepFunction`返回`stepBackward`函数。一个指向返回的函数的引用保存在了`moveNearerToZero`常量中。
|
||||
|
||||
现在,`moveNearerToZero`指向了正确的函数,它可以被用来数到`0`:
|
||||
@ -486,4 +486,4 @@ Swift统一的函数语法足够灵活,可以用来表示任何函数,包括
|
||||
// -3...
|
||||
// -2...
|
||||
// -1...
|
||||
// zero!
|
||||
// zero!
|
||||
|
||||
@ -0,0 +1,296 @@
|
||||
# 方法(Methods)
|
||||
|
||||
**方法**是与某些特定类型相关联的功能/函数。类、结构体、枚举都可以定义实例方法;实例方法为指定类型的实例封装了特定的任务与功能。类、结构体、枚举也可以定义类(型)方法(type itself);类型方法与类型自身相关联。类型方法与Objective-C中的类方法(class methods)相似。
|
||||
|
||||
在Swift中,结构体和枚举能够定义方法;事实上这是Swift与C/Objective-C的主要区别之一。在Objective-C中,类是唯一能定义方法的类型。在Swift中,你能够选择是否定义一个类/结构体/枚举,并且你仍然享有在你创建的类型(类/结构体/枚举)上定义方法的灵活性。
|
||||
|
||||
### 实例方法(Instance Methods)
|
||||
|
||||
**实例方法**是某个特定类、结构体或者枚举类型的实例的方法。实例方法支撑实例的功能: 或者提供方法,以访问和修改实例属性;或者提供与实例的目的相关的功能。实例方法的语法与函数完全一致,参考[函数说明](functions.md "函数说明")。
|
||||
|
||||
实例方法要写在它所属的类型的前后括号之间。实例方法能够访问他所属类型的所有的其他实例方法和属性。实例方法只能被它所属的类的特定实例调用。实例方法不能被孤立于现存的实例而被调用。
|
||||
|
||||
下面是定义一个很简单的类`Counter`的例子(`Counter`能被用来对一个动作发生的次数进行计数):
|
||||
|
||||
```
|
||||
class Counter {
|
||||
var count = 0
|
||||
func increment() {
|
||||
count++
|
||||
}
|
||||
func incrementBy(amount: Int) {
|
||||
count += amount
|
||||
}
|
||||
func reset() {
|
||||
count = 0
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`Counter`类定理了三个实例方法:
|
||||
- `increment`让计数器按一递增;
|
||||
- `incrementBy(amount: Int)`让计数器按一个指定的整数值递增;
|
||||
- `reset`将计数器重置为0。
|
||||
|
||||
`Counter`这个类还声明了一个可变属性`count`,用它来保持对当前计数器值的追踪。
|
||||
|
||||
和调用属性一样,用点语法(dot syntax)调用实例方法:
|
||||
|
||||
```
|
||||
let counter = Counter()
|
||||
// the initial counter value is 0
|
||||
counter.increment()
|
||||
// the counter's value is now 1
|
||||
counter.incrementBy(5)
|
||||
// the counter's value is now 6
|
||||
counter.reset()
|
||||
// the counter's value is now 0
|
||||
```
|
||||
|
||||
### 方法的局部参数名称和外部参数名称(Local and External Parameter Names for Methods)
|
||||
|
||||
函数参数有一个局部名称(在函数体内部使用)和一个外部名称(在调用函数时使用),参考[External Parameter Names](external_parameter_names.md)。对于方法参数也是这样,因为方法就是函数(只是这个函数与某个类型相关联了)。但是,方法和函数的局部名称和外部名称的默认行为是不一样的。
|
||||
|
||||
Swift中的方法和Objective-C中的方法极其相似。像在Objective-C中一样,Swift中方法的名称通常用一个介词指向方法的第一个参数,比如:`with`,`for`,`by`等等。前面的`Counter`类的例子中`incrementBy`方法就是这样的。介词的使用让方法在被调用时能像一个句子一样被解读。Swift这种方法命名约定很容易落实,因为它是用不同的默认处理方法参数的方式,而不是用函数参数(来实现的)。
|
||||
|
||||
具体来说,Swift默认仅给方法的第一个参数名称一个局部参数名称;但是默认同时给第二个和后续的参数名称局部参数名称和外部参数名称。
|
||||
这个约定与典型的命名和调用约定相匹配,这与你在写Objective-C的方法时很相似。这个约定还让expressive method调用不需要再检查/限定参数名。
|
||||
|
||||
看看下面这个`Counter`的替换版本(它定义了一个更复杂的`incrementBy`方法):
|
||||
|
||||
```
|
||||
class Counter {
|
||||
var count: Int = 0
|
||||
func incrementBy(amount: Int, numberOfTimes: Int) {
|
||||
count += amount * numberOfTimes
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`incrementBy`方法有两个参数: `amount`和`numberOfTimes`。默认地,Swift只把`amount`当作一个局部名称,但是把`numberOfTimes`即看作本地名称又看作外部名称。下面调用这个方法:
|
||||
|
||||
```
|
||||
let counter = Counter()
|
||||
counter.incrementBy(5, numberOfTimes: 3)
|
||||
// counter value is now 15
|
||||
```
|
||||
|
||||
你不必为第一个参数值再定义一个外部变量名:因为从函数名`incrementBy`已经能很清楚地看出它的目的/作用。但是第二个参数,就要被一个外部参数名称所限定,以便在方法被调用时让他目的/作用明确。
|
||||
|
||||
这种默认的行为能够有效的检查方法,比如你在参数numberOfTimes前写了个井号( `#` )时:
|
||||
|
||||
```
|
||||
func incrementBy(amount: Int, #numberOfTimes: Int) {
|
||||
count += amount * numberOfTimes
|
||||
}
|
||||
```
|
||||
|
||||
这种默认行为使上面代码意味着:在Swift中定义方法使用了与Objective-C同样的语法风格,并且方法将以自然表达式的方式被调用。
|
||||
|
||||
### 修改外部参数名称(Modifying External Parameter Name Behavior for Methods)
|
||||
|
||||
有时为方法的第一个参数提供一个外部参数名称是非常有用的,尽管这不是默认的行为。你可以自己添加一个明确的外部名称;你也可以用一个hash符号作为第一个参数的前缀,然后用这个局部名字作为外部名字。
|
||||
|
||||
相反,如果你不想为方法的第二个及后续的参数提供一个外部名称,你可以通过使用下划线(`_`)作为该参数的显式外部名称来覆盖默认行为。
|
||||
|
||||
### `self`属性(The self Property)
|
||||
|
||||
类型的每一个实例都有一个隐含属性叫做`self`,它完全等同于这个实力变量本身。你可以在一个实例的实例方法中使用这个隐含的`self`属性来引用当前实例。
|
||||
|
||||
上面例子中的`increment`方法可以被写成这样:
|
||||
```
|
||||
func increment() {
|
||||
self.count++
|
||||
}
|
||||
```
|
||||
|
||||
实际上,你不必在你的代码里面经常写`self`。不论何时,在一个方法中使用一个已知的属性或者方法名称,如果你没有明确的写`self`,Swift假定你是指当前实例的属性或者方法。这种假定在上面的`Counter`中已经示范了:`Counter`中的三个实例方法中都使用的是`count`(而不是`self.count`)
|
||||
|
||||
这条规则的主要例外发生在当实例方法的某个参数名称与实例的某个属性名称相同时。
|
||||
在这种情况下,参数名称享有优先权,并且在引用属性时必须使用一种更恰当(被限定更严格)的方式。
|
||||
你可以使用隐藏的`self`属性来区分参数名称和属性名称。
|
||||
|
||||
下面的例子演示了`self`消除方法参数`x`和实例属性`x`之间的歧义:
|
||||
|
||||
```
|
||||
struct Point {
|
||||
var x = 0.0, y = 0.0
|
||||
func isToTheRightOfX(x: Double) -> Bool {
|
||||
return self.x > x
|
||||
}
|
||||
}
|
||||
let somePoint = Point(x: 4.0, y: 5.0)
|
||||
if somePoint.isToTheRightOfX(1.0) {
|
||||
println("This point is to the right of the line where x == 1.0")
|
||||
}
|
||||
// prints "This point is to the right of the line where x == 1.0"
|
||||
```
|
||||
|
||||
如果不使用`self`前缀,Swift就认为两次使用的`x`都指的是名称为`x`的函数参数。
|
||||
|
||||
### 在实例方法中修改值类型(Modifying Value Types from Within Instance Methods)
|
||||
|
||||
结构体和枚举是**值类型**[Structures and Enumerations Are Value Types]("#")。一般情况下,值类型的属性不能在他的实例方法中被修改。
|
||||
|
||||
但是,如果你确实需要在某个具体的方法中修改结构体或者枚举的属性,你可以选择`变异(mutating)`这个方法。方法可以从内部变异它的属性;并且它做的任何改变在方法结束时都会回写到原始结构。方法会给它隐含的`self`属性赋值一个全新的实例,这个新实例在方法结束后将替换原来的实例。
|
||||
|
||||
要`变异`方法, 将关键字`mutating` 放到方法的`func`关键字之前就可以了:
|
||||
|
||||
```
|
||||
struct Point {
|
||||
var x = 0.0, y = 0.0
|
||||
mutating func moveByX(deltaX: Double, y deltaY: Double) {
|
||||
x += deltaX
|
||||
y += deltaY
|
||||
}
|
||||
}
|
||||
var somePoint = Point(x: 1.0, y: 1.0)
|
||||
somePoint.moveByX(2.0, y: 3.0)
|
||||
println("The point is now at (\(somePoint.x), \(somePoint.y))")
|
||||
// prints "The point is now at (3.0, 4.0)"
|
||||
```
|
||||
|
||||
上面的Point结构体定义了一个变异方法(mutating method)`moveByX`,`moveByX`用来移动一个point。`moveByX`方法在被调用时修改了这个point,而不是返回一个新的point。方法定义是加上那个了`mutating`关键字,所以方法可以修改值类型的属性了。
|
||||
|
||||
注意:不能在结构体类型的常量上调用变异方法,因为常量的属性不能被改变,就算你想改变的是常量的可变属性也不行,参考[Stored Properties of Constant Structure Instances]("#")
|
||||
|
||||
```
|
||||
let fixedPoint = Point(x: 3.0, y: 3.0)
|
||||
fixedPoint.moveByX(2.0, y: 3.0)
|
||||
// this will report an error
|
||||
```
|
||||
### 在变异方法中给self赋值(Assigning to self Within a Mutating Method)
|
||||
|
||||
变异方法能够赋给隐含属性`self`一个全新的实例。上面`Point`的例子可以用下面的方式改写:
|
||||
|
||||
```
|
||||
struct Point {
|
||||
var x = 0.0, y = 0.0
|
||||
mutating func moveByX(deltaX: Double, y deltaY: Double) {
|
||||
self = Point(x: x + deltaX, y: y + deltaY)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
新版的变异方法`moveByX`创建了一个新的分支结构(他的x和y的值都被设定为目标值了)。调用这个版本的方法和调用上个版本的最终结果是一样的。
|
||||
|
||||
枚举的变异方法可以让`self`从相同的枚举设置为不同的成员。
|
||||
|
||||
```
|
||||
enum TriStateSwitch {
|
||||
case Off, Low, High
|
||||
mutating func next() {
|
||||
switch self {
|
||||
case Off:
|
||||
self = Low
|
||||
case Low:
|
||||
self = High
|
||||
case High:
|
||||
self = Off
|
||||
}
|
||||
}
|
||||
}
|
||||
var ovenLight = TriStateSwitch.Low
|
||||
ovenLight.next()
|
||||
// ovenLight is now equal to .High
|
||||
ovenLight.next()
|
||||
// ovenLight is now equal to .Off
|
||||
```
|
||||
|
||||
上面的例子中定义了一个三态开关的枚举。每次调用`next`方法时,开关在不同的电源状态(`Off`,`Low`,`High`)之前循环切换。
|
||||
|
||||
### 类型方法(Type Methods)
|
||||
|
||||
实例方法是被类型的某个实例调用的方法。你也可以定义类列本身调用的方法,这种方法就叫做**类型方法**。声明类的类型方法,在方法的`func`关键字之前加上关键字`class`;声明结构体和枚举的类型方法,在方法的`func`关键字之前加上关键字`static`。
|
||||
|
||||
> 注意:
|
||||
|
||||
> 在Objective-C里面,你只能为Objective-C的类定义类型方法(type-level methods)。在Swift中,你可以为所有的类、结构体和枚举定义类型方法:Each type method is explicitly scoped to the type it supports.
|
||||
|
||||
类型方法和实例方法一样用点语法调用。但是,你是在类型上调用这个方法,而不是在实例上调用。下面是如何在SomeClass类上调用类型方法的例子:
|
||||
|
||||
```
|
||||
class SomeClass {
|
||||
class func someTypeMethod() {
|
||||
// type method implementation goes here
|
||||
}
|
||||
}
|
||||
SomeClass.someTypeMethod()
|
||||
```
|
||||
|
||||
在类型方法的方法体(body)中,`self`指向这个类型本身,而不是类型的某个实例。对于结构体和枚举来说,这意味着你可以用`self`来消除静态属性和静态方法参数之间的二意性(类似于我们在前面处理实例属性和实例方法参数时做的那样)。
|
||||
|
||||
一般地,在类型方法里面所使用的任何未限定的方法和属性名称,将会指向其他的类型级别的方法和属性。一个类型方法可以用另一个类型方法的名称调用踏,而无需在方法名称前面加上类型名称的前缀。同样,结构体和枚举的类型方法也能够直接通过静态属性的名称访问静态属性,而不需要类型名称前缀。
|
||||
|
||||
下面的例子定义了一个名为`LevelTracker`结构体。它监测玩家的发展情况(游戏的不同层次或阶段)。这是一个单人游戏,但也可以用作多玩家游戏中单个设备上的信息存储。
|
||||
|
||||
游戏初始时,所有的游戏等级(除了等级1)都被锁定。每次有玩家完成一个等级,这个等级就对这个设备上的所有玩家解锁。`LevelTracker`结构体用静态属性和方法监测游戏的哪个等级已经被解锁。他还监测每个玩家的当前等级。
|
||||
|
||||
```
|
||||
struct LevelTracker {
|
||||
static var highestUnlockedLevel = 1
|
||||
static func unlockLevel(level: Int) {
|
||||
if level > highestUnlockedLevel { highestUnlockedLevel = level }
|
||||
}
|
||||
static func levelIsUnlocked(level: Int) -> Bool {
|
||||
return level <= highestUnlockedLevel
|
||||
}
|
||||
var currentLevel = 1
|
||||
mutating func advanceToLevel(level: Int) -> Bool {
|
||||
if LevelTracker.levelIsUnlocked(level) {
|
||||
currentLevel = level
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`LevelTracker`监测玩家的已解锁的最高等级。这个值被存储在静态属性`highestUnlockedLevel`中。
|
||||
|
||||
`LevelTracker`还定义了两个类型方法与`highestUnlockedLevel`配合工作。第一个类型方法是`unlockLevel`:一旦新等级被解锁,它会更新`highestUnlockedLevel`的值。第二个类型方法是`levelIsUnlocked`:如果某个给定的等级已经被解锁,他返回`true`。(注意:我们没用使用`LevelTracker.highestUnlockedLevel`,这个类型方法还是能够访问静态属性`highestUnlockedLevel`)
|
||||
|
||||
除了静态属性和类型方法,`LevelTracker`还监测每个玩家的进度。它用实例属性`currentLevel`来监测玩家当前正在进行的等级。
|
||||
|
||||
为了便于管理`currentLevel`属性,`LevelTracker`定义了实例方法`advanceToLevel`。这个方法会在更新`currentLevel`之前检查所请求的新等级是否已经解锁。`advanceToLevel`方法返回布尔值以指示是否确实能够设置`currentLevel`了。
|
||||
|
||||
下面,`Player`类使用`LevelTracker`来监测和更新每个玩家的发展进度:
|
||||
|
||||
```
|
||||
class Player {
|
||||
var tracker = LevelTracker()
|
||||
let playerName: String
|
||||
func completedLevel(level: Int) {
|
||||
LevelTracker.unlockLevel(level + 1)
|
||||
tracker.advanceToLevel(level + 1)
|
||||
}
|
||||
init(name: String) {
|
||||
playerName = name
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`Player`类创建一个新的`LevelTracker`实例来检测这个用户的发展进度。他提供了`completedLevel`方法:一旦玩家完成某个指定等级就调用它。这个方法为所有玩家解锁下一等级,并且将当前玩家的进度更新为下一等级。(我们忽略了`advanceToLevel`返回的布尔值,因为之前调用`LevelTracker.unlockLevel`时就知道了这个等级已经被解锁了)
|
||||
|
||||
你还可以为一个新的玩家创建一个`Player`的实例,然后看这个玩家完成等级一时发生了什么:
|
||||
|
||||
```
|
||||
var player = Player(name: "Argyrios")
|
||||
player.completedLevel(1)
|
||||
println("highest unlocked level is now \(LevelTracker.highestUnlockedLevel)")
|
||||
// prints "highest unlocked level is now 2"
|
||||
```
|
||||
|
||||
如果你创建了第二个玩家,并尝试让他开始一个没有被任何玩家解锁的等级,你试图去设置玩家当前等级时会失败的:
|
||||
|
||||
```
|
||||
player = Player(name: "Beto")
|
||||
if player.tracker.advanceToLevel(6) {
|
||||
println("player is now on level 6")
|
||||
} else {
|
||||
println("level 6 has not yet been unlocked")
|
||||
}
|
||||
// prints "level 6 has not yet been unlocked"
|
||||
```
|
||||
|
||||
|
||||
@ -0,0 +1,161 @@
|
||||
# 下标 (Subscripts)
|
||||
|
||||
下标可以定义在类(Class)、结构体(structures)和枚举(enumerations)这些目标中,可以认为是访问对象、集合或序列的快捷方式。举例来说,用下标访问一个数组(Array)实例中的元素可以这样写 `someArray[index]` ,访问字典(Dictionary)实例中的元素可以这样写 `someDictionary[key]`,而不需要再调用实例的某个方法来获得元素的值。
|
||||
|
||||
对于同一个目标可以定义多个下标,通过索引值类型的不同来进行重载,而且索引值的个数可以是多个。
|
||||
|
||||
> 译者:这里下标重载在本小节中原文并没有任何演示
|
||||
|
||||
|
||||
## 下标语法
|
||||
|
||||
下标允许你通过在实例后面的方括号中传入一个或者多个的索引值来对实例进行访问和赋值。语法类似于实例方法和实例属性的混合。与定义实例方法类似,定义下标使用`subscript`关键字,显式声明入参(一个或多个)和返回类型。与实例方法不同的是下标可以设定为读写或只读。这种方式又有点像实例属性的getter和setter:
|
||||
|
||||
```
|
||||
subscript(index: Int) -> Int {
|
||||
get {
|
||||
// 返回与入参匹配的Int类型的值
|
||||
}
|
||||
|
||||
set(newValue) {
|
||||
// 执行赋值操作
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`newValue`的类型必须和下标定义的返回类型相同。与实例属性相同的是set的入参声明`newValue`就算不写,在set代码块中依然可以使用`newValue`这个变量来访问新赋的值。
|
||||
|
||||
与只读实例属性一样,可以直接将原本应该写在get代码块中的代码写在subscript中即可:
|
||||
|
||||
```
|
||||
subscript(index: Int) -> Int {
|
||||
// 返回与入参匹配的Int类型的值
|
||||
}
|
||||
```
|
||||
|
||||
下面代码演示了一个在TimesTable结构体中使用只读下标的用法,该结构体用来展示传入整数的N倍。
|
||||
|
||||
```
|
||||
struct TimesTable {
|
||||
let multiplier: Int
|
||||
subscript(index: Int) -> Int {
|
||||
return multiplier * index
|
||||
}
|
||||
}
|
||||
let threeTimesTable = TimesTable(multiplier: 3)
|
||||
println("3的6倍是\(threeTimesTable[6])")
|
||||
// 输出 "3的6倍是18"
|
||||
```
|
||||
|
||||
在上例中,通过TimesTable结构体创建了一个用来表示索引值三倍的实例。数值3作为结构体构造函数入参表示这个值将成为实例成员multiplier的值。
|
||||
|
||||
你可以通过下标来来得到结果,比如`threeTimesTable[6]`。这句话访问了threeTimesTable的第六个元素,返回18或者6的3倍。
|
||||
|
||||
> <b>提示</b>
|
||||
>
|
||||
> TimesTable例子是基于一个固定的数学公式。它并不适合开放写权限来对threeTimesTable[someIndex]进行赋值操作,这也是为什么下标只定义为只读的原因。
|
||||
|
||||
|
||||
## 下标用法
|
||||
|
||||
下标根据使用场景不同也具有不同的含义。通常下标是用来访问集合(collection),列表(list)或序列(sequence)中元素的快捷方式。你可以为特定的类或结构体中自由的实现下标来提供合适的功能。
|
||||
|
||||
例如,Swift的字典(Dictionary)实现了通过下标来对其实例中存放的值进行存取操作。在字典中设值可以通过给字典提供一个符合字典索引类型的索引值的表达式赋一个与字典存放值类型匹配的值来做到:
|
||||
|
||||
```
|
||||
var numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
|
||||
numberOfLegs["bird"] = 2
|
||||
```
|
||||
|
||||
上例定义一个名为numberOfLegs的变量并用一个字典表达式初始化出了包含三对键值的字典实例。numberOfLegs的字典存放值类型推断为`Dictionary<String, Int>`。字典实例创建完成之后通过下标的方式将整型值`2`赋值到字典实例的索引为`bird`的位置中。
|
||||
|
||||
更多关于字典(Dictionary)下标的信息请参考[字典的访问与修改](#)
|
||||
|
||||
> <b>提示</b>
|
||||
>
|
||||
> Swift中Dictionary的下标实现中,在get部分返回值是`Int?`,也就是说不是每个字典的索引都能得到一个整型值,对于没有设过值的索引的访问返回的结果就是`nil`;同样想要从字典实例中删除某个索引下的值也只需要给这个索引赋值为`nil`即可。
|
||||
|
||||
|
||||
## 下标选项
|
||||
|
||||
下标允许任意数量的入参索引,并且每个入参类型也没有限制。下标的返回值也可以是任何类型。下标可以使用变量参数和可变参数,但使用in-out参数或给参数设置默认值都是不允许的。
|
||||
|
||||
一个类或结构体可以根据自身需要提供多个下标实现,在定义下标时通过入参个类型进行区分,使用下标时会自动匹配合适的下标实现运行,这就是下标的重载。
|
||||
|
||||
一个下标入参是最常见的情况,但只要有合适的场景也可以定义多个下标入参。如下例定义了一个Matrix结构体,将呈现一个Double类型的二维数组。Matrix结构体的下标需要两个整型参数:
|
||||
|
||||
```
|
||||
struct Matrix {
|
||||
let rows: Int, columns: Int
|
||||
var grid: Double[]
|
||||
init(rows: Int, columns: Int) {
|
||||
self.rows = rows
|
||||
self.columns = columns
|
||||
grid = Array(count: rows * columns, repeatedValue: 0.0)
|
||||
}
|
||||
func indexIsValidForRow(row: Int, column: Int) -> Bool {
|
||||
return row >= 0 && row < rows && column >= 0 && column < columns
|
||||
}
|
||||
subscript(row: Int, column: Int) -> Double {
|
||||
get {
|
||||
assert(indexIsValidForRow(row, column: column), "Index out of range")
|
||||
return grid[(row * columns) + column]
|
||||
}
|
||||
set {
|
||||
assert(indexIsValidForRow(row, column: column), "Index out of range")
|
||||
grid[(row * columns) + columns] = newValue
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Matrix提供了一个两个入参的构造方法,入参分别是`rows`和`columns`,创建了一个足够容纳rows * columns个数的Double类型数组。为了存储,将数组的大小和数组每个元素初始值0.0,都传入数组的构造方法中来创建一个正确大小的新数组。关于数组的构造方法和析构方法请参考[Creating and Initializing an Array](#)。
|
||||
|
||||
你可以通过传入合适的row和column的数量来构造一个新的Matrix实例:
|
||||
|
||||
```
|
||||
var matrix = Matrix(rows: 2, columns: 2)
|
||||
```
|
||||
|
||||
上例中创建了一个新的两行两列的Matrix实例。在阅读顺序从左上到右下的Matrix实例中的数组实例grid是矩阵二维数组的扁平化存储:
|
||||
|
||||
```
|
||||
// 示意图
|
||||
grid = [0.0, 0.0, 0.0, 0.0]
|
||||
|
||||
col0 col1
|
||||
row0 [0.0, 0.0,
|
||||
row1 0.0, 0.0]
|
||||
```
|
||||
|
||||
将值赋给带有row和column下标的matrix实例表达式可以完成赋值操作,下标入参使用逗号分割
|
||||
|
||||
```
|
||||
matrix[0, 1] = 1.5
|
||||
matrix[1, 0] = 3.2
|
||||
```
|
||||
|
||||
上面两句话分别让matrix的右上值为1.5,坐下值为3.2:
|
||||
|
||||
```
|
||||
[0.0, 1.5,
|
||||
3.2, 0.0]
|
||||
```
|
||||
|
||||
Matrix下标的getter和setter中同时调用了下标入参的row和column是否有效的判断。为了方便进行断言,Matrix包含了一个名为indexIsValid的成员方法,用来确认入参的row或column值是否会造成数组越界:
|
||||
|
||||
```
|
||||
func indexIsValidForRow(row: Int, column: Int) -> Bool {
|
||||
return row >= 0 && row < rows && column >= 0 && column < columns
|
||||
}
|
||||
```
|
||||
|
||||
断言在下标越界时触发:
|
||||
|
||||
```
|
||||
let someValue = matrix[2, 2]
|
||||
// 断言将会触发,因为 [2, 2] 已经超过了matrix的最大长度
|
||||
```
|
||||
|
||||
> 译者:这里有个词Computed Properties 这里统一翻译为实例属性了 微软术语引擎里没有这个词
|
||||
|
||||
|
||||
@ -21,10 +21,452 @@ Swift使用自动引用计数(ARC)这一机制来跟踪和管理你的应用程
|
||||
|
||||
然而,当ARC收回和释放了正在被使用中的实例,该实例的属性和方法将不能再被访问和调用。实际上,如果你试图访问这个实例,你的应用程序很可能会崩溃。
|
||||
|
||||
为了确保使用中的实例不会被回收,ARC会跟踪和计算每一个类实例正在被多少属性,常量和变量所引用。哪怕实例的引用数为一,ARC都不会回收这个实例。
|
||||
为了确保使用中的实例不会被销毁,ARC会跟踪和计算每一个实例正在被多少属性,常量和变量所引用。哪怕实例的引用数为一,ARC都不会销毁这个实例。
|
||||
|
||||
为了使之成为可能,无论你将类实例赋值给属性,常量或者是变量,属性,常量或者变量,都会对此实例创建强引用。之所以称之为强引用,是因为它会将实例牢牢的保持住,只要强引用还在,实例是不允许被释放的。
|
||||
为了使之成为可能,无论你将实例赋值给属性,常量或者是变量,属性,常量或者变量,都会对此实例创建强引用。之所以称之为强引用,是因为它会将实例牢牢的保持住,只要强引用还在,实例是不允许被销毁的。
|
||||
|
||||
## 自动引用计数实践
|
||||
## 自动引用计数实战
|
||||
|
||||
下面的例子展示了自动引用计数的工作机制。例子以一个简单的Person类开始,并定义了一个叫name的常量属性:
|
||||
|
||||
class Person {
|
||||
let name: String
|
||||
|
||||
init(name: String) {
|
||||
self.name = name
|
||||
println("\(name) is being initialized")
|
||||
}
|
||||
|
||||
deinit {
|
||||
println("\(name) is being deinitialized")
|
||||
}
|
||||
}
|
||||
|
||||
Person类有一个构造函数,此构造函数为实例的name属性赋值并打印出信息,以表明初始化过程生效。Person类同时也拥有析构函数,同样会在实例被销毁的时候打印出信息。
|
||||
|
||||
接下来的代码片段定义了三个类型为Person?的变量,用来按照代码片段中的顺序,为新的Person实例建立多个引用。由于这些变量是被定义为可选类型(Person?,而不是Person),它们的值会被自动初始化为nil,目前还不会引用到Person类的实例。
|
||||
|
||||
var reference1: Person?
|
||||
var reference2: Person?
|
||||
var reference3: Person?
|
||||
|
||||
现在你可以创建Person类的新实例,并且将它赋值给三个变量其中的一个:
|
||||
|
||||
reference1 = Person(name: "John Appleseed")
|
||||
// prints "John Appleseed is being initialized”
|
||||
|
||||
应当注意到当你调用Person类的构造函数的时候,"John Appleseed is being initialized”会被打印出来。由此可以确定构造函数被执行。
|
||||
|
||||
由于Person类的新实例被赋值给了reference1变量,所以reference1到Person类的新实例之间建立了一个强引用。正是因为这个强引用,ARC会保证Person实例被保持在内存中不被销毁。
|
||||
|
||||
如果你将同样的Person实例也赋值给其他两个变量,该实例又会多出两个强引用:
|
||||
|
||||
reference2 = reference1
|
||||
reference3 = reference1
|
||||
|
||||
现在这个Person实例已经有三个强引用了。
|
||||
|
||||
如果你通过给两个变量赋值nil的方式断开两个强引用(包括最先的那个强引用),只留下一个强引用,Person实例不会被销毁:
|
||||
|
||||
reference2 = reference1
|
||||
reference3 = reference1
|
||||
|
||||
ARC会在第三个,也即最后一个强引用被断开的时候,销毁Person实例,这也意味着你不再使用这个Person实例:
|
||||
|
||||
reference3 = nil
|
||||
// prints "John Appleseed is being deinitialized"
|
||||
|
||||
## 类实例之间的循环强引用
|
||||
|
||||
在上面的例子中,ARC会跟踪你所新创建的Person实例的引用数量,并且会在Person实例不再被需要时销毁它。
|
||||
|
||||
然而,我们可能会写出这样的代码,一个类永远不会有0个强引用。这种情况发生在两个类实例互相保持对方的强引用,并让对方不被销毁。这就是所谓的循环强引用。
|
||||
|
||||
你可以通过定义类之间的关系为弱引用或者无主引用,以此替代强引用,从而解决循环强引用的问题。具体的过程在[解决类实例之间的循环强引用](http://numbbbbb.github.io/the-swift-programming-language-in-chinese/chapter2/16_Automatic_Reference_Counting.html#1)中有描述。不管怎样,在你学习怎样解决循环强引用之前,很有必要了解一下它是怎样产生的。
|
||||
|
||||
下面展示了一个不经意产生循环强引用的例子。例子定义了两个类:Person和Apartment,用来建模公寓和它其中的居民:
|
||||
|
||||
class Person {
|
||||
let name: String
|
||||
init(name: String) { self.name = name }
|
||||
var apartment: Apartment?
|
||||
deinit { println("\(name) is being deinitialized") }
|
||||
}
|
||||
|
||||
class Apartment {
|
||||
let number: Int
|
||||
init(number: Int) { self.number = number }
|
||||
var tenant: Person?
|
||||
deinit { println("Apartment #\(number) is being deinitialized") }
|
||||
}
|
||||
|
||||
每一个Person实例有一个类型为String,名字为name的属性,并有一个可选的初始化为nil的apartment属性。apartment属性是可选的,因为一个人并不总是拥有公寓。
|
||||
|
||||
类似的,每个Apartment实例有一个叫number,类型为Int的属性,并有一个可选的初始化为nil的tenant属性。tenant属性是可选的,因为一栋公寓并不总是有居民。
|
||||
|
||||
这两个类都定义了析构函数,用以在类实例被析构的时候输出信息。这让你能够知晓Person和Apartment的实例是否像预期的那样被销毁。
|
||||
|
||||
接下来的代码片段定义了两个可选类型的变量john和number73,并分别被设定为下面的Apartment和Person的实例。这两个变量都被初始化为nil,并为可选的:
|
||||
|
||||
var john: Person?
|
||||
var number73: Apartment?
|
||||
|
||||
现在你可以创建特定的Person和Apartment实例并将类实例赋值给john和number73变量:
|
||||
|
||||
john = Person(name: "John Appleseed")
|
||||
number73 = Apartment(number: 73)
|
||||
|
||||
在两个实例被创建和赋值后,下图表面了强引用的关系。变量john现在有一个指向Person实例的强引用,而变量number73有一个指向Apartment实例的强引用:
|
||||
|
||||

|
||||
|
||||
现在你能够将这两个实例关联在一起,这样人就能有公寓住了,而公寓也有了房客。注意感叹号是用来展开和访问可选变量john和number73中的实例,这样实例的属性才能被赋值:
|
||||
|
||||
john!.apartment = number73
|
||||
number73!.tenant = john
|
||||
|
||||
在将两个实例联系在一起之后,强引用的关系如图所示:
|
||||
|
||||

|
||||
|
||||
不幸的是,将这两个实例关联在一起之后,一个循环强引用被创建了。Person实例现在有了一个指向Apartment实例的强引用,而Apartment实例也有了一个指向Person实例的强引用。因此,当你断开john和number73变量所持有的强引用时,引用计数并不会降为0,实例也不会被ARC销毁:
|
||||
|
||||
john = nil
|
||||
number73 = nil
|
||||
|
||||
注意,当你把这两个变量设为nil时,没有任何一个析构函数被调用。强引用循环阻止了Person和Apartment类实例的销毁,并在你的应用程序中造成了内存泄漏。
|
||||
|
||||
在你将john和number73赋值为nil后,强引用关系如下图:
|
||||
|
||||

|
||||
|
||||
Person和Apartment实例之间的强引用关系保留了下来并且不会被断开。
|
||||
|
||||
##解决实例之间的循环强引用
|
||||
|
||||
Swift提供了两种办法用来解决你在使用类的属性时所遇到的循环强引用问题:弱引用(weak reference)和无主引用(unowned reference)。
|
||||
|
||||
弱引用和无主引用允许循环引用中的一个实例引用另外一个实例而不保持强引用。这样实例能够互相引用而不产生循环强引用。
|
||||
|
||||
对于生命周期中会变为nil的实例使用弱引用。相反的,对于初始化赋值后再也不会被赋值为nil的实例,使用无主引用。
|
||||
|
||||
## 弱引用
|
||||
|
||||
弱引用不会牢牢保持住引用的实例,并且不会阻止ARC销毁被引用的实例。这种行为阻止了引用变为循环强引用。声明属性或者变量时,在前面加上weak关键字表明这是一个弱引用。
|
||||
|
||||
在实例的生命周期中,如果某些时候引用没有值,那么弱引用可以阻止循环强引用。如果引用总是有值,则可以使用无主引用,在[无主引用](http://numbbbbb.github.io/the-swift-programming-language-in-chinese/chapter2/16_Automatic_Reference_Counting.html#2)中有描述。在上面Apartment的例子中,一个公寓的生命周期中,有时是没有“居民”的,因此适合使用弱引用来解决循环强引用。
|
||||
|
||||
> 注意: 弱引用必须被声明为变量,表明其值能在运行时被修改。弱引用不能被声明为常量。
|
||||
|
||||
因为弱引用可以没有值,你必须将每一个弱引用声明为可选类型。可选类型是在Swift语言中推荐的用来表示可能没有值的类型。
|
||||
|
||||
因为弱引用不会保持所引用的实例,即使引用存在,实例也有可能被销毁。因此,ARC会在引用的实例被销毁后自动将其赋值为nil。你可以像其他可选值一样,检查弱引用的值是否存在,你永远也不会遇到被销毁了而不存在的实例。
|
||||
|
||||
下面的例子跟上面Person和Apartment的例子一致,但是有一个重要的区别。这一次,Apartment的tenant属性被声明为弱引用:
|
||||
|
||||
class Person {
|
||||
let name: String
|
||||
init(name: String) { self.name = name }
|
||||
var apartment: Apartment?
|
||||
deinit { println("\(name) is being deinitialized") }
|
||||
}
|
||||
|
||||
class Apartment {
|
||||
let number: Int
|
||||
init(number: Int) { self.number = number }
|
||||
weak var tenant: Person?
|
||||
deinit { println("Apartment #\(number) is being deinitialized") }
|
||||
}
|
||||
|
||||
然后跟之前一样,建立两个变量(john和number73)之间的强引用,并关联两个实例:
|
||||
|
||||
var john: Person?
|
||||
var number73: Apartment?
|
||||
|
||||
john = Person(name: "John Appleseed")
|
||||
number73 = Apartment(number: 73)
|
||||
|
||||
john!.apartment = number73
|
||||
number73!.tenant = john
|
||||
|
||||
现在,两个关联在一起的实例的引用关系如下图所示:
|
||||
|
||||

|
||||
|
||||
Person实例依然保持对Apartment实例的强引用,但是Apartment实例只是对Person实例的弱引用。这意味着当你断开john变量所保持的强引用时,再也没有指向Person实例的强引用了:
|
||||
|
||||

|
||||
|
||||
由于再也没有指向Person实例的强引用,该实例会被销毁:
|
||||
|
||||
john = nil
|
||||
// prints "John Appleseed is being deinitialized"
|
||||
|
||||
唯一剩下的指向Apartment实例的强引用来自于变量number73。如果你断开这个强引用,再也没有指向Apartment实例的强引用了:
|
||||
|
||||

|
||||
|
||||
由于再也没有指向Apartment实例的强引用,该实例也会被销毁:
|
||||
|
||||
number73 = nil
|
||||
// prints "Apartment #73 is being deinitialized"
|
||||
|
||||
上面的两段代码展示了变量john和number73在被赋值为nil后,Person实例和Apartment实例的析构函数都打印出“销毁”的信息。这证明了引用循环被打破了。
|
||||
|
||||
##无主引用
|
||||
|
||||
和弱引用类似,无主引用不会牢牢保持住引用的实例。和弱引用不同的是,无主引用是永远有值的。因此,无主引用总是被定义为非可选类型(non-optional type)。你可以在声明属性或者变量时,在前面加上关键字unowned表示这是一个无主引用。
|
||||
|
||||
由于无主引用是非可选类型,你不需要在使用它的时候将它展开。无主引用总是可以被直接访问。不过ARC无法在实例被销毁后将无主引用设为nil,因为非可选类型的变量不允许被赋值为nil。
|
||||
|
||||
> 注意: 如果你试图在实例被销毁后,访问该实例的无主引用,会触发运行时错误。使用无主引用,你必须确保引用始终指向一个未销毁的实例。
|
||||
|
||||
> 还需要注意的是如果你试图访问实例已经被销毁的无主引用,程序会直接崩溃,而不会发生无法预期的行为。所以你应当避免这样的事情发生。
|
||||
|
||||
下面的例子定义了两个类,Customer和CreditCard,模拟了银行客户和客户的信用卡。这两个类中,每一个都将另外一个类的实例作为自身的属性。这种关系会潜在的创造循环强引用。
|
||||
|
||||
Customer和CreditCard之间的关系与前面弱引用例子中Apartment和Person的关系截然不同。在这个数据模型中,一个客户可能有或者没有信用卡,但是一张信用卡总是关联着一个客户。为了表示这种关系,Customer类有一个可选类型的card属性,但是CreditCard类有一个非可选类型的customer属性。
|
||||
|
||||
此外,只能通过将一个number值和customer实例传递给CreditCard构造函数的方式来创建CreditCard实例。这样可以确保当创建CreditCard实例时总是有一个customer实例与之关联。
|
||||
|
||||
由于信用卡总是关联着一个客户,因此将customer属性定义为无主引用,用以避免循环强引用:
|
||||
|
||||
class Customer {
|
||||
let name: String
|
||||
var card: CreditCard?
|
||||
init(name: String) {
|
||||
self.name = name
|
||||
}
|
||||
deinit { println("\(name) is being deinitialized") }
|
||||
}
|
||||
|
||||
class CreditCard {
|
||||
let number: Int
|
||||
unowned let customer: Customer
|
||||
init(number: Int, customer: Customer) {
|
||||
self.number = number
|
||||
self.customer = customer
|
||||
}
|
||||
deinit { println("Card #\(number) is being deinitialized") }
|
||||
}
|
||||
|
||||
下面的代码片段定义了一个叫john的可选类型Customer变量,用来保存某个特定客户的引用。由于是可选类型,所以变量被初始化为nil。
|
||||
|
||||
var john: Customer?
|
||||
|
||||
现在你可以创建Customer类的实例,用它初始化CreditCard实例,并将新创建的CreditCard实例赋值为客户的card属性。
|
||||
|
||||
john = Customer(name: "John Appleseed")
|
||||
john!.card = CreditCard(number: 1234_5678_9012_3456, customer: john!)
|
||||
|
||||
在你关联两个实例后,他们的引用关系如下图所示:
|
||||
|
||||

|
||||
|
||||
Customer实例持有对CreditCard实例的强引用,而CreditCard实例持有对Customer实例的无主引用。
|
||||
|
||||
由于customer的无主引用,当你断开john变量持有的强引用时,再也没有指向Customer实例的强引用了:
|
||||
|
||||

|
||||
|
||||
由于再也没有指向Customer实例的强引用,该实例被销毁了。其后,再也没有指向CreditCard实例的强引用,该实例也随之被销毁了:
|
||||
|
||||
john = nil
|
||||
// prints "John Appleseed is being deinitialized"
|
||||
// prints "Card #1234567890123456 is being deinitialized"
|
||||
|
||||
最后的代码展示了在john变量被设为nil后Customer实例和CreditCard实例的构造函数都打印出了“销毁”的信息。
|
||||
|
||||
##无主引用以及显式展开的可选属性
|
||||
|
||||
上面弱引用和无主引用的例子涵盖了两种常用的需要打破循环强引用的场景。
|
||||
|
||||
Person和Apartment的例子展示了两个属性的值都允许为nil,并会潜在的产生循环强引用。这种场景最适合用弱引用来解决。
|
||||
|
||||
Customer和CreditCard的例子展示了一个属性的值允许为nil,而另一个属性的值不允许为nil,并会潜在的产生循环强引用。这种场景最适合通过无主引用来解决。
|
||||
|
||||
然而,存在着第三种场景,在这种场景中,两个属性都必须有值,并且初始化完成后不能为nil。在这种场景中,需要一个类使用无主属性,而另外一个类使用显示展开的可选属性。
|
||||
|
||||
这使两个属性在初始化完成后能被直接访问(不需要可选展开),同事避免了循环引用。这一节将为你展示如何建立这种关系。
|
||||
|
||||
下面的例子定义了两个类,Country和City,每个类将另外一个类的实例保存为属性。在这个模型中,每个国家必须有首都,而每一个城市必须属于一个国家。为了实现这种关系,Country类拥有一个capitalCity属性,而City类有一个country属性:
|
||||
|
||||
class Country {
|
||||
let name: String
|
||||
let capitalCity: City!
|
||||
init(name: String, capitalName: String) {
|
||||
self.name = name
|
||||
self.capitalCity = City(name: capitalName, country: self)
|
||||
}
|
||||
}
|
||||
|
||||
class City {
|
||||
let name: String
|
||||
unowned let country: Country
|
||||
init(name: String, country: Country) {
|
||||
self.name = name
|
||||
self.country = country
|
||||
}
|
||||
}
|
||||
|
||||
为了建立两个类的依赖关系,City的构造函数有一个Country实例的参数,并且将实例保存为country属性。
|
||||
|
||||
Country的构造函数调用了City的构造函数。然而,只有Country的实例完全初始化完后,Country的构造函数才能把self传给City的构造函数。([在两阶段构造函数中有具体描述](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-XID_288))
|
||||
|
||||
为了满足这种需求,通过在类型结尾处加上感叹号(City!)的方式,将Country的capitalCity属性声明为显示展开的可选类型属性。这表示像其他可选类型一样,capitalCity属性的默认值为nil,但是不需要展开他的值就能访问它。([在显示展开的可选类型中有描述](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_436))
|
||||
|
||||
由于capitalCity默认值为nil,一旦Country的实例在构造函数中给name属性赋值后,整个初始化过程就完成了。这代表一旦name属性被后,Country的构造函数就能引用并传递显式的self。Country的构造函数在赋值capitalCity时,就能将self作为参数传递给City的构造函数。
|
||||
|
||||
以上的意义在于你可以通过一条语句同时创建Country和City的实例,而不产生循环强引用,并且capitalCity的属性能被直接访问,而不需要通过感叹号来展开它的可选值:
|
||||
|
||||
var country = Country(name: "Canada", capitalName: "Ottawa")
|
||||
println("\(country.name)'s capital city is called \(country.capitalCity.name)")
|
||||
// prints "Canada's capital city is called Ottawa"
|
||||
|
||||
在上面的例子中,使用显示展开可选值的意义在于满足了两个类构造函数的需求。capitalCity属性在初始化完成后,能作为非可选值使用同事还避免了循环强引用。
|
||||
|
||||
##闭包引起的循环强引用
|
||||
|
||||
前面我们看到了循环强引用环是在两个类实例属性互相保持对方的强引用时产生的,还知道了如何用弱引用和无主引用来打破循环强引用。
|
||||
|
||||
循环强引用还会发生在当你将一个闭包赋值给类实例的某个属性,并且这个闭包体中又使用了实例。这个闭包体中可能访问了实例的某个属性,例如self.someProperty,或者闭包中调用了实例的某个方法,例如self.someMethod。这两种情况都导致了闭包 “捕获" self,从而产生了循环强引用。
|
||||
|
||||
循环强引用的产生,是因为闭包和类相似,都是引用类型。当你把一个闭包赋值给某个属性时,你也把一个引用赋值给了这个闭包。实质上,这跟之前的问题是一样的-两个强引用让彼此一直有效。但是,和两个类实例不同,这次一个是类实例,另一个是闭包。
|
||||
|
||||
Swift提供了一种优雅的方法来解决这个问题,称之为闭包占用列表(closuer capture list)。同样的,在学习如何用闭包占用列表破坏循环强引用之前,先来了解一下循环强引用是如何产生的,这对我们是很有帮助的。
|
||||
|
||||
下面的例子为你展示了当一个闭包引用了self后是如何产生一个循环强引用的。例子中定义了一个叫HTMLElement的类,用一种简单的模型表示HTML中的一个单独的元素:
|
||||
|
||||
class HTMLElement {
|
||||
|
||||
let name: String
|
||||
let text: String?
|
||||
|
||||
@lazy var asHTML: () -> String = {
|
||||
if let text = self.text {
|
||||
return "<\(self.name)>\(text)</\(self.name)>"
|
||||
} else {
|
||||
return "<\(self.name) />"
|
||||
}
|
||||
}
|
||||
|
||||
init(name: String, text: String? = nil) {
|
||||
self.name = name
|
||||
self.text = text
|
||||
}
|
||||
|
||||
deinit {
|
||||
println("\(name) is being deinitialized")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
HTMLElement类定义了一个name属性来表示这个元素的名称,例如代表段落的"p",或者代表换行的"br"。HTMLElement还定义了一个可选属性text,用来设置和展现HTML元素的文本。
|
||||
|
||||
除了上面的两个属性,HTMLElement还定义了一个lazy属性asHTML。这个属性引用了一个闭包,将name和text组合成HTML字符串片段。该属性是() -> String类型,或者可以理解为“一个没有参数,返回String的函数”。
|
||||
|
||||
默认情况下,闭包赋值给了asHTML属性,这个闭包返回一个代表HTML标签的字符串。如果text值存在,该标签就包含可选值text;如果text不存在,该标签就不包含文本。对于段落元素,根据text是"some text"还是nil,闭包会返回"`<p>some text</p>`"或者"`<p />`"。
|
||||
|
||||
可以像实例方法那样去命名、使用asHTML属性。然而,由于asHTML是闭包而不是实例方法,如果你想改变特定元素的HTML处理的话,可以用自定义的闭包来取代默认值。
|
||||
|
||||
> 注意: asHTML声明为lazy属性,因为只有当元素确实需要处理为HTML输出的字符串时,才需要使用asHTML。也就是说,在默认的闭包中可以使用self,因为只有当初始化完成以及self确实存在后,才能访问lazy属性。
|
||||
|
||||
HTMLElement类只提供一个构造函数,通过name和text(如果有的话)参数来初始化一个元素。该类也定义了一个析构函数,当HTMLElement实例被销毁时,打印一条消息。
|
||||
|
||||
下面的代码展示了如何用HTMLElement类创建实例并打印消息。
|
||||
|
||||
var paragraph: HTMLElement? = HTMLElement(name: "p", text: "hello, world")
|
||||
println(paragraph!.asHTML())
|
||||
// prints"hello, world"
|
||||
|
||||
>注意: 上面的paragraph变量定义为可选HTMLElement,因此我们可以赋值nil给它来演示循环强引用。
|
||||
|
||||
不幸的是,上面写的HTMLElement类产生了类实例和asHTML默认值的闭包之间的循环强引用。循环强引用如下图所示:
|
||||
|
||||

|
||||
|
||||
实例的asHTML属性持有闭包的强引用。但是,闭包在其闭包体内使用了self(引用了self.name和self.text),因此闭包占有了self,这意味着闭包又反过来持有了HTMLElement实例的强引用。这样两个对象就产生了循环强引用。(更多关于闭包占有值的信息,请参考[Capturing Values](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html#//apple_ref/doc/uid/TP40014097-CH11-XID_129))。
|
||||
|
||||
>注意: 虽然闭包多次使用了self,它只占有HTMLElement实例的一个强引用。
|
||||
|
||||
如果设置paragraph变量为nil,打破它持有的HTMLElement实例的强引用,HTMLElement实例和它的闭包都不会被销毁,也是因为循环强引用:
|
||||
|
||||
paragraph = nil
|
||||
|
||||
注意HTMLElementdeinitializer中的消息并没有别打印,证明了HTMLElement实例并没有被销毁。
|
||||
|
||||
##解决闭包引起的循环强引用
|
||||
|
||||
在定义闭包时同时定义占有列表作为闭包的一部分,通过这种方式可以解决闭包和类实例之间的循环强引用。占有列表定义了闭包体内占有一个或者多个引用类型的规则。跟解决两个类实例间的循环强引用一样,声明每个占有的引用为弱引用或无主引用,而不是强引用。应当根据代码关系来决定使用弱引用还是无主引用。
|
||||
|
||||
>注意: Swift有如下要求:只要在闭包内使用self的成员,就要用self.someProperty或者self.someMethod(而不只是someProperty或someMethod)。这提醒你可能会不小心就占有了self。
|
||||
|
||||
##定义占有列表
|
||||
|
||||
占有列表中的每个元素都是由weak或者unowned关键字和实例的引用(如self或someInstance)成对组成。每一对都在花括号中,通过逗号分开。
|
||||
|
||||
占有列表放置在闭包参数列表和返回类型之前:
|
||||
|
||||
@lazy var someClosure: (Int, String) -> String = {
|
||||
[unowned self] (index: Int, stringToProcess: String) -> String in
|
||||
// closure body goes here
|
||||
}
|
||||
|
||||
如果闭包没有指定参数列表或者返回类型,则可以通过上下文推断,那么可以占有列表放在闭包开始的地方,跟着是关键字in:
|
||||
|
||||
@lazy var someClosure: () -> String = {
|
||||
[unowned self] in
|
||||
// closure body goes here
|
||||
}
|
||||
|
||||
##弱引用和无主引用
|
||||
|
||||
当闭包和占有的实例总是互相引用时并且总是同时销毁时,将闭包内的占有定义为无主引用。
|
||||
|
||||
相反的,当占有引用有时可能会是nil时,将闭包内的占有定义为弱引用。弱引用总是可选类型,并且当引用的实例被销毁后,弱引用的值会自动置为nil。这使我们可以在闭包内检查他们是否存在。
|
||||
|
||||
>注意: 如果占有的引用绝对不会置为nil,应该用无主引用,而不是弱引用。
|
||||
|
||||
前面的HTMLElement例子中,无主引用是正确的解决循环强引用的方法。这样这样编写HTMLElement类来避免循环强引用:
|
||||
|
||||
class HTMLElement {
|
||||
|
||||
let name: String
|
||||
let text: String?
|
||||
|
||||
@lazy var asHTML: () -> String = {
|
||||
[unowned self] in
|
||||
if let text = self.text {
|
||||
return "<\(self.name)>\(text)</\(self.name)>"
|
||||
} else {
|
||||
return "<\(self.name) />"
|
||||
}
|
||||
}
|
||||
|
||||
init(name: String, text: String? = nil) {
|
||||
self.name = name
|
||||
self.text = text
|
||||
}
|
||||
|
||||
deinit {
|
||||
println("\(name) is being deinitialized")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
上面的HTMLElement实现和之前的实现一致,只是在asHTML闭包中多了一个占有列表。这里,占有列表是[unowned self],表示“用无主引用而不是强引用来占有self”。
|
||||
|
||||
和之前一样,我们可以创建并打印HTMLElement实例:
|
||||
|
||||
var paragraph: HTMLElement? = HTMLElement(name: "p", text: "hello, world")
|
||||
println(paragraph!.asHTML())
|
||||
// prints "<p>hello, world</p>"
|
||||
|
||||
使用占有列表后引用关系如下图所示:
|
||||
|
||||

|
||||
|
||||
这一次,闭包以无主引用的形式占有self,并不会持有HTMLElement实例的强引用。如果将paragraph赋值为nil,HTMLElement实例将会被销毁,并能看到它的析构函数打印出的消息。
|
||||
|
||||
paragraph = nil
|
||||
// prints "p is being deinitialized"
|
||||
|
||||
@ -0,0 +1,115 @@
|
||||
# 特性
|
||||
|
||||
特性提供了关于声明和类型的更多信息。在Swift中有两类特性,用于修饰声明的以及用于修饰类型的。例如,`required`特性,当应用于一个类的指定或便利初始化器声明时,表明它的每个子类都必须实现那个初始化器。再比如`noreturn`特性,当应用于函数或方法类型时,表明该函数或方法不会返回到它的调用者。
|
||||
|
||||
通过以下方式指定一个特性:符号`@`后面跟特性名,如果包含参数,则把参数带上:
|
||||
|
||||
```
|
||||
@attribute name
|
||||
@attribute name(attribute arguments)
|
||||
```
|
||||
|
||||
有些声明特性通过接收参数来指定特性的更多信息以及它是如何修饰一个特定的声明的。这些特性的参数写在小括号内,它们的格式由它们所属的特性来定义。
|
||||
|
||||
## 声明特性
|
||||
|
||||
声明特性只能应用于声明。然而,你也可以将`noreturn`特性应用于函数或方法类型。
|
||||
|
||||
`assignment`
|
||||
|
||||
该特性用于修饰重载了复合赋值运算符的函数。重载了复合赋值运算符的函数必需将它们的初始输入参数标记为`inout`。如何使用`assignment`特性的一个例子,请见:[复合赋值运算符]()。
|
||||
|
||||
`class_protocol`
|
||||
|
||||
该特性用于修饰一个协议表明该协议只能被类类型采用[待改:adopted]。
|
||||
|
||||
如果你用`objc`特性修饰一个协议,`class_protocol`特性就会隐式地应用到该协议,因此无需显式地用`class_protocol`特性标记该协议。
|
||||
|
||||
`exported`
|
||||
|
||||
该特性用于修饰导入声明,以此来导出已导入的模块,子模块,或当前模块的声明。如果另一个模块导入了当前模块,那么那个模块可以访问当前模块的导出项。
|
||||
|
||||
`final`
|
||||
|
||||
该特性用于修饰一个类或类中的属性,方法,以及下标成员。如果用它修饰一个类,那么这个类则不能被继承。如果用它修饰类中的属性,方法或下标,则表示在子类中,它们不能被重写。
|
||||
|
||||
`lazy`
|
||||
|
||||
该特性用于修饰类或结构体中的存储型变量属性,表示该属性的初始值最多只被计算和存储一次,且发生在第一次访问它时。如何使用`lazy`特性的一个例子,请见:[惰性存储型属性]()。
|
||||
|
||||
`noreturn`
|
||||
|
||||
该特性用于修饰函数或方法声明,表明该函数或方法的对应类型,`T`,是`@noreturn T`。你可以用这个特性修饰函数或方法的类型,这样一来,函数或方法就不会返回到它的调用者中去。
|
||||
|
||||
对于一个没有用`noreturn`特性标记的函数或方法,你可以将它重写(override)为用该特性标记的。相反,对于一个已经用`noreturn`特性标记的函数或方法,你则不可以将它重写为没使用该特性标记的。相同的规则试用于当你在一个comforming类型中实现一个协议方法时。
|
||||
|
||||
`NSCopying`
|
||||
|
||||
该特性用于修饰一个类的存储型变量属性。该特性将使属性的setter与属性值的一个副本合成,由`copyWithZone`方法返回,而不是属性本身的值。该属性的类型必需遵循`NSCopying`协议。
|
||||
|
||||
`NSCopying`特性的行为与Objective-C中的`copy`特性相似。
|
||||
|
||||
`NSManaged`
|
||||
|
||||
该特性用于修饰`NSManagedObject`子类中的存储型变量属性,表明属性的存储和实现由Core Data在运行时基于相关实体描述动态提供。
|
||||
|
||||
`objc`
|
||||
|
||||
该特性用于修饰任意可以在Objective-C中表示的声明,比如,非嵌套类,协议,类和协议中的属性和方法(包含getter和setter),初始化器,析构器,以下下标。`objc`特性告诉编译器该声明可以在Objective-C代码中使用。
|
||||
|
||||
如果你将`objc`特性应用于一个类或协议,它也会隐式地应用于那个类或协议的成员。对于标记了`objc`特性的类,编译器会隐式地为它的子类添加`objc`特性。标记了`objc`特性的协议不能继承自没有标记`objc`的协议。
|
||||
|
||||
`objc`特性有一个可选的参数,由标记符组成。当你想把`objc`所修饰的实体以一个不同的名字暴露给Objective-C,你就可以使用这个特性参数。你可以使用这个参数来命名类,协议,方法,getters,setters,以及初始化器。下面的例子把`ExampleClass`中`enabled`属性的getter暴露给Objective-C,名字是`isEnabled`,而不是它原来的属性名。
|
||||
|
||||
```
|
||||
@objc
|
||||
class ExampleClass {
|
||||
var enabled: Bool {
|
||||
@objc(isEnabled) get {
|
||||
// Return the appropriate value
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`optional`
|
||||
|
||||
用该特性修饰协议的属性,方法或下标成员,表示实现这些成员并不需要一致性类型(conforming type)。
|
||||
|
||||
你只能用`optional`特性修饰那些标记了`objc`特性的协议。因此,只有类类型可以adopt和comform to那些包含可选成员需求的协议。更多关于如何使用`optional`特性以及如何访问可选协议成员的指导,例如,当你不确定一个conforming类型是否实现了它们,请见:[可选协议需求]()。
|
||||
|
||||
`required`
|
||||
|
||||
用该特性修饰一个类的指定或便利初始化器,表示该类的所有子类都必需实现该初始化器。
|
||||
|
||||
加了该特性的指定初始化器必需显式地实现,而便利初始化器既可显式地实现,也可以在子类实现了超类所有指定初始化器后继承而来(或者当子类使用便利初始化器重写了指定初始化器)。
|
||||
|
||||
### Interface Builder使用的声明特性
|
||||
|
||||
Interface Builder特性是Interface Builder用来与Xcode同步的声明特性。Swift提供了以下的Interface Builder特性:`IBAction`,`IBDesignable`,`IBInspectable`,以及`IBOutlet`。这些特性与Objective-C中对应的特性在概念上是相同的。
|
||||
|
||||
`IBOutlet`和`IBInspectable`用于修饰一个类的属性声明;`IBAction`特性用于修饰一个类的方法声明;`IBDesignable`用于修饰类的声明。
|
||||
|
||||
## 类型特性
|
||||
|
||||
类型特性只能用于修饰类型。然而,你也可以用`noreturn`特性去修饰函数或方法声明。
|
||||
|
||||
`auto_closure`
|
||||
|
||||
这个特性通过自动地将表达式封闭到一个无参数闭包中来延迟表达式的求值。使用该特性修饰无参的函数或方法类型,返回表达式的类型。一个如何使用`auto_closure`特性的例子,见[函数类型]()
|
||||
|
||||
`noreturn`
|
||||
|
||||
该特性用于修饰函数或方法的类型,表明该函数或方法不会返回到它的调用者中去。你也可以用它标记函数或方法的声明,表示函数或方法的相应类型,`T`,是`@noreturn T`。
|
||||
|
||||
> 特性的语法
|
||||
> attribute -> @ [attribute-name]() [attribute-argument-clause]()opt
|
||||
> attribute-name -> [identifier]()
|
||||
> attribute-argument-clause -> ( [balanced-tokens]()opt )
|
||||
> attributes -> [attribute]() [attributes]()opt
|
||||
> balanced-tokens -> [balanced-token]() [balanced-tokens]()opt
|
||||
> balanced-token -> ( [balanced-tokens]()opt )
|
||||
> balanced-token -> [ [balanced-tokens]()opt ]
|
||||
> balanced-token -> { [balanced-tokens]()opt }
|
||||
> balanced-token -> 任意标识符,关键字,字面量,或运算符
|
||||
> balanced-token -> 任意标点符号,除了(, ), [, ], {, 或 }
|
||||
|
||||
@ -1,57 +1,738 @@
|
||||
# 语法总结 (Summary of the Grammar)
|
||||
|
||||
## 语句
|
||||
`结构已搞定,内容已填充,翻译仍在进行中...`
|
||||
|
||||
> 语句
|
||||
*statement* → [*expression*]() ; opt
|
||||
*statement* → [*declaration*]() ; opt
|
||||
*statement* → [*loop-statement*]() ; opt
|
||||
*statement* → [*branch-statement*]() ; opt
|
||||
*statement* → [*labeled-statement*]()
|
||||
*statement* → [*control-transfer-statement*]() ; opt
|
||||
*statements* → [*statement statement*]() opt
|
||||
## 语句 (Statements)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 循环语句
|
||||
> *loop-statement* → [*for-statement*]()
|
||||
> *loop-statement* → [*for-in-statement*]()
|
||||
> *loop-statement* → [*while-statement*]()
|
||||
> *loop-statement* → [*do-while-statement*]()
|
||||
> 语句 (Grammar of a statement)
|
||||
> *statement* → [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) **;** _opt_
|
||||
> *statement* → [*declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration) **;** _opt_
|
||||
> *statement* → [*loop-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/loop-statement) **;** _opt_
|
||||
> *statement* → [*branch-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/branch-statement) **;** _opt_
|
||||
> *statement* → [*labeled-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/labeled-statement)
|
||||
> *statement* → [*control-transfer-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/control-transfer-statement) **;** _opt_
|
||||
> *statements* → [*statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement) [*statements*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements) _opt_
|
||||
|
||||
<p></p>
|
||||
|
||||
> for 循环
|
||||
> *for-statement* → **for** [*for-init*]() opt ; [*expression*]() opt ; [*expression*]() opt [*code-block*]()
|
||||
> *for-statement* → **for** ( [*for-init*]() opt ; [*expression*]() opt ; [*expression*]() opt ) [*code-block*]()
|
||||
> *for-init* → [*variable-declaration*]() | [*expression-list*]()
|
||||
> 循环语句 (Grammar of a loop statement)
|
||||
> *loop-statement* → [*for-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-statement)
|
||||
> *loop-statement* → [*for-in-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-in-statement)
|
||||
> *loop-statement* → [*while-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/wheetatype类型ile-statement)
|
||||
> *loop-statement* → [*do-while-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/do-while-statement)
|
||||
|
||||
<p></p>
|
||||
|
||||
> for-in 循环
|
||||
> *for-in-statement* → **for** [*pattern*]() **in** [*expression*]() [*code-block*]()
|
||||
> For 循环 (Grammar of a for statement)
|
||||
> *for-statement* → **for** [*for-init*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-init) _opt_ **;** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) _opt_ **;** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) _opt_ [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block)
|
||||
> *for-statement* → **for** **(** [*for-init*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-init) _opt_ **;** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) _opt_ **;** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) _opt_ **)** [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block)
|
||||
> *for-init* → [*variable-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-declaration) | [*expression-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression-list)
|
||||
|
||||
<p></p>
|
||||
|
||||
> while 循环
|
||||
> *while-statement* → **while** [*while-condition*]() [*code-block*]()
|
||||
> *while-condition* → [*expression*]() | [*declaration*]()
|
||||
> For-In 循环 (Grammar of a for-in statement)
|
||||
> *for-in-statement* → **for** [*pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern) **in** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block)
|
||||
|
||||
<p></p>
|
||||
|
||||
> do-while 循环
|
||||
> *do-while-statement* → **do** [*code-block*]() **while** [*while-condition*]()
|
||||
> While 循环 (Grammar of a while statement)
|
||||
> *while-statement* → **while** [*while-condition*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-condition) [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block)
|
||||
> *while-condition* → [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) | [*declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 判断语句(Branch Statement)
|
||||
> *branch-statement* → [*if-statement*]()
|
||||
> *branch-statement* → [*switch-statement*]()
|
||||
> Do-While 循环 (Grammar of a do-while statement)
|
||||
> *do-while-statement* → **do** [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block) **while** [*while-condition*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-condition)
|
||||
|
||||
<p></p>
|
||||
|
||||
> if 语句
|
||||
> *if-statement* → **if** [*if-condition*]() [*code-block*]() [*else-clause*]() opt
|
||||
> *if-condition* → [*expression*]() | [*declaration*]()
|
||||
> *else-clause* → **else** [*code-block*]() | **else** [*if-statement*]()
|
||||
> 分支语句 (Grammar of a branch statement)
|
||||
> *branch-statement* → [*if-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-statement)
|
||||
> *branch-statement* → [*switch-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-statement)
|
||||
|
||||
<p></p>
|
||||
|
||||
> If 语句 (Grammar of an if statement)
|
||||
> *if-statement* → **if** [*if-condition*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-condition) [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block) [*else-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/else-clause) _opt_
|
||||
> *if-condition* → [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) | [*declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration)
|
||||
> *else-clause* → **else** [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block) | **else** [*if-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-statement)
|
||||
|
||||
<p></p>
|
||||
|
||||
> Switch 语句 (Grammar of a switch statement)
|
||||
> *switch-statement* → **switch** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) **{** [*switch-cases*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-cases) _opt_ **}**
|
||||
> *switch-cases* → [*switch-case*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-case) [*switch-cases*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-cases) _opt_
|
||||
> *switch-case* → [*case-label*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-label) [*statements*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements) | [*default-label*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/default-label) [*statements*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements)
|
||||
> *switch-case* → [*case-label*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-label) **;** | [*default-label*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/default-label) **;**
|
||||
> *case-label* → **case** [*case-item-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-item-list) **:**
|
||||
> *case-item-list* → [*pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern) [*guard-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-clause) _opt_ | [*pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern) [*guard-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-clause) _opt_ **,** [*case-item-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-item-list)
|
||||
> *default-label* → **default** **:**
|
||||
> *guard-clause* → **where** [*guard-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-expression)
|
||||
> *guard-expression* → [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 带标签的语句 (Grammar of a labeled statement)
|
||||
> *labeled-statement* → [*statement-label*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement-label) [*loop-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/loop-statement) | [*statement-label*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement-label) [*switch-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-statement)
|
||||
> *statement-label* → [*label-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name) **:**
|
||||
> *label-name* → [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 控制传递语句 (Grammar of a control transfer statement)
|
||||
> *control-transfer-statement* → [*break-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/break-statement)
|
||||
> *control-transfer-statement* → [*continue-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/continue-statement)
|
||||
> *control-transfer-statement* → [*fallthrough-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/fallthrough-statement)
|
||||
> *control-transfer-statement* → [*return-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/return-statement)
|
||||
|
||||
<p></p>
|
||||
|
||||
> Break 语句 (Grammar of a break statement)
|
||||
> *break-statement* → **break** [*label-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name) _opt_
|
||||
|
||||
<p></p>
|
||||
|
||||
> Continue 语句 (Grammar of a continue statement)
|
||||
> *continue-statement* → **continue** [*label-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name) _opt_
|
||||
|
||||
<p></p>
|
||||
|
||||
> Fallthrough 语句 (Grammar of a fallthrough statement)
|
||||
> *fallthrough-statement* → **fallthrough**
|
||||
|
||||
<p></p>
|
||||
|
||||
> Return 语句 (Grammar of a return statement)
|
||||
> *return-statement* → **return** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) _opt_
|
||||
|
||||
## 泛型参数 (Generic Parameters and Arguments)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 泛型形参子句 (Grammar of a generic parameter clause)
|
||||
> *generic-parameter-clause* → **<** [*generic-parameter-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-parameter-list) [*requirement-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/requirement-clause) _opt_ **>**
|
||||
> *generic-parameter-list* → [*generic-parameter*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-parameter) | [*generic-parameter*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-parameter) **,** [*generic-parameter-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-parameter-list)
|
||||
> *generic-parameter* → [*type-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-name)
|
||||
> *generic-parameter* → [*type-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-name) **:** [*type-identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-identifier)
|
||||
> *generic-parameter* → [*type-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-name) **:** [*protocol-composition-type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/protocol-composition-type)
|
||||
> *requirement-clause* → **where** [*requirement-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/requirement-list)
|
||||
> *requirement-list* → [*requirement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/requirement) | [*requirement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/requirement) **,** [*requirement-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/requirement-list)
|
||||
> *requirement* → [*conformance-requirement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/conformance-requirement) | [*same-type-requirement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/same-type-requirement)
|
||||
> *conformance-requirement* → [*type-identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-identifier) **:** [*type-identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-identifier)
|
||||
> *conformance-requirement* → [*type-identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-identifier) **:** [*protocol-composition-type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/protocol-composition-type)
|
||||
> *same-type-requirement* → [*type-identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-identifier) **==** [*type-identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-identifier)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 泛型参数子句 (Grammar of a generic argument clause)
|
||||
> *generic-argument-clause* → **<** [*generic-argument-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-argument-list) **>**
|
||||
> *generic-argument-list* → [*generic-argument*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-argument) | [*generic-argument*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-argument) **,** [*generic-argument-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-argument-list)
|
||||
> *generic-argument* → [*type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type)
|
||||
|
||||
## 声明 (Declarations)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 声明语法 (Grammar of a declaration)
|
||||
> *declaration* → [*import-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/import-declaration)
|
||||
> *declaration* → [*constant-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/constant-declaration)
|
||||
> *declaration* → [*variable-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-declaration)
|
||||
> *declaration* → [*typealias-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/typealias-declaration)
|
||||
> *declaration* → [*function-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-declaration)
|
||||
> *declaration* → [*enum-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/enum-declaration)
|
||||
> *declaration* → [*struct-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/struct-declaration)
|
||||
> *declaration* → [*class-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/class-declaration)
|
||||
> *declaration* → [*protocol-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/protocol-declaration)
|
||||
> *declaration* → [*initializer-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/initializer-declaration)
|
||||
> *declaration* → [*deinitializer-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/deinitializer-declaration)
|
||||
> *declaration* → [*extension-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/extension-declaration)
|
||||
> *declaration* → [*subscript-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/subscript-declaration)
|
||||
> *declaration* → [*operator-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/operator-declaration)
|
||||
> *declarations* → [*declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration) [*declarations*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declarations) _opt_
|
||||
> *declaration-specifiers* → [*declaration-specifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration-specifier) [*declaration-specifiers*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration-specifiers) _opt_
|
||||
> *declaration-specifier* → **class** | **mutating** | **nonmutating** | **override** | **static** | **unowned** | **unowned(safe)** | **unowned(unsafe)** | **weak**
|
||||
|
||||
<p></p>
|
||||
|
||||
> (Grammar of a top-level declaration)
|
||||
> *top-level-declaration* → [*statements*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements) _opt_
|
||||
|
||||
<p></p>
|
||||
|
||||
> 代码块 (Grammar of a code block)
|
||||
> *code-block* → **{** [*statements*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements) _opt_ **}**
|
||||
|
||||
<p></p>
|
||||
|
||||
> (Grammar of an import declaration)
|
||||
> *import-declaration* → [*attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes) _opt_ **import** [*import-kind*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/import-kind) _opt_ [*import-path*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/import-path)
|
||||
> *import-kind* → **typealias** | **struct** | **class** | **enum** | **protocol** | **var** | **func**
|
||||
> *import-path* → [*import-path-identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/import-path-identifier) | [*import-path-identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/import-path-identifier) **.** [*import-path*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/import-path)
|
||||
> *import-path-identifier* → [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier) | [*operator*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/operator)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 常数声明 (Grammar of a constant declaration)
|
||||
> *constant-declaration* → [*attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes) _opt_ [*declaration-specifiers*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration-specifiers) _opt_ **let** [*pattern-initializer-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/pattern-initializer-list)
|
||||
> *pattern-initializer-list* → [*pattern-initializer*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/pattern-initializer) | [*pattern-initializer*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/pattern-initializer) **,** [*pattern-initializer-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/pattern-initializer-list)
|
||||
> *pattern-initializer* → [*pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern) [*initializer*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/initializer) _opt_
|
||||
> *initializer* → **=** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 变量声明 (Grammar of a variable declaration)
|
||||
> *variable-declaration* → [*variable-declaration-head*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-declaration-head) [*pattern-initializer-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/pattern-initializer-list)
|
||||
> *variable-declaration* → [*variable-declaration-head*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-declaration-head) [*variable-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-name) [*type-annotation*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-annotation) [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block)
|
||||
> *variable-declaration* → [*variable-declaration-head*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-declaration-head) [*variable-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-name) [*type-annotation*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-annotation) [*getter-setter-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/getter-setter-block)
|
||||
> *variable-declaration* → [*variable-declaration-head*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-declaration-head) [*variable-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-name) [*type-annotation*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-annotation) [*getter-setter-keyword-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/getter-setter-keyword-block)
|
||||
> *variable-declaration* → [*variable-declaration-head*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-declaration-head) [*variable-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-name) [*type-annotation*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-annotation) [*initializer*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/initializer) _opt_ [*willSet-didSet-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/willSet-didSet-block)
|
||||
> *variable-declaration-head* → [*attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes) _opt_ [*declaration-specifiers*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration-specifiers) _opt_ **var**
|
||||
> *variable-name* → [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier)
|
||||
> *getter-setter-block* → **{** [*getter-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/getter-clause) [*setter-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/setter-clause) _opt_ **}**
|
||||
> *getter-setter-block* → **{** [*setter-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/setter-clause) [*getter-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/getter-clause) **}**
|
||||
> *getter-clause* → [*attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes) _opt_ **get** [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block)
|
||||
> *setter-clause* → [*attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes) _opt_ **set** [*setter-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/setter-name) _opt_ [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block)
|
||||
> *setter-name* → **(** [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier) **)**
|
||||
> *getter-setter-keyword-block* → **{** [*getter-keyword-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/getter-keyword-clause) [*setter-keyword-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/setter-keyword-clause) _opt_ **}**
|
||||
> *getter-setter-keyword-block* → **{** [*setter-keyword-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/setter-keyword-clause) [*getter-keyword-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/getter-keyword-clause) **}**
|
||||
> *getter-keyword-clause* → [*attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes) _opt_ **get**
|
||||
> *setter-keyword-clause* → [*attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes) _opt_ **set**
|
||||
> *willSet-didSet-block* → **{** [*willSet-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/willSet-clause) [*didSet-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/didSet-clause) _opt_ **}**
|
||||
> *willSet-didSet-block* → **{** [*didSet-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/didSet-clause) [*willSet-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/willSet-clause) **}**
|
||||
> *willSet-clause* → [*attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes) _opt_ **willSet** [*setter-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/setter-name) _opt_ [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block)
|
||||
> *didSet-clause* → [*attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes) _opt_ **didSet** [*setter-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/setter-name) _opt_ [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 类型别名声明 (Grammar of a type alias declaration)
|
||||
> *typealias-declaration* → [*typealias-head*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/typealias-head) [*typealias-assignment*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/typealias-assignment)
|
||||
> *typealias-head* → **typealias** [*typealias-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/typealias-name)
|
||||
> *typealias-name* → [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier)
|
||||
> *typealias-assignment* → **=** [*type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 函数声明 (Grammar of a function declaration)
|
||||
> *function-declaration* → [*function-head*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-head) [*function-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-name) [*generic-parameter-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-parameter-clause) _opt_ [*function-signature*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-signature) [*function-body*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-body)
|
||||
> *function-head* → [*attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes) _opt_ [*declaration-specifiers*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration-specifiers) _opt_ **func**
|
||||
> *function-name* → [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier) | [*operator*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/operator)
|
||||
> *function-signature* → [*parameter-clauses*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter-clauses) [*function-result*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-result) _opt_
|
||||
> *function-result* → **->** [*attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes) _opt_ [*type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type)
|
||||
> *function-body* → [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block)
|
||||
> *parameter-clauses* → [*parameter-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter-clause) [*parameter-clauses*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter-clauses) _opt_
|
||||
> *parameter-clause* → **(** **)** | **(** [*parameter-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter-list) **...** _opt_ **)**
|
||||
> *parameter-list* → [*parameter*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter) | [*parameter*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter) **,** [*parameter-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter-list)
|
||||
> *parameter* → **inout** _opt_ **let** _opt_ **#** _opt_ [*parameter-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter-name) [*local-parameter-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/local-parameter-name) _opt_ [*type-annotation*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-annotation) [*default-argument-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/default-argument-clause) _opt_
|
||||
> *parameter* → **inout** _opt_ **var** **#** _opt_ [*parameter-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter-name) [*local-parameter-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/local-parameter-name) _opt_ [*type-annotation*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-annotation) [*default-argument-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/default-argument-clause) _opt_
|
||||
> *parameter* → [*attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes) _opt_ [*type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type)
|
||||
> *parameter-name* → [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier) | **_**
|
||||
> *local-parameter-name* → [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier) | **_**
|
||||
> *default-argument-clause* → **=** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 枚举声明 (Grammar of an enumeration declaration)
|
||||
> *enum-declaration* → [*attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes) _opt_ [*union-style-enum*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/union-style-enum) | [*attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes) _opt_ [*raw-value-style-enum*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/raw-value-style-enum)
|
||||
> *union-style-enum* → [*enum-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/enum-name) [*generic-parameter-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-parameter-clause) _opt_ **{** [*union-style-enum-members*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/union-style-enum-members) _opt_ **}**
|
||||
> *union-style-enum-members* → [*union-style-enum-member*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/union-style-enum-member) [*union-style-enum-members*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/union-style-enum-members) _opt_
|
||||
> *union-style-enum-member* → [*declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration) | [*union-style-enum-case-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/union-style-enum-case-clause)
|
||||
> *union-style-enum-case-clause* → [*attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes) _opt_ **case** [*union-style-enum-case-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/union-style-enum-case-list)
|
||||
> *union-style-enum-case-list* → [*union-style-enum-case*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/union-style-enum-case) | [*union-style-enum-case*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/union-style-enum-case) **,** [*union-style-enum-case-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/union-style-enum-case-list)
|
||||
> *union-style-enum-case* → [*enum-case-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/enum-case-name) [*tuple-type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/tuple-type) _opt_
|
||||
> *enum-name* → [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier)
|
||||
> *enum-case-name* → [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier)
|
||||
> *raw-value-style-enum* → [*enum-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/enum-name) [*generic-parameter-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-parameter-clause) _opt_ **:** [*type-identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-identifier) **{** [*raw-value-style-enum-members*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/raw-value-style-enum-members) _opt_ **}**
|
||||
> *raw-value-style-enum-members* → [*raw-value-style-enum-member*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/raw-value-style-enum-member) [*raw-value-style-enum-members*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/raw-value-style-enum-members) _opt_
|
||||
> *raw-value-style-enum-member* → [*declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration) | [*raw-value-style-enum-case-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/raw-value-style-enum-case-clause)
|
||||
> *raw-value-style-enum-case-clause* → [*attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes) _opt_ **case** [*raw-value-style-enum-case-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/raw-value-style-enum-case-list)
|
||||
> *raw-value-style-enum-case-list* → [*raw-value-style-enum-case*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/raw-value-style-enum-case) | [*raw-value-style-enum-case*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/raw-value-style-enum-case) **,** [*raw-value-style-enum-case-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/raw-value-style-enum-case-list)
|
||||
> *raw-value-style-enum-case* → [*enum-case-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/enum-case-name) [*raw-value-assignment*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/raw-value-assignment) _opt_
|
||||
> *raw-value-assignment* → **=** [*literal*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/literal)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 结构体声明 (Grammar of a structure declaration)
|
||||
> *struct-declaration* → [*attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes) _opt_ **struct** [*struct-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/struct-name) [*generic-parameter-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-parameter-clause) _opt_ [*type-inheritance-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-inheritance-clause) _opt_ [*struct-body*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/struct-body)
|
||||
> *struct-name* → [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier)
|
||||
> *struct-body* → **{** [*declarations*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declarations) _opt_ **}**
|
||||
|
||||
<p></p>
|
||||
|
||||
> 类声明 (Grammar of a class declaration)
|
||||
> *class-declaration* → [*attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes) _opt_ **class** [*class-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/class-name) [*generic-parameter-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-parameter-clause) _opt_ [*type-inheritance-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-inheritance-clause) _opt_ [*class-body*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/class-body)
|
||||
> *class-name* → [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier)
|
||||
> *class-body* → **{** [*declarations*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declarations) _opt_ **}**
|
||||
|
||||
<p></p>
|
||||
|
||||
> 协议声明 (Grammar of a protocol declaration)
|
||||
> *protocol-declaration* → [*attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes) _opt_ **protocol** [*protocol-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/protocol-name) [*type-inheritance-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-inheritance-clause) _opt_ [*protocol-body*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/protocol-body)
|
||||
> *protocol-name* → [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier)
|
||||
> *protocol-body* → **{** [*protocol-member-declarations*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/protocol-member-declarations) _opt_ **}**
|
||||
> *protocol-member-declaration* → [*protocol-property-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/protocol-property-declaration)
|
||||
> *protocol-member-declaration* → [*protocol-method-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/protocol-method-declaration)
|
||||
> *protocol-member-declaration* → [*protocol-initializer-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/protocol-initializer-declaration)
|
||||
> *protocol-member-declaration* → [*protocol-subscript-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/protocol-subscript-declaration)
|
||||
> *protocol-member-declaration* → [*protocol-associated-type-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/protocol-associated-type-declaration)
|
||||
> *protocol-member-declarations* → [*protocol-member-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/protocol-member-declaration) [*protocol-member-declarations*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/protocol-member-declarations) _opt_
|
||||
|
||||
<p></p>
|
||||
|
||||
> 协议属性声明 (Grammar of a protocol property declaration)
|
||||
> *protocol-property-declaration* → [*variable-declaration-head*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-declaration-head) [*variable-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-name) [*type-annotation*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-annotation) [*getter-setter-keyword-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/getter-setter-keyword-block)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 协议方法声明 (Grammar of a protocol method declaration)
|
||||
> *protocol-method-declaration* → [*function-head*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-head) [*function-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-name) [*generic-parameter-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-parameter-clause) _opt_ [*function-signature*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-signature)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 协议构造函数声明 (Grammar of a protocol initializer declaration)
|
||||
> *protocol-initializer-declaration* → [*initializer-head*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/initializer-head) [*generic-parameter-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-parameter-clause) _opt_ [*parameter-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter-clause)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 协议附属脚本声明 (Grammar of a protocol subscript declaration)
|
||||
> *protocol-subscript-declaration* → [*subscript-head*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/subscript-head) [*subscript-result*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/subscript-result) [*getter-setter-keyword-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/getter-setter-keyword-block)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 协议关联类型声明 (Grammar of a protocol associated type declaration)
|
||||
> *protocol-associated-type-declaration* → [*typealias-head*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/typealias-head) [*type-inheritance-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-inheritance-clause) _opt_ [*typealias-assignment*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/typealias-assignment) _opt_
|
||||
|
||||
<p></p>
|
||||
|
||||
> 构造函数声明 (Grammar of an initializer declaration)
|
||||
> *initializer-declaration* → [*initializer-head*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/initializer-head) [*generic-parameter-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-parameter-clause) _opt_ [*parameter-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter-clause) [*initializer-body*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/initializer-body)
|
||||
> *initializer-head* → [*attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes) _opt_ **convenience** _opt_ **init**
|
||||
> *initializer-body* → [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 析构函数声明 (Grammar of a deinitializer declaration)
|
||||
> *deinitializer-declaration* → [*attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes) _opt_ **deinit** [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 扩展声明 (Grammar of an extension declaration)
|
||||
> *extension-declaration* → **extension** [*type-identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-identifier) [*type-inheritance-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-inheritance-clause) _opt_ [*extension-body*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/extension-body)
|
||||
> *extension-body* → **{** [*declarations*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declarations) _opt_ **}**
|
||||
|
||||
<p></p>
|
||||
|
||||
> 附属脚本声明 (Grammar of a subscript declaration)
|
||||
> *subscript-declaration* → [*subscript-head*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/subscript-head) [*subscript-result*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/subscript-result) [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block)
|
||||
> *subscript-declaration* → [*subscript-head*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/subscript-head) [*subscript-result*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/subscript-result) [*getter-setter-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/getter-setter-block)
|
||||
> *subscript-declaration* → [*subscript-head*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/subscript-head) [*subscript-result*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/subscript-result) [*getter-setter-keyword-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/getter-setter-keyword-block)
|
||||
> *subscript-head* → [*attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes) _opt_ **subscript** [*parameter-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter-clause)
|
||||
> *subscript-result* → **->** [*attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes) _opt_ [*type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 运算符声明 (Grammar of an operator declaration)
|
||||
> *operator-declaration* → [*prefix-operator-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/prefix-operator-declaration) | [*postfix-operator-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/postfix-operator-declaration) | [*infix-operator-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/infix-operator-declaration)
|
||||
> *prefix-operator-declaration* → **operator** **prefix** [*operator*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/operator) **{** **}**
|
||||
> *postfix-operator-declaration* → **operator** **postfix** [*operator*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/operator) **{** **}**
|
||||
> *infix-operator-declaration* → **operator** **infix** [*operator*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/operator) **{** [*infix-operator-attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/infix-operator-attributes) _opt_ **}**
|
||||
> *infix-operator-attributes* → [*precedence-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/precedence-clause) _opt_ [*associativity-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/associativity-clause) _opt_
|
||||
> *precedence-clause* → **precedence** [*precedence-level*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/precedence-level)
|
||||
> *precedence-level* → Digit 0 through 255
|
||||
> *associativity-clause* → **associativity** [*associativity*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/associativity)
|
||||
> *associativity* → **left** | **right** | **none**
|
||||
|
||||
## 模式 (Patterns)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 模式语法 (Grammar of a pattern)
|
||||
> *pattern* → [*wildcard-pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/wildcard-pattern) [*type-annotation*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-annotation) _opt_
|
||||
> *pattern* → [*identifier-pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/identifier-pattern) [*type-annotation*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-annotation) _opt_
|
||||
> *pattern* → [*value-binding-pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/value-binding-pattern)
|
||||
> *pattern* → [*tuple-pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/tuple-pattern) [*type-annotation*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-annotation) _opt_
|
||||
> *pattern* → [*enum-case-pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/enum-case-pattern)
|
||||
> *pattern* → [*type-casting-pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/type-casting-pattern)
|
||||
> *pattern* → [*expression-pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/expression-pattern)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 通配符模式 (Grammar of a wildcard pattern)
|
||||
> *wildcard-pattern* → **_**
|
||||
|
||||
<p></p>
|
||||
|
||||
> 标识符模式 (Grammar of an identifier pattern)
|
||||
> *identifier-pattern* → [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 值绑定模式 (Grammar of a value-binding pattern)
|
||||
> *value-binding-pattern* → **var** [*pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern) | **let** [*pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 元组模式 (Grammar of a tuple pattern)
|
||||
> *tuple-pattern* → **(** [*tuple-pattern-element-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/tuple-pattern-element-list) _opt_ **)**
|
||||
> *tuple-pattern-element-list* → [*tuple-pattern-element*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/tuple-pattern-element) | [*tuple-pattern-element*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/tuple-pattern-element) **,** [*tuple-pattern-element-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/tuple-pattern-element-list)
|
||||
> *tuple-pattern-element* → [*pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 枚举用例模式 (Grammar of an enumeration case pattern)
|
||||
> *enum-case-pattern* → [*type-identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-identifier) _opt_ **.** [*enum-case-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/enum-case-name) [*tuple-pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/tuple-pattern) _opt_
|
||||
|
||||
<p></p>
|
||||
|
||||
> 类型转换模式 (Grammar of a type casting pattern)
|
||||
> *type-casting-pattern* → [*is-pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/is-pattern) | [*as-pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/as-pattern)
|
||||
> *is-pattern* → **is** [*type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type)
|
||||
> *as-pattern* → [*pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern) **as** [*type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 表达式模式 (Grammar of an expression pattern)
|
||||
> *expression-pattern* → [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression)
|
||||
|
||||
## 特性 (Attributes)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 特性语法 (Grammar of an attribute)
|
||||
> *attribute* → **@** [*attribute-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attribute-name) [*attribute-argument-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attribute-argument-clause) _opt_
|
||||
> *attribute-name* → [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier)
|
||||
> *attribute-argument-clause* → **(** [*balanced-tokens*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/balanced-tokens) _opt_ **)**
|
||||
> *attributes* → [*attribute*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attribute) [*attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes) _opt_
|
||||
> *balanced-tokens* → [*balanced-token*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/balanced-token) [*balanced-tokens*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/balanced-tokens) _opt_
|
||||
> *balanced-token* → **(** [*balanced-tokens*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/balanced-tokens) _opt_ **)**
|
||||
> *balanced-token* → **[** [*balanced-tokens*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/balanced-tokens) _opt_ **]**
|
||||
> *balanced-token* → **{** [*balanced-tokens*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/balanced-tokens) _opt_ **}**
|
||||
> *balanced-token* → Any identifier, keyword, literal, or operator
|
||||
> *balanced-token* → Any punctuation except (, ), [, ], {, or }
|
||||
|
||||
## 表达式 (Expressions)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 表达式语法 (Grammar of an expression)
|
||||
> *expression* → [*prefix-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/prefix-expression) [*binary-expressions*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/binary-expressions) _opt_
|
||||
> *expression-list* → [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) | [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) **,** [*expression-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression-list)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 前缀表达式 (Grammar of a prefix expression)
|
||||
> *prefix-expression* → [*prefix-operator*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/prefix-operator) _opt_ [*postfix-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/postfix-expression)
|
||||
> *prefix-expression* → [*in-out-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/in-out-expression)
|
||||
> *in-out-expression* → **&** [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 二进制表达式 (Grammar of a binary expression)
|
||||
> *binary-expression* → [*binary-operator*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/binary-operator) [*prefix-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/prefix-expression)
|
||||
> *binary-expression* → [*assignment-operator*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/assignment-operator) [*prefix-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/prefix-expression)
|
||||
> *binary-expression* → [*conditional-operator*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/conditional-operator) [*prefix-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/prefix-expression)
|
||||
> *binary-expression* → [*type-casting-operator*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/type-casting-operator)
|
||||
> *binary-expressions* → [*binary-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/binary-expression) [*binary-expressions*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/binary-expressions) _opt_
|
||||
|
||||
<p></p>
|
||||
|
||||
> 赋值运算符 (Grammar of an assignment operator)
|
||||
> *assignment-operator* → **=**
|
||||
|
||||
<p></p>
|
||||
|
||||
> 条件运算符 (Grammar of a conditional operator)
|
||||
> *conditional-operator* → **?** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) **:**
|
||||
|
||||
<p></p>
|
||||
|
||||
> 类型转换运算符 (Grammar of a type-casting operator)
|
||||
> *type-casting-operator* → **is** [*type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type) | **as** **?** _opt_ [*type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 主表达式 (Grammar of a primary expression)
|
||||
> *primary-expression* → [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier) [*generic-argument-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-argument-clause) _opt_
|
||||
> *primary-expression* → [*literal-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/literal-expression)
|
||||
> *primary-expression* → [*self-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/self-expression)
|
||||
> *primary-expression* → [*superclass-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/superclass-expression)
|
||||
> *primary-expression* → [*closure-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/closure-expression)
|
||||
> *primary-expression* → [*parenthesized-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/parenthesized-expression)
|
||||
> *primary-expression* → [*implicit-member-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/implicit-member-expression)
|
||||
> *primary-expression* → [*wildcard-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/wildcard-expression)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 文本表达式 (Grammar of a literal expression)
|
||||
> *literal-expression* → [*literal*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/literal)
|
||||
> *literal-expression* → [*array-literal*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/array-literal) | [*dictionary-literal*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/dictionary-literal)
|
||||
> *literal-expression* → **__FILE__** | **__LINE__** | **__COLUMN__** | **__FUNCTION__**
|
||||
> *array-literal* → **[** [*array-literal-items*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/array-literal-items) _opt_ **]**
|
||||
> *array-literal-items* → [*array-literal-item*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/array-literal-item) **,** _opt_ | [*array-literal-item*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/array-literal-item) **,** [*array-literal-items*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/array-literal-items)
|
||||
> *array-literal-item* → [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression)
|
||||
> *dictionary-literal* → **[** [*dictionary-literal-items*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/dictionary-literal-items) **]** | **[** **:** **]**
|
||||
> *dictionary-literal-items* → [*dictionary-literal-item*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/dictionary-literal-item) **,** _opt_ | [*dictionary-literal-item*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/dictionary-literal-item) **,** [*dictionary-literal-items*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/dictionary-literal-items)
|
||||
> *dictionary-literal-item* → [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) **:** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression)
|
||||
|
||||
<p></p>
|
||||
|
||||
> Self 表达式 (Grammar of a self expression)
|
||||
> *self-expression* → **self**
|
||||
> *self-expression* → **self** **.** [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier)
|
||||
> *self-expression* → **self** **[** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) **]**
|
||||
> *self-expression* → **self** **.** **init**
|
||||
|
||||
<p></p>
|
||||
|
||||
> 超类表达式 (Grammar of a superclass expression)
|
||||
> *superclass-expression* → [*superclass-method-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/superclass-method-expression) | [*superclass-subscript-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/superclass-subscript-expression) | [*superclass-initializer-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/superclass-initializer-expression)
|
||||
> *superclass-method-expression* → **super** **.** [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier)
|
||||
> *superclass-subscript-expression* → **super** **[** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) **]**
|
||||
> *superclass-initializer-expression* → **super** **.** **init**
|
||||
|
||||
<p></p>
|
||||
|
||||
> 闭包表达式 (Grammar of a closure expression)
|
||||
> *closure-expression* → **{** [*closure-signature*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/closure-signature) _opt_ [*statements*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements) **}**
|
||||
> *closure-signature* → [*parameter-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter-clause) [*function-result*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-result) _opt_ **in**
|
||||
> *closure-signature* → [*identifier-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-list) [*function-result*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-result) _opt_ **in**
|
||||
> *closure-signature* → [*capture-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/capture-list) [*parameter-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter-clause) [*function-result*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-result) _opt_ **in**
|
||||
> *closure-signature* → [*capture-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/capture-list) [*identifier-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-list) [*function-result*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-result) _opt_ **in**
|
||||
> *closure-signature* → [*capture-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/capture-list) **in**
|
||||
> *capture-list* → **[** [*capture-specifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/capture-specifier) [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) **]**
|
||||
> *capture-specifier* → **weak** | **unowned** | **unowned(safe)** | **unowned(unsafe)**
|
||||
|
||||
<p></p>
|
||||
|
||||
> 隐式成员表达式 (Grammar of a implicit member expression)
|
||||
> *implicit-member-expression* → **.** [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 带圆括号的表达式 (Grammar of a parenthesized expression)
|
||||
> *parenthesized-expression* → **(** [*expression-element-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression-element-list) _opt_ **)**
|
||||
> *expression-element-list* → [*expression-element*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression-element) | [*expression-element*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression-element) **,** [*expression-element-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression-element-list)
|
||||
> *expression-element* → [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) | [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier) **:** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 通配符表达式 (Grammar of a wildcard expression)
|
||||
> *wildcard-expression* → **_**
|
||||
|
||||
<p></p>
|
||||
|
||||
> 后缀表达式 (Grammar of a postfix expression)
|
||||
> *postfix-expression* → [*primary-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/primary-expression)
|
||||
> *postfix-expression* → [*postfix-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/postfix-expression) [*postfix-operator*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/postfix-operator)
|
||||
> *postfix-expression* → [*function-call-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/function-call-expression)
|
||||
> *postfix-expression* → [*initializer-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/initializer-expression)
|
||||
> *postfix-expression* → [*explicit-member-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/explicit-member-expression)
|
||||
> *postfix-expression* → [*postfix-self-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/postfix-self-expression)
|
||||
> *postfix-expression* → [*dynamic-type-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/dynamic-type-expression)
|
||||
> *postfix-expression* → [*subscript-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/subscript-expression)
|
||||
> *postfix-expression* → [*forced-value-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/forced-value-expression)
|
||||
> *postfix-expression* → [*optional-chaining-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/optional-chaining-expression)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 函数调用表达式 (Grammar of a function call expression)
|
||||
> *function-call-expression* → [*postfix-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/postfix-expression) [*parenthesized-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/parenthesized-expression)
|
||||
> *function-call-expression* → [*postfix-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/postfix-expression) [*parenthesized-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/parenthesized-expression) _opt_ [*trailing-closure*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/trailing-closure)
|
||||
> *trailing-closure* → [*closure-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/closure-expression)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 初始化表达式 (Grammar of an initializer expression)
|
||||
> *initializer-expression* → [*postfix-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/postfix-expression) **.** **init**
|
||||
|
||||
<p></p>
|
||||
|
||||
> 显式成员表达式 (Grammar of an explicit member expression)
|
||||
> *explicit-member-expression* → [*postfix-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/postfix-expression) **.** [*decimal-digit*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-digit)
|
||||
> *explicit-member-expression* → [*postfix-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/postfix-expression) **.** [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier) [*generic-argument-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-argument-clause) _opt_
|
||||
|
||||
<p></p>
|
||||
|
||||
> (Grammar of a self expression)
|
||||
> *postfix-self-expression* → [*postfix-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/postfix-expression) **.** **self**
|
||||
|
||||
<p></p>
|
||||
|
||||
> 动态类型表达式 (Grammar of a dynamic type expression)
|
||||
> *dynamic-type-expression* → [*postfix-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/postfix-expression) **.** **dynamicType**
|
||||
|
||||
<p></p>
|
||||
|
||||
> 附属脚本表达式 (Grammar of a subscript expression)
|
||||
> *subscript-expression* → [*postfix-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/postfix-expression) **[** [*expression-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression-list) **]**
|
||||
|
||||
<p></p>
|
||||
|
||||
> (Grammar of a forced-value expression)
|
||||
> *forced-value-expression* → [*postfix-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/postfix-expression) **!**
|
||||
|
||||
<p></p>
|
||||
|
||||
> 可选链表达式 (Grammar of an optional-chaining expression)
|
||||
> *optional-chaining-expression* → [*postfix-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/postfix-expression) **?**
|
||||
|
||||
## 词法结构 (Lexical Structure)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 标识符语法 (Grammar of an identifier)
|
||||
> *identifier* → [*identifier-head*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-head) [*identifier-characters*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-characters) _opt_
|
||||
> *identifier* → **`** [*identifier-head*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-head) [*identifier-characters*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-characters) _opt_ **`**
|
||||
> *identifier* → [*implicit-parameter-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/implicit-parameter-name)
|
||||
> *identifier-list* → [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier) | [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier) **,** [*identifier-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-list)
|
||||
> *identifier-head* → Upper- or lowercase letter A through Z
|
||||
> *identifier-head* → U+00A8, U+00AA, U+00AD, U+00AF, U+00B2–U+00B5, or U+00B7–U+00BA
|
||||
> *identifier-head* → U+00BC–U+00BE, U+00C0–U+00D6, U+00D8–U+00F6, or U+00F8–U+00FF
|
||||
> *identifier-head* → U+0100–U+02FF, U+0370–U+167F, U+1681–U+180D, or U+180F–U+1DBF
|
||||
> *identifier-head* → U+1E00–U+1FFF
|
||||
> *identifier-head* → U+200B–U+200D, U+202A–U+202E, U+203F–U+2040, U+2054, or U+2060–U+206F
|
||||
> *identifier-head* → U+2070–U+20CF, U+2100–U+218F, U+2460–U+24FF, or U+2776–U+2793
|
||||
> *identifier-head* → U+2C00–U+2DFF or U+2E80–U+2FFF
|
||||
> *identifier-head* → U+3004–U+3007, U+3021–U+302F, U+3031–U+303F, or U+3040–U+D7FF
|
||||
> *identifier-head* → U+F900–U+FD3D, U+FD40–U+FDCF, U+FDF0–U+FE1F, or U+FE30–U+FE44
|
||||
> *identifier-head* → U+FE47–U+FFFD
|
||||
> *identifier-head* → U+10000–U+1FFFD, U+20000–U+2FFFD, U+30000–U+3FFFD, or U+40000–U+4FFFD
|
||||
> *identifier-head* → U+50000–U+5FFFD, U+60000–U+6FFFD, U+70000–U+7FFFD, or U+80000–U+8FFFD
|
||||
> *identifier-head* → U+90000–U+9FFFD, U+A0000–U+AFFFD, U+B0000–U+BFFFD, or U+C0000–U+CFFFD
|
||||
> *identifier-head* → U+D0000–U+DFFFD or U+E0000–U+EFFFD
|
||||
> *identifier-character* → Digit 0 through 9
|
||||
> *identifier-character* → U+0300–U+036F, U+1DC0–U+1DFF, U+20D0–U+20FF, or U+FE20–U+FE2F
|
||||
> *identifier-character* → [*identifier-head*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-head)
|
||||
> *identifier-characters* → [*identifier-character*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-character) [*identifier-characters*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-characters) _opt_
|
||||
> *implicit-parameter-name* → **$** [*decimal-digits*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-digits)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 字面值语法 (Grammar of a literal)
|
||||
> *literal* → [*integer-literal*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/integer-literal) | [*floating-point-literal*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/floating-point-literal) | [*string-literal*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/string-literal)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 整形字面值 (Grammar of an integer literal)
|
||||
> *integer-literal* → [*binary-literal*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/binary-literal)
|
||||
> *integer-literal* → [*octal-literal*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/octal-literal)
|
||||
> *integer-literal* → [*decimal-literal*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-literal)
|
||||
> *integer-literal* → [*hexadecimal-literal*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-literal)
|
||||
> *binary-literal* → **0b** [*binary-digit*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/binary-digit) [*binary-literal-characters*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/binary-literal-characters) _opt_
|
||||
> *binary-digit* → Digit 0 or 1
|
||||
> *binary-literal-character* → [*binary-digit*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/binary-digit) | **_**
|
||||
> *binary-literal-characters* → [*binary-literal-character*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/binary-literal-character) [*binary-literal-characters*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/binary-literal-characters) _opt_
|
||||
> *octal-literal* → **0o** [*octal-digit*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/octal-digit) [*octal-literal-characters*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/octal-literal-characters) _opt_
|
||||
> *octal-digit* → Digit 0 through 7
|
||||
> *octal-literal-character* → [*octal-digit*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/octal-digit) | **_**
|
||||
> *octal-literal-characters* → [*octal-literal-character*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/octal-literal-character) [*octal-literal-characters*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/octal-literal-characters) _opt_
|
||||
> *decimal-literal* → [*decimal-digit*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-digit) [*decimal-literal-characters*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-literal-characters) _opt_
|
||||
> *decimal-digit* → Digit 0 through 9
|
||||
> *decimal-digits* → [*decimal-digit*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-digit) [*decimal-digits*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-digits) _opt_
|
||||
> *decimal-literal-character* → [*decimal-digit*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-digit) | **_**
|
||||
> *decimal-literal-characters* → [*decimal-literal-character*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-literal-character) [*decimal-literal-characters*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-literal-characters) _opt_
|
||||
> *hexadecimal-literal* → **0x** [*hexadecimal-digit*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit) [*hexadecimal-literal-characters*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-literal-characters) _opt_
|
||||
> *hexadecimal-digit* → Digit 0 through 9, a through f, or A through F
|
||||
> *hexadecimal-literal-character* → [*hexadecimal-digit*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit) | **_**
|
||||
> *hexadecimal-literal-characters* → [*hexadecimal-literal-character*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-literal-character) [*hexadecimal-literal-characters*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-literal-characters) _opt_
|
||||
|
||||
<p></p>
|
||||
|
||||
> 浮点型字面值 (Grammar of a floating-point literal)
|
||||
> *floating-point-literal* → [*decimal-literal*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-literal) [*decimal-fraction*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-fraction) _opt_ [*decimal-exponent*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-exponent) _opt_
|
||||
> *floating-point-literal* → [*hexadecimal-literal*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-literal) [*hexadecimal-fraction*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-fraction) _opt_ [*hexadecimal-exponent*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-exponent)
|
||||
> *decimal-fraction* → **.** [*decimal-literal*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-literal)
|
||||
> *decimal-exponent* → [*floating-point-e*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/floating-point-e) [*sign*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/sign) _opt_ [*decimal-literal*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-literal)
|
||||
> *hexadecimal-fraction* → **.** [*hexadecimal-literal*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-literal) _opt_
|
||||
> *hexadecimal-exponent* → [*floating-point-p*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/floating-point-p) [*sign*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/sign) _opt_ [*hexadecimal-literal*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-literal)
|
||||
> *floating-point-e* → **e** | **E**
|
||||
> *floating-point-p* → **p** | **P**
|
||||
> *sign* → **+** | **-**
|
||||
|
||||
<p></p>
|
||||
|
||||
> 字符串字面值 (Grammar of a string literal)
|
||||
> *string-literal* → **"** [*quoted-text*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/quoted-text) **"**
|
||||
> *quoted-text* → [*quoted-text-item*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/quoted-text-item) [*quoted-text*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/quoted-text) _opt_
|
||||
> *quoted-text-item* → [*escaped-character*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/escaped-character)
|
||||
> *quoted-text-item* → **\(** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) **)**
|
||||
> *quoted-text-item* → Any Unicode extended grapheme cluster except ", \, U+000A, or U+000D
|
||||
> *escaped-character* → **\0** | **\\** | **\t** | **\n** | **\r** | **\"** | **\'**
|
||||
> *escaped-character* → **\x** [*hexadecimal-digit*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit) [*hexadecimal-digit*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit)
|
||||
> *escaped-character* → **\u** [*hexadecimal-digit*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit) [*hexadecimal-digit*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit) [*hexadecimal-digit*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit) [*hexadecimal-digit*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit)
|
||||
> *escaped-character* → **\U** [*hexadecimal-digit*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit) [*hexadecimal-digit*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit) [*hexadecimal-digit*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit) [*hexadecimal-digit*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit) [*hexadecimal-digit*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit) [*hexadecimal-digit*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit) [*hexadecimal-digit*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit) [*hexadecimal-digit*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 运算符语法 (Grammar of operators)
|
||||
> *operator* → [*operator-character*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/operator-character) [*operator*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/operator) _opt_
|
||||
> *operator-character* → **/** | **=** | **-** | **+** | **!** | ***** | **%** | **<** | **>** | **&** | **|** | **^** | **~** | **.**
|
||||
> *binary-operator* → [*operator*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/operator)
|
||||
> *prefix-operator* → [*operator*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/operator)
|
||||
> *postfix-operator* → [*operator*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/operator)
|
||||
|
||||
## 类型 (Types)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 类型 (Grammar of a type)
|
||||
> *type* → [*array-type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/array-type) | [*function-type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/function-type) | [*type-identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-identifier) | [*tuple-type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/tuple-type) | [*optional-type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/optional-type) | [*implicitly-unwrapped-optional-type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/implicitly-unwrapped-optional-type) | [*protocol-composition-type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/protocol-composition-type) | [*metatype-type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/metatype-type)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 类型标注 (Grammar of a type annotation)
|
||||
> *type-annotation* → **:** [*attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes) _opt_ [*type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 类型标识 (Grammar of a type identifier)
|
||||
> *type-identifier* → [*type-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-name) [*generic-argument-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-argument-clause) _opt_ | [*type-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-name) [*generic-argument-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-argument-clause) _opt_ **.** [*type-identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-identifier)
|
||||
> *type-name* → [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 元组类型 (Grammar of a tuple type)
|
||||
> *tuple-type* → **(** [*tuple-type-body*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/tuple-type-body) _opt_ **)**
|
||||
> *tuple-type-body* → [*tuple-type-element-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/tuple-type-element-list) **...** _opt_
|
||||
> *tuple-type-element-list* → [*tuple-type-element*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/tuple-type-element) | [*tuple-type-element*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/tuple-type-element) **,** [*tuple-type-element-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/tuple-type-element-list)
|
||||
> *tuple-type-element* → [*attributes*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes) _opt_ **inout** _opt_ [*type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type) | **inout** _opt_ [*element-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/element-name) [*type-annotation*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-annotation)
|
||||
> *element-name* → [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 函数类型 (Grammar of a function type)
|
||||
> *function-type* → [*type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type) **->** [*type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 数组类型 (Grammar of an array type)
|
||||
> *array-type* → [*type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type) **[** **]** | [*array-type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/array-type) **[** **]**
|
||||
|
||||
<p></p>
|
||||
|
||||
> 可选类型 (Grammar of an optional type)
|
||||
> *optional-type* → [*type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type) **?**
|
||||
|
||||
<p></p>
|
||||
|
||||
> 隐式解析可选类型 (Grammar of an implicitly unwrapped optional type)
|
||||
> *implicitly-unwrapped-optional-type* → [*type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type) **!**
|
||||
|
||||
<p></p>
|
||||
|
||||
> 协议合成类型 (Grammar of a protocol composition type)
|
||||
> *protocol-composition-type* → **protocol** **<** [*protocol-identifier-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/protocol-identifier-list) _opt_ **>**
|
||||
> *protocol-identifier-list* → [*protocol-identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/protocol-identifier) | [*protocol-identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/protocol-identifier) **,** [*protocol-identifier-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/protocol-identifier-list)
|
||||
> *protocol-identifier* → [*type-identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-identifier)
|
||||
|
||||
<p></p>
|
||||
|
||||
> 元类型 (Grammar of a metatype type)
|
||||
> *metatype-type* → [*type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type) **.** **Type** | [*type*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type) **.** **Protocol**
|
||||
|
||||
<p></p>
|
||||
|
||||
> 类型继承子句 (Grammar of a type inheritance clause)
|
||||
> *type-inheritance-clause* → **:** [*type-inheritance-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-inheritance-list)
|
||||
> *type-inheritance-list* → [*type-identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-identifier) | [*type-identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-identifier) **,** [*type-inheritance-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-inheritance-list)
|
||||
|
||||
@ -0,0 +1,375 @@
|
||||
# 语句
|
||||
|
||||
在 Swift 中,有两种类型的语句:简单语句和控制流语句。简单语句是最常见的,用于构造表达式和声明。控制流语句则用于控制程序执行的流程,Swift 中有三种类型的控制流语句:循环语句、分支语句和控制传递语句。
|
||||
|
||||
循环语句用于重复执行代码块;分支语句用于执行满足特定条件的代码块;控制传递语句则用于修改代码的执行顺序。在稍后的叙述中,将会详细地介绍每一种类型的控制流语句。
|
||||
|
||||
是否将分号(;)添加到语句的结尾处是可选的。但若要在同一行内写多条独立语句,请务必使用分号。
|
||||
|
||||
> GRAMMAR OF A STATEMENT
|
||||
|
||||
> *statement* → [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression)**;** *opt*
|
||||
|
||||
> *statement* → [*declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration)**;** *opt*
|
||||
|
||||
> *statement* → [*loop-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/loop-statement)**;** *opt*
|
||||
|
||||
> *statement* → [*branch-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/branch-statement)**;** *opt*
|
||||
|
||||
> *statement* → [*labeled-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/labeled-statement)
|
||||
|
||||
> *statement* → [*control-transfer-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/control-transfer-statement)**;** *opt*
|
||||
|
||||
> *statement* → [*statment*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement) [*statements*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements)**;** *opt*
|
||||
|
||||
## 循环语句
|
||||
|
||||
取决于特定的循环条件,循环语句允许重复执行代码块。Swift 提供四种类型的循环语句:`for`语句、`for-in`语句、`while`语句和`do-while`语句。
|
||||
|
||||
通过`break`语句和`continue`语句可以改变循环语句的控制流。有关这两条语句,请参考[*Break 语句*`待添加链接`]()和[*Continue 语句*`待添加链接`]()。
|
||||
|
||||
> GRAMMAR OF A LOOP STATEMENT
|
||||
|
||||
> *loop-statement* → [*for-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-statement)
|
||||
|
||||
> *loop-statement* → [*for-in-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-in-statement)
|
||||
|
||||
> *loop-statement* → [*while-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-statement)
|
||||
|
||||
> *loop-statement* → [*do-while-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/do-while-statement)
|
||||
|
||||
### For 语句
|
||||
|
||||
`for`语句允许在重复执行代码块的同时,递增一个计数器。
|
||||
|
||||
`for`语句的形式如下:
|
||||
|
||||
```swift
|
||||
for `initialzation`; `condition`; `increment` {
|
||||
`statements`
|
||||
}
|
||||
```
|
||||
|
||||
*initialzation*、*condition*和*increment*之间的分号,以及包围循环体*statements*的大括号都是不可省略的。
|
||||
|
||||
`for`语句的执行流程如下:
|
||||
|
||||
1. *initialzation*只会被执行一次,通常用于声明和初始化在接下来的循环中需要使用的变量。
|
||||
|
||||
2. 计算*condition*表达式:
|
||||
如果为真(`true`),*statements*将会被执行,然后转到第3步。如果为假(`false`),*statements*和*increment*都不会被执行,`for`至此执行完毕。
|
||||
|
||||
3. 计算*increment*表达式,然后转到第2步。
|
||||
|
||||
定义在*initialzation*中的变量仅在`for`语句的作用域以内有效。*condition*表达式的值的类型必须符合`LogicValue`协议。
|
||||
|
||||
> GRAMMAR OF A FOR STATEMENT
|
||||
|
||||
> *for-statement* → **for** [*for-init*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-init) *opt* **;** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) *opt* **;** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) *opt* [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block)
|
||||
|
||||
> *for-statement* → **for ( ** [*for-init*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-init) *opt* **;** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) *opt* **;** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) *opt* **)** [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block)
|
||||
|
||||
> *for-statement* → [*variable-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-declaration) | [*expression-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression-list)
|
||||
|
||||
### For-In 语句
|
||||
|
||||
`for-in`语句允许在重复执行代码块的同时,迭代集合(或符合`Sequence`协议的任意类型)中的每一项。
|
||||
|
||||
`for-in`语句的形式如下:
|
||||
|
||||
```swift
|
||||
for `item` in `collection` {
|
||||
`statements`
|
||||
}
|
||||
```
|
||||
|
||||
`for-in`语句在循环开始前会调用*collection*表达式的`generate`方法来获取一个生成器类型(这是一个符合`Generator`协议的类型)的值。接下来循环开始,调用*collection*表达式的`next`方法。如果其返回值不是`None`,它将会被赋给*item*,然后执行*statements*,执行完毕后回到循环开始处;否则,将不会赋值给*item*也不会执行*statements*,`for-in`至此执行完毕。
|
||||
|
||||
> GRAMMAR OF A FOR-IN STATEMENT
|
||||
|
||||
> *for-in-statement* → **for** [*pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern) **in** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block)
|
||||
|
||||
### While 语句
|
||||
|
||||
`while`语句允许重复执行代码块。
|
||||
|
||||
`while`语句的形式如下:
|
||||
|
||||
```swift
|
||||
while `condition` {
|
||||
`statements`
|
||||
}
|
||||
```
|
||||
|
||||
`while`语句的执行流程如下:
|
||||
|
||||
1. 计算*condition*表达式:
|
||||
如果为真(`true`),转到第2步。如果为假(`false`),`while`至此执行完毕。
|
||||
|
||||
2. 执行*statements*,然后转到第1步。
|
||||
|
||||
由于*condition*的值在*statements*执行前就已计算出,因此`while`语句中的*statements*可能会被执行若干次,也可能不会被执行。
|
||||
|
||||
*condition*表达式的值的类型必须符合`LogicValue`协议。同时,*condition*表达式也可以使用可选绑定,请参考[可选绑定`待添加链接`]()。
|
||||
|
||||
> GRAMMAR OF A WHILE STATEMENT
|
||||
|
||||
> *while-statement* → **while** [*while-condition*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-condition) [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block)
|
||||
|
||||
> *while-condition* → [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) | [*declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration)
|
||||
|
||||
### Do-While 语句
|
||||
|
||||
`do-while`语句允许代码块被执行一次或多次。
|
||||
|
||||
`do-while`语句的形式如下:
|
||||
|
||||
```swift
|
||||
do {
|
||||
`statements`
|
||||
} while `condition`
|
||||
```
|
||||
|
||||
`do-while`语句的执行流程如下:
|
||||
|
||||
1. 执行*statements*,然后转到第2步。
|
||||
|
||||
2. 计算*condition*表达式:
|
||||
如果为真(`true`),转到第1步。如果为假(`false`),`do-while`至此执行完毕。
|
||||
|
||||
由于*condition*表达式的值是在*statements*表达式执行后才计算出,因此*do-while*语句中的*statements*至少会被执行一次。
|
||||
|
||||
*condition*表达式的值的类型必须符合`LogicValue`协议。同时,*condition*表达式也可以使用可选绑定,请参考[可选绑定`待添加链接`]()。
|
||||
|
||||
> GRAMMAR OF A DO-WHILE STATEMENT
|
||||
|
||||
> *do-while-statement* → **do** [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block) **while** [*while-condition*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-condition)
|
||||
|
||||
## 分支语句
|
||||
|
||||
取决于一个或者多个条件的值,分支语句允许程序执行指定部分的代码。显然,分支语句中条件的值将会决定如何分支以及执行哪一块代码。Swift 提供两种类型的分支语句:`if`语句和`switch`语句。
|
||||
|
||||
`switch`语句中的控制流可以用`break`语句修改,请参考[Break 语句`待添加链接`]()。
|
||||
|
||||
> GRAMMAR OF A BRANCH STATEMENT
|
||||
|
||||
> *branch-statement* → [*if-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-statement)
|
||||
|
||||
> *branch-statement* → [*switch-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-statement)
|
||||
|
||||
### If 语句
|
||||
|
||||
取决于一个或多个条件的值,`if`语句将决定执行哪一块代码。
|
||||
|
||||
`if`语句有两种标准形式,在这两种形式里都必须有大括号。
|
||||
|
||||
第一种形式是当且仅当条件为真时执行代码,像下面这样:
|
||||
|
||||
```swift
|
||||
if `condition` {
|
||||
`statements`
|
||||
}
|
||||
```
|
||||
|
||||
第二种形式是在第一种形式的基础上添加*else语句*,当只有一个*else语句*时,像下面这样:
|
||||
|
||||
```swift
|
||||
if `condition` {
|
||||
`statements to execute if condition is true`
|
||||
} else {
|
||||
`statements to execute if condition is false`
|
||||
}
|
||||
```
|
||||
|
||||
同时,*else语句*也可包含`if`语句,从而形成一条链来测试更多的条件,像下面这样:
|
||||
|
||||
```swift
|
||||
if `condition 1` {
|
||||
`statements to execute if condition 1 is true`
|
||||
} else if `condition 2` {
|
||||
`statements to execute if condition 2 is true`
|
||||
}
|
||||
else {
|
||||
`statements to execute if both conditions are false`
|
||||
}
|
||||
```
|
||||
|
||||
`if`语句中条件的值的类型必须符合`LogicValue`协议。同时,条件也可以使用可选绑定,请参考[可选绑定`待添加链接`]()。
|
||||
|
||||
> GRAMMAR OF AN IF STATEMENT
|
||||
|
||||
> *if-statement* → **if** [*if-condition*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-condition) [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block) [*else-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/else-clause) *opt*
|
||||
|
||||
> *if-condition* → [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) | [*declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration)
|
||||
|
||||
> *else-clause* → **else** [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block) | **else** [*if-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-statement) *opt*
|
||||
|
||||
### Switch 语句
|
||||
|
||||
取决于`switch`语句的控制表达式(*control expression*),`switch`语句将决定执行哪一块代码。
|
||||
|
||||
`switch`语句的形式如下:
|
||||
|
||||
```swift
|
||||
switch `control expression` {
|
||||
case `pattern 1`:
|
||||
`statements`
|
||||
case `pattern 2` where `condition`:
|
||||
`statements`
|
||||
case `pattern 3` where `condition`,
|
||||
`pattern 4` where `condition`:
|
||||
`statements`
|
||||
default:
|
||||
`statements`
|
||||
}
|
||||
```
|
||||
|
||||
`switch`语句的*控制表达式(control expression)*会首先被计算,然后与每一个*case*的模式(pattern)进行匹配。如果匹配成功,程序将会执行对应的*case*块里的*statements*。另外,每一个*case*块都不能为空,也就是说在每一个*case*块中至少有一条语句。如果你不想在匹配到的*case*块中执行代码,只需在块里写一条`break`语句即可。
|
||||
|
||||
可以用作控制表达式的值是十分灵活的,除了标量类型(scalar types,如`Int`、`Character`)外,你可以使用任何类型的值,包括浮点数、字符串、元组、自定义类的实例和可选(optional)类型,甚至是枚举类型中的成员值和指定的范围(range)等。关于在`switch`语句中使用这些类型,请参考[控制流`待添加链接`]()一章的[Switch`待添加链接`]()。
|
||||
|
||||
你可以在模式后面添加一个起保护作用的表达式(guard expression)。*起保护作用的表达式*是这样构成的:关键字`where`后面跟着一个作为额外测试条件的表达式。因此,当且仅当*控制表达式*匹配一个*case*的某个模式且起保护作用的表达式为真时,对应*case*块中的*statements*才会被执行。在下面的例子中,*控制表达式*只会匹配含两个相等元素的元组,如`(1, 1)`:
|
||||
|
||||
```swift
|
||||
case let (x, y) where x == y:
|
||||
}
|
||||
```
|
||||
|
||||
正如上面这个例子,也可以在模式中使用`let`(或`var`)语句来绑定常量(或变量)。这些常量(或变量)可以在其对应的起保护作用的表达式和其对应的*case*块里的代码中引用。但是,如果*case*中有多个模式匹配控制表达式,那么这些模式都不能绑定常量(或变量)。
|
||||
|
||||
`switch`语句也可以包含默认(`default`)块,只有其它*case*块都无法匹配控制表达式时,默认块中的代码才会被执行。一个`switch`语句只能有一个默认块,而且必须在`switch`语句的最后面。
|
||||
|
||||
尽管模式匹配操作实际的执行顺序,特别是模式的计算顺序是不可知的,但是 Swift 规定`switch`语句中的模式匹配的顺序和书写源代码的顺序保持一致。因此,当多个模式含有相同的值且能够匹配控制表达式时,程序只会执行源代码中第一个匹配的*case*块中的代码。
|
||||
|
||||
#### Switch 语句必须是完备的
|
||||
|
||||
在 Swift 中,`switch`语句中控制表达式的每一个可能的值都必须至少有一个`case`块与之对应。在某些情况下(例如,表达式的类型是`Int`),你可以使用默认块满足该要求。
|
||||
|
||||
#### 不存在隐式的贯穿(fall through)
|
||||
|
||||
当匹配的*case*块中的代码执行完毕后,程序会终止`switch`语句,而不会继续执行下一个*case*块。这就意味着,如果你想执行下一个*case*块,需要显式地在你需要的*case*块里使用`fallthrough`语句。关于`fallthrough`语句的更多信息,请参考[Fallthrough 语句`待添加链接`]()。
|
||||
|
||||
> GRAMMAR OF A SWITCH STATEMENT
|
||||
|
||||
> *switch-statement* → **switch** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) **{** [*switch-cases*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-cases) *opt* **}**
|
||||
|
||||
> *switch-cases* → [*switch-case*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-case) [*switch-cases*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-cases) *opt*
|
||||
|
||||
> *switch-case* → [*case-label*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-label) [*statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements) | [*default-label*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/default-label) [*statements*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements)
|
||||
|
||||
> *switch-case* → [*case-label*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-label) **;** | [*default-label*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/default-label) **;**
|
||||
|
||||
> *case-label* → **case** [*case-item-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-item-list) **:**
|
||||
|
||||
> *case-item-list* → [*pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern) [*guard-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-clause) *opt* | [*pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern) [*guard-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-clause) *opt*, [*case-item-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-item-list)
|
||||
|
||||
> *default-label* → **default :**
|
||||
|
||||
> *guard-clause* → **where** [*guard-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-expression)
|
||||
|
||||
> *guard-expression* → [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression)
|
||||
|
||||
## 带标签的语句
|
||||
|
||||
你可以在循环语句或`switch`语句前面加上*标签*,它由标签名和紧随其后的冒号(:)组成。在`break`和`continue`后面跟上标签名可以显式地在循环语句或`switch`语句中更改控制流,把控制权传递给指定标签标记的语句。关于这两条语句用法,请参考[Break 语句`待添加链接`]()和[Continue 语句`待添加链接`]()。
|
||||
|
||||
标签的作用域是该标签所标记的语句之后的所有语句。你可以不使用带标签的语句,但只要使用它,标签名就必唯一。
|
||||
|
||||
关于使用带标签的语句的例子,请参考[控制流`待添加链接`]()一章的[带标签的语句`待添加链接`]()。
|
||||
|
||||
> GRAMMAR OF A LABELED STATEMENT
|
||||
|
||||
> *labeled-statement* → [*statement-label*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement-label) [*loop-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/loop-statement) | [*statement-label*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement-label) [*switch-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-statement)
|
||||
|
||||
> *statement-label* → [*label-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name) **:**
|
||||
|
||||
> *label-name* → [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier)
|
||||
|
||||
## 控制传递语句
|
||||
|
||||
通过无条件地把控制权从一片代码传递到另一片代码,控制传递语句能够改变代码执行的顺序。Swift 提供四种类型的控制传递语句:`break`语句、`continue`语句、`fallthrough`语句和`return`语句。
|
||||
|
||||
> GRAMMAR OF A CONTROL TRANSER STATEMENT
|
||||
|
||||
> *control-transfer-statement* → [*break-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/break-statement)
|
||||
|
||||
> *control-transfer-statement* → [*continue-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/continue-statement)
|
||||
|
||||
> *control-transfer-statement* → [*fallthrough-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/fallthrough-statement)
|
||||
|
||||
> *control-transfer-statement* → [*return-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/return-statement)
|
||||
|
||||
### Break 语句
|
||||
|
||||
`break`语句用于终止循环或`switch`语句的执行。使用`break`语句时,可以只写`break`这个关键词,也可以在`break`后面跟上标签名(label name),像下面这样:
|
||||
|
||||
```swift
|
||||
break
|
||||
break `label name`
|
||||
```
|
||||
|
||||
当`break`语句后面带标签名时,可用于终止由这个标签标记的循环或`switch`语句的执行。
|
||||
|
||||
而当只写`break`时,则会终止`switch`语句或上下文中包含`break`语句的最内层循环的执行。
|
||||
|
||||
在这两种情况下,控制权都会被传递给循环或`switch`语句外面的第一行语句。
|
||||
|
||||
关于使用`break`语句的例子,请参考[控制流`待添加链接`]()一章的[Break`待添加链接`]()和[带标签的语句`待添加链接`]()。
|
||||
|
||||
> GRAMMAR OF A BREAK STATEMENT
|
||||
|
||||
> *break-statement* → **break** [*label-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name) *opt*
|
||||
|
||||
### Continue 语句
|
||||
|
||||
`continue`语句用于终止循环中当前迭代的执行,但不会终止该循环的执行。使用`continue`语句时,可以只写`continue`这个关键词,也可以在`continue`后面跟上标签名(label name),像下面这样:
|
||||
|
||||
```swift
|
||||
continue
|
||||
continue `label name`
|
||||
```
|
||||
|
||||
当`continue`语句后面带标签名时,可用于终止由这个标签标记的循环中当前迭代的执行。
|
||||
|
||||
而当只写`break`时,可用于终止上下文中包含`continue`语句的最内层循环中当前迭代的执行。
|
||||
|
||||
在这两种情况下,控制权都会被传递给循环外面的第一行语句。
|
||||
|
||||
在`for`语句中,`continue`语句执行后,*increment*表达式还是会被计算,这是因为每次循环体执行完毕后*increment*表达式都会被计算。
|
||||
|
||||
关于使用`continue`语句的例子,请参考[控制流`待添加链接`]()一章的[Continue`待添加链接`]()和[带标签的语句`待添加链接`]()。
|
||||
|
||||
> GRAMMAR OF A CONTINUE STATEMENT
|
||||
|
||||
> *continue-statement* → **continue** [*label-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name) *opt*
|
||||
|
||||
### Fallthrough 语句
|
||||
|
||||
`fallthrough`语句用于在`switch`语句中传递控制权。`fallthrough`语句会把控制权从`switch`语句中的一个`case`传递给下一个`case`。这种传递是无条件的,即使下一个`case`的值与`switch`语句的控制表达式的值不匹配。
|
||||
|
||||
`fallthrough`语句可出现在`switch`语句中的任意`case`里,但不能出现在最后一个'case'块。同时,`fallthrough`语句也不能把控制权传递给使用了可选绑定的`case`块。
|
||||
|
||||
关于在`switch`语句中使用`fallthrough`语句的例子,请参考[控制流`待添加链接`]()一章的[控制传递语句`待添加链接`]()。
|
||||
|
||||
> GRAMMAR OF A FALLTRHOUGH STATEMENT
|
||||
|
||||
> *continue-statement* → **fallthrough**
|
||||
|
||||
### Return 语句
|
||||
|
||||
`return`语句用于在函数或方法的实现中将控制权传递给调用者,接着程序将会从调用者的位置继续向下执行。
|
||||
|
||||
使用`return`语句时,可以只写`return`这个关键词,也可以在`return`后面跟上表达式,像下面这样:
|
||||
|
||||
```swift
|
||||
return
|
||||
return `表达式`
|
||||
```
|
||||
|
||||
当`return`语句后面带表达式时,表达式的值将会返回给调用者。如果表达式值的类型与调用者期望的类型不匹配,Swift 则会在返回表达式的值之前将表达式值的类型转换为调用者期望的类型。
|
||||
|
||||
而当只写`return`时,仅仅是将控制权从该函数或方法传递给调用者,而不返回一个值。(这就是说,该函数或方法的返回类型为`Void`或`()`)
|
||||
|
||||
> GRAMMAR OF A RETURN STATEMENT
|
||||
|
||||
> *return-statement* → **return** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) *opt*
|
||||
Reference in New Issue
Block a user