correct the duplicated code
This commit is contained in:
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="1.1" data-basepath=".." data-revision="1402300340983">
|
||||
<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>
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_60">
|
||||
<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>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="1.2" data-basepath=".." data-revision="1402300340983">
|
||||
<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>
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_61">
|
||||
<section class="normal" id="section-gitbook_119">
|
||||
|
||||
<h1 id="swift-">Swift 初见</h1>
|
||||
<p>本页内容包括:</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="1" data-basepath=".." data-revision="1402300340983">
|
||||
<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>
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_62">
|
||||
<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="1402300340983">
|
||||
<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>
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_64">
|
||||
<section class="normal" id="section-gitbook_122">
|
||||
|
||||
<h1 id="-">基础部分</h1>
|
||||
<p>Swift 是 iOS 和 OS X 应用开发的一门新语言。然而,如果你有 C 或者 Objective-C 开发经验的话,你会发现 Swift 的很多内容都是你熟悉的。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.2" data-basepath=".." data-revision="1402300340983">
|
||||
<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>
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_86">
|
||||
<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="1402300340983">
|
||||
<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>
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_65">
|
||||
<section class="normal" id="section-gitbook_123">
|
||||
|
||||
<h1 id="-strings-and-characters-">字符串和字符 (Strings and Characters)</h1>
|
||||
<p>本页包含内容:</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.4" data-basepath=".." data-revision="1402300340983">
|
||||
<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>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.5" data-basepath=".." data-revision="1402300340983">
|
||||
<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>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.6" data-basepath=".." data-revision="1402300340983">
|
||||
<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>
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_79">
|
||||
<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="1402300340983">
|
||||
<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>
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_80">
|
||||
<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="1402300340983">
|
||||
<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>
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_81">
|
||||
<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="1402300340983">
|
||||
<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>
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_82">
|
||||
<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="1402300340983">
|
||||
<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>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.11" data-basepath=".." data-revision="1402300340983">
|
||||
<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>
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_84">
|
||||
<section class="normal" id="section-gitbook_142">
|
||||
|
||||
<h1 id="-methods-">方法(Methods)</h1>
|
||||
<p><strong>方法</strong>是与某些特定类型相关联的功能/函数。类、结构体、枚举都可以定义实例方法;实例方法为指定类型的实例封装了特定的任务与功能。类、结构体、枚举也可以定义类(型)方法(type itself);类型方法与类型自身相关联。类型方法与Objective-C中的类方法(class methods)相似。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.12" data-basepath=".." data-revision="1402300340983">
|
||||
<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>
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_85">
|
||||
<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>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.13" data-basepath=".." data-revision="1402300340983">
|
||||
<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>
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_63">
|
||||
<section class="normal" id="section-gitbook_121">
|
||||
|
||||
<h1 id="-">继承</h1>
|
||||
<p>一个类可以继承另一个类的方法,属性和其它特性。当一个类继承其它类,继承类叫子类,被继承类叫超类(或父类)。在Swift中,继承是区分「类」与其它类型的一个基本特征。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.14" data-basepath=".." data-revision="1402300340983">
|
||||
<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>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.15" data-basepath=".." data-revision="1402300340983">
|
||||
<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>
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_88">
|
||||
<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="1402300340983">
|
||||
<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>
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_89">
|
||||
<section class="normal" id="section-gitbook_147">
|
||||
|
||||
<h1 id="-">自动引用计数</h1>
|
||||
<p>本页包含内容:</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.17" data-basepath=".." data-revision="1402300340983">
|
||||
<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>
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_90">
|
||||
<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="1402300340983">
|
||||
<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>
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_91">
|
||||
<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="1402300340983">
|
||||
<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>
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_92">
|
||||
<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="1402300340983">
|
||||
<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>
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_93">
|
||||
<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="1402300340983">
|
||||
<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>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.22" data-basepath=".." data-revision="1402300340983">
|
||||
<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>
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_96">
|
||||
<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="1402300340983">
|
||||
<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>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2" data-basepath=".." data-revision="1402300340983">
|
||||
<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>
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_100">
|
||||
<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="1402300340983">
|
||||
<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>
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_102">
|
||||
<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="1402300340983">
|
||||
<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>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.3" data-basepath=".." data-revision="1402300340983">
|
||||
<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>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.4" data-basepath=".." data-revision="1402300340983">
|
||||
<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>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.6" data-basepath=".." data-revision="1402300340983">
|
||||
<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>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.7" data-basepath=".." data-revision="1402300340983">
|
||||
<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>
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_101">
|
||||
<section class="normal" id="section-gitbook_159">
|
||||
|
||||
<h1 id="-">特性</h1>
|
||||
<p>特性提供了关于声明和类型的更多信息。在Swift中有两类特性,用于修饰声明的以及用于修饰类型的。例如,<code>required</code>特性,当应用于一个类的指定或便利初始化器声明时,表明它的每个子类都必须实现那个初始化器。再比如<code>noreturn</code>特性,当应用于函数或方法类型时,表明该函数或方法不会返回到它的调用者。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.8" data-basepath=".." data-revision="1402300340983">
|
||||
<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>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.9" data-basepath=".." data-revision="1402300340983">
|
||||
<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>
|
||||
|
||||
@ -44,7 +44,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.10" data-basepath=".." data-revision="1402300340983">
|
||||
<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>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.5" data-basepath=".." data-revision="1402300340983">
|
||||
<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>
|
||||
@ -587,7 +587,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_110">
|
||||
<section class="normal" id="section-gitbook_168">
|
||||
|
||||
<h1 id="-">语句</h1>
|
||||
<p>在 Swift 中,有两种类型的语句:简单语句和控制流语句。简单语句是最常见的,用于构造表达式和声明。控制流语句则用于控制程序执行的流程,Swift 中有三种类型的控制流语句:循环语句、分支语句和控制传递语句。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3" data-basepath=".." data-revision="1402300340983">
|
||||
<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>
|
||||
|
||||
@ -44,7 +44,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="0" data-basepath="." data-revision="1402300340983">
|
||||
<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>
|
||||
@ -585,7 +585,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_59">
|
||||
<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 1402300340984
|
||||
# Revision 1402300498180
|
||||
|
||||
CACHE:
|
||||
index.html
|
||||
|
||||
File diff suppressed because one or more lines are too long
@ -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!
|
||||
|
||||
Reference in New Issue
Block a user