update 03/02

This commit is contained in:
numbbbbb
2014-06-09 21:37:47 +08:00
parent 853ba2fcfe
commit a00d973e61
41 changed files with 715 additions and 41 deletions

View File

@ -46,7 +46,7 @@
<div class="book" data-level="1.1" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="1.1" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="1.2" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="1.2" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="1" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="1" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.1" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="2.1" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.2" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="2.2" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.3" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="2.3" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.4" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="2.4" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.5" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="2.5" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.6" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="2.6" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.7" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="2.7" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.8" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="2.8" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.9" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="2.9" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.10" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="2.10" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.11" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="2.11" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.12" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="2.12" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.13" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="2.13" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.14" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="2.14" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.15" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="2.15" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.16" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="2.16" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.17" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="2.17" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.18" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="2.18" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.19" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="2.19" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.20" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="2.20" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.21" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="2.21" data-basepath=".." data-revision="1402311241483">
<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,6 +587,487 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_39">
<h1 id="-">协议</h1>
<p><code>Protocol(协议)</code>定义了用于完成某项任务或功能的方法,属性等,它不具备任何功能的细节实现,只用来<strong>统一</strong>方法,属性等的名称和其类型.(<em>译者注: 其他语言中也把 <code>Portocol</code> 称为 <code>Interface(接口)</code></em> ).协议可以被<code>类,枚举,结构体</code>采纳并实现,任意满足了协议要求的<code>类,枚举,结构体</code>被称之为<code>协议遵循者</code>.</p>
<p>协议可以要求其<code>遵循者</code>必须具备的某些特定的<code>属性,方法,操作符,下标</code>.</p>
<h2 id="-">协议的语法</h2>
<p><code>协议</code>的定义和<code>类,结构体,枚举</code>的定义非常相似:</p>
<pre><code>protocol SomeProtocol {
// 此处书写协议的内容
}
</code></pre><p><code>类型名称</code>后加上<code>协议名称</code> ,并用冒号<code>:</code>分隔,从而实现协议;当实现多个协议时,各协议之间用逗号<code>,</code>分隔.</p>
<pre><code>struct SomeStructure: FirstProtocol, AnotherProtocol{
// 此处书写结构体的定义
}
</code></pre><p>当某个类实现了协议,并含有父类时,应当把父类名放在所有的协议名称之前</p>
<pre><code>class SomeClass: SomeSuperClass, FirstProtocol, AnotherProtocol{
// 此处书写类的定义
}
</code></pre><h2 id="-">属性要求</h2>
<p><code>协议</code>能够要求其<code>遵循者</code>必须拥有<strong>特定名称和类型</strong><code>实例属性(instance property)</code><code>类属性 (type property)</code>,也可以指定协议中的属性的<code>settable</code><code>gettable</code>,但它并不要求<code>属性</code><code>存储型属性(stored property)</code>还是<code>计算型属性(calculate property)</code>.</p>
<p>当协议要求其中的<code>某个属性</code><code>gettable</code>时,即使实现了它的<code>setter</code>也不会出错. (<em>译者注:此小节术语较多,可参阅属性章节</em>).</p>
<p>属性通常被声明为变量,通过前置<code>var</code>关键字. 在属性声明后写上<code>{ get set }</code>指定属性为可读写的.<code>{ get }</code>用来描述属性为可读的.</p>
<pre><code>protocol SomeProtocol {
var musBeSettable : Int { get set }
var doesNotNeedToBeSettable: Int { get }
}
</code></pre><p>当协议用来被类实现时,使用<code>class</code>关键字来说明该属性为类成员 ; 当协议被结构体或枚举实现时,则使用<code>static</code>关键字来说明</p>
<pre><code>protocol AnotherProtocol {
class var someTypeProperty: Int { get set }
}
</code></pre><p>下边的协议包含了一个实例属性.</p>
<pre><code>protocol FullyNamed {
var fullName: string { get }
}
</code></pre><p><code>FullyNamed</code> 定义了一个拥有 <code>fullName</code> 属性的协议. 该协议要求其 <code>遵循者</code> 必须拥有一个名为 <code>fullName</code>, 类型为 <code>String</code> 的可读属性.</p>
<p>下例是一个<code>遵循</code><code>FullyNamed</code> 协议的简单结构体</p>
<pre><code>struct Person: FullyNamed{
var fullName: String
}
let john = Person(fullName: &quot;John Appleseed&quot;)
//john.fullName 为 &quot;John Appleseed&quot;
</code></pre><p>定义一个名为<code>Person</code>并实现了<code>FullyNamed</code>协议的结构体. 每一个<code>Person</code>实例都拥有一个<code>String</code>类型,名为<code>fullName</code><code>存储型属性</code>,它满足了<code>FullyNamed</code>协议的要求,也就是说 <code>Person</code>完整的遵循了该协议.(如果协议未被完整遵循,Swift编译时会报出错误). </p>
<p>下例是一个遵循了<code>FullyNamed</code>协议的类:</p>
<pre><code>class Starship: FullyNamed {
var prefix: String?
var name: String
init(name: String, prefix: Stirng? = nil ) {
self.anme = name
self.prefix = prefix
}
var fullName: String {
return (prefix ? prefix ! + &quot; &quot; : &quot; &quot;) + name
}
}
var ncc1701 = Starship(name: &quot;Enterprise&quot;, prefix: &quot;USS&quot;)
// ncc1701.fullName == &quot;USS Enterprise&quot;
</code></pre><p>该类将<code>fullName</code>实现为<code>计算型只读属性</code>.它的每一个实例都有一个名为<code>name</code>的必备属性和一个名为<code>prefix</code>的可选属性. 当<code>prefix</code>存在时,将<code>prefix</code>插入到<code>name</code>之前来为<code>Starship</code>构建<code>fullName</code></p>
<h2 id="-">方法要求</h2>
<p><code>协议</code>可以要求其<code>遵循者</code>必备某些特定的<code>实例方法</code><code>类方法</code>. 这些方法作为<code>协议</code>的一部分,像普通的方法一样写在协议体中,但却不需要方法体.而且,协议中的方法同样支持可变参数. </p>
<blockquote>
<p>笔记: 协议中的<code>方法</code>的语法同普通<code>方法</code>一样,但是不支持<code>默认参数</code>. </p>
</blockquote>
<p>在协议中定义<code>类方法</code><code>类属性</code>一样,只需在方法前加上<code>class</code>关键字; 当协议用于被<code>枚举</code><code>结构体</code>遵循时,<code>类方法</code>的关键字需要换为<code>static</code>:</p>
<pre><code>protocol SomeProtocol {
class func someTypeMethod()
}
</code></pre><p>下边是拥有一个实例方法的协议的例子</p>
<pre><code>protocol RandomNumberGenerator {
func random() -&gt; Double
}
</code></pre><p><code>RandomNumberGenerator</code> 协议要求其<code>遵循者</code>必须拥有一个名为<code>random</code>, 返回值类型为<code>Double</code> 的实例方法. (这里假设随机数在 [0,1] 之间). 该协议只为生成随机数提供了一个统一的函数名称,而不去做具体的实现工作. </p>
<p>这里有一个名为<code>LinearCongruentialGenerator</code><code>遵循</code><code>RandomNumberGenerator</code>协议的类. 该类实现了名为 <em>linear congruential generator</em>(线性同余生成器) 的假随机数算法</p>
<pre><code>class LinearCongruentialGenerator: RandomNumberGenerator {
var lastRandom = 42.0
let m = 139968.0
let a = 3877.0
let c = 29573.0
func random() -&gt; Double {
lastRandom = ((lastRandom * a + c) % m)
return lastRandom / m
}
}
let generator = LinearCongruentialGenerator()
println(&quot;Here&#39;s a random number: \(generator.random())&quot;)
// 输出 : &quot;Here&#39;s a random number: 0.37464991998171&quot;
println(&quot;And another one: \(generator.random())&quot;)
// 输出 : &quot;And another one: 0.729023776863283&quot;
</code></pre><h2 id="-">突变方法要求</h2>
<p>有时不得不在方法中改变实例的所属的类型.在基于<code>Value Type</code>的实例方法(<em>译者注:指结构体和枚举中的方法</em>)的<code>func</code>前加上<code>mutating</code>关键字来表明该方法允许改变该实例和其属性的所属类型. 这一突变过程在 [Modifyting Value Types from Within Instance Methods][1] 一节中有详细描述.</p>
<p>如果你打算在协议中定义一个能够改变实例所属类型的实例方法,只需要在方法前加上<code>mutating</code>关键字.使得结构体和枚举遵循该协议.(<em>译者注:类中的变量为 Reference Type ,可以轻易的改变实例及其属性的类型 . 而结构体和枚举中的变量都为 Value Type, 因此需要加上<code>mutating</code>关键字才能更改它们的所属类型</em>)</p>
<blockquote>
<p>当协议的实例方法标记为<code>mutating</code>时,在结构体或枚举的实现该方法时中,<code>mutating</code>关键字是不必可少的;当使用类遵循该协议时,则不需要为这个实例方法前加 <code>mutating</code> 关键字.</p>
</blockquote>
<p>下例定义了一个名为<code>Togglable</code>,含有一个<code>toggle</code>方法的协议.通过名称猜测,<code>toggle</code>方法应该是用来 <strong>切换或恢复</strong> 某个属性的状态使用的.<code>toggle</code>方法前含有<code>mutating</code>关键字,用以标识其可以更改其<code>遵循者</code>的实例及其属性的所属类型.</p>
<pre><code>protocol Togglable {
mutating func toggle()
}
</code></pre><p>如果你使用枚举或结构体来实现<code>Togglabl</code>协议时,必须在枚举或接头体的<code>toggle</code>方法前加上<code>mutating</code>关键字.</p>
<p>下例定义了一个名为<code>OnOffSwitch</code>的枚举. 这个枚举可以切换<code>On</code>,<code>Off</code>两种状态. 该枚举中的 <code>toggle</code>含有<code>mutating</code>标记,用以匹配<code>Togglable</code>协议的方法要求:</p>
<pre><code>enum OnOffSwitch: Togglable {
case Off, On
mutating func toggle() {
switch self {
case Off:
self = On
case On:
self = Off
}
}
}
var lightSwitch = OnOffSwitch.Off
lightSwitch.toggle()
//lightSwitch 现在的值为 .On
</code></pre><h2 id="-">协议作为类型</h2>
<p>尽管<code>协议</code>本身不实现任何功能,但你可以将它当做<code>类型</code>来使用.</p>
<p>包括: </p>
<ul>
<li>作为函数,方法或构造器中的参数类型,返回值类型</li>
<li>作为常量,变量,属性的类型</li>
<li>作为数组,字典或其他容器中的元素类型</li>
</ul>
<blockquote>
<p>Note: 协议是一种类型,因此你应该向其他类型那样(Int,Double,String),使用驼峰式写法来书写协议</p>
</blockquote>
<p>这里有一个使用协议类型的例子:</p>
<pre><code>class Dice {
let sides: Int
let generator: RandomNumberGenerator
init(sides: Int, generator: RandomNumberGenerator) {
self.sides = sides
self.generator = generator
}
func roll() -&gt; Int {
return Int(generator.random() * Double(sides)) +1
}
}
</code></pre><p>这里定义了一个名为 <code>Dice</code>的类,用来代表桌游中的N个面的骰子.</p>
<p> <code>Dice</code>拥有名为<code>sides</code><code>generator</code>的两个属性,前者用来表示骰子有几个面,后者用来为骰子提供一个随机数的生成器</p>
<p><code>generator</code>是一个<code>RandomNumberGenerator</code>协议类型的属性.因此,你可以为它赋值任何<code>遵循</code>该协议的类型.</p>
<p><code>Dice</code>也拥有一个<code>构造器(initializer)</code>用来设置它的初始状态.构造器中含有一个名为<code>generator</code>,类型为<code>RandomNumberGenerator</code>的形参.你可以在此传入任意遵循<code>RandomNumberGenerator</code>协议的类型.</p>
<p><code>roll</code>是一个用以返回骰子面值的实例方法.该方法先调用<code>generator</code><code>random</code>方法来创建一个 [0-1] 之间的随机数,然后使用这个随机数来生成骰子的面值. 这里的<code>generator</code> 被声明为采纳了<code>RandomNumberGenerator</code>协议,用以确保<code>random</code>方法能够被调用 </p>
<p>下例展示了一个使用<code>LinearCongruentialGenerator</code>实例作为随机数生成器的六面骰子</p>
<pre><code>var d6 = Dice(sides: 6,generator: LinearCongruentialGenerator())
for _ in 1...5 {
println(&quot;Random dice roll is \(d6.roll())&quot;)
}
//输出结果
//Random dice roll is 3
//Random dice roll is 5
//Random dice roll is 4
//Random dice roll is 5
//Random dice roll is 4
</code></pre><h2 id="-">委托(代理)</h2>
<p>委托是一种设计模式(<em>译者注:想起了那年 UITableViewDelegate 中的奔跑,那是我逝去的Objective-C...</em>),它允许类或结构体将一些需要它们负责的功能或任务交由(委托)给其他的类型.</p>
<p>代理设计模式的实现很简单,首先定义一个<code>协议</code><code>封装</code>那些需要被委托的功能的<code>函数和方法</code>, 然后确保其<code>遵循者</code>拥有这些被委托的<code>函数和方法</code>.
委托模式可以用来响应特定的动作,或接收外部数据源提供的数据而无需要知道外部数据源的类型.</p>
<p>下边这个例子展示了两个基于骰子游戏的两个协议: </p>
<pre><code>protocol DiceGame {
var dice: Dice { get }
func play()
}
protocol DiceGameDelegate {
func gameDidStart(game: DiceGame)
func game(game: DiceGame, didStartNewTurnWithDiceRoll diceRoll:Int)
func gameDidEnd(game: DiceGame)
}
</code></pre><p><code>DiceGame</code>协议可以被任何包含骰子的游戏采纳.<code>DiceGameDelegate</code>协议可以用来追踪<code>DiceGame</code>的游戏过程</p>
<p>下边是一个 <code>Snakes and Ladders</code> 游戏的新版本(<a href="2">Control Flow</a><em>含有关于该游戏的介绍</em>).新版本使用<code>Dice</code>中的骰子,实现<code>DiceGame</code><code>DiceGameDelegate</code>协议</p>
<pre><code>class SnakesAndLadders: DiceGame {
let finalSquare = 25
let dic = Dice(sides: 6, generator: LinearCongruentialGenerator())
var square = 0
var board: Int[]
init() {
board = Int[](count: finalSquare + 1, repeatedValue: 0)
board[03] = +08; board[06] = +11; borad[09] = +09; board[10] = +02
borad[14] = -10; board[19] = -11; borad[22] = -02; board[24] = -08
}
var delegate: DiceGameDelegate?
func play() {
square = 0
delegate?.gameDidStart(self)
gameLoop: while square != finalSquare {
let diceRoll = dice.roll()
delegate?.game(self,didStartNewTurnWithDiceRoll: diceRoll)
switch square + diceRoll {
case finalSquare:
break gameLoop
case let newSquare where newSquare &gt; finalSquare:
continue gameLoop
default:
square += diceRoll
square += board[square]
}
}
delegate?.gameDIdEnd(self)
}
}
</code></pre><p>更详细的<code>Shakes and Ladders</code>游戏描述,请在 <a href="3">Control Flow -&gt; Break</a> 章节查看.<br>这个版本的游戏被包装到了名为<code>SnakeAndLadders</code>并实现了<code>DiceGame</code>协议的类中. 该类含有一个可读的<code>dice</code>属性和一个<code>play</code>方法用来遵循协议.</p>
<p>游戏的初始化设置(<code>setup</code>)被为类的构造器(<code>init()</code>)来实现.所有的游戏逻辑被转移到了协议方法<code>play</code>中.</p>
<p>注意:<code>delegate</code> 被定义为遵循<code>DiceGameDelegate</code>协议的可选属性,因为委托并不是该游戏的必备条件.</p>
<p><code>DicegameDelegate</code> 提供了三个方法用来追踪游戏过程.被放置于游戏的逻辑中,即<code>play()</code>方法内.分别在游戏开始时,新一轮开始时,游戏结束时被调用.</p>
<p>因为<code>delegate</code>是一个遵循<code>DiceGameDelegate</code>的可选属性,因此在<code>play()</code>方法中使用了<code>可选链</code>来调用委托方法. 如果<code>delegate</code>属性为<code>nil</code>, 则委托调用<em>优雅的,不含错误的</em>失败.如果<code>delegate</code>不为<code>nil</code>, 则这些委托方法被调用,并且把<code>SnakesAndLadders</code>的这个实例当做参数一并传递</p>
<p>下边的这个例子展示了一个名为<code>DiceGameTracker</code>,实现<code>DiceGameDelegate</code>协议的类</p>
<pre><code>class DiceGameTracker: DiceGameDelegate {
var numberOfTurns = 0
func gameDidStart(game: DiceGame) {
numberOfTurns = 0
if game is SnakesAndLadders {
println(&quot;Started a new game of Snakes and Ladders&quot;)
}
println(&quot;The game is using a \(game.dice.sides)-sided dice&quot;)
}
func game(game: DiceGame, didStartNewTurnWithDiceRoll diceRoll: Int) {
++numberOfTurns
println(&quot;Rolled a \(diceRoll)&quot;)
}
func gameDidEnd(game: DiceGame) {
println(&quot;The game lasted for \(numberOfTurns) turns&quot;)
}
}
</code></pre><p><code>DiceGameTracker</code>实现了<code>DiceGameDelegate</code>协议中要求的全部方法.用来记录游戏已经进行的轮数. 当游戏开始时,<code>numberOfTurns</code>属性被赋值为0; 在每新一轮中递加; 游戏结束后,输出打印游戏的总轮数.</p>
<p><code>gameDidStart</code>使用<code>game</code>参数来打印游戏的一些介绍信息.<code>game</code>的类型是<code>DiceGame</code>而不是 <code>SnakeAndLadders</code>, 因此<code>gameDidStart</code>只能访问和使用<code>DiceGame</code>协议中的成员. 但是仍然可以使用类型转换来访问其实例. 在<code>gameDidStart</code>中,当<code>game</code><code>SnakesAndLadders</code>的实例时,会打印出适当的信息. 因为<code>game</code>是被视为遵循了<code>DiceGame</code>协议的属性,也就是说它拥有<code>dice</code>属性,所以<code>gameDidStart</code>方法可以访问和打印<code>dice</code><code>sides</code>属性,而无需知道这是一场什么游戏....</p>
<p>这是<code>DiceGameTracker</code>的运行实例:</p>
<pre><code>“let tracker = DiceGameTracker()
let game = SnakesAndLadders()
game.delegate = tracker
game.play()
// Started a new game of Snakes and Ladders
// The game is using a 6-sided dice
// Rolled a 3
// Rolled a 5
// Rolled a 4
// Rolled a 5
// The game lasted for 4 turns”
</code></pre><h2 id="-extension-">在延展(Extension)中添加协议成员</h2>
<p>即使无法修改源代码,依然可以通过<code>延展(Extension)</code>来扩展已存在类型(<em>译者注: 类,结构体,枚举等</em>).<code>延展</code>中可以为已存在的类型添加属性,方法,下标,协议等成员. 详情请在<a href="4">延展</a>章节中查看.</p>
<blockquote>
<p>笔记: 通过延展为已存在的类型增加协议时,该类型的实例会自动添加协议中的方法</p>
</blockquote>
<p>下例中<code>TextRepresentable</code>协议含有一个<code>asText</code> 方法,可以被任何类型<code>遵循</code></p>
<pre><code>protocol TextRepresentable {
func asText() -&gt; String
}
</code></pre><p>通过延展为为上一节中的<code>Dice</code>类实现并遵循<code>TextRepresentable</code>协议</p>
<pre><code>extension Dice: TextRepresentable {
cun asText() -&gt; String {
return &quot;A \(sides)-sided dice&quot;
}
}
</code></pre><p><code>Dice</code>类型的实例现在可以被视为<code>TextRepresentable</code>类型:</p>
<pre><code>let d12 = Dice(sides: 12,generator: LinearCongruentialGenerator())
println(d12.asText())
// 输出 &quot;A 12-sided dice&quot;
</code></pre><p><code>SnakesAndLadders</code> 类也可通过<code>延展</code>来遵循协议:</p>
<pre><code>extension SnakeAndLadders: TextRepresentable {
func asText() -&gt; String {
return &quot;A game of Snakes and Ladders with \(finalSquare) squares&quot;
}
}
println(game.asText())
// 输出 &quot;A game of Snakes and Ladders with 25 squares&quot;
</code></pre><h2 id="-">通过延展声明协议</h2>
<p>如果一个类型已经实现了协议中的所有要求,却没有声明时,可以通过声明空<code>延展</code>来采纳协议:</p>
<pre><code>struct Hamster {
var name: String
func asText() -&gt; String {
return &quot;A hamster named \(name)&quot;
}
}
extension Hamster: TextRepresentabl {}
</code></pre><p>现在开始,<code>Hamster</code>的实例可以被当做<code>TextRepresentable</code>类型使用</p>
<pre><code>let simonTheHamster = Hamster(name: &quot;Simon&quot;)
let somethingTextRepresentable: TextRepresentabl = simonTheHamester
println(somethingTextRepresentable.asText())
// 输出 &quot;A hamster named Simon&quot;
</code></pre><blockquote>
<p>注意: 类型不会因为满足了某协议而直接改变,你必须为它做出明显的实现协议声明</p>
</blockquote>
<h2 id="-">集合中的协议类型</h2>
<p>协议类型可以被集合使用,表示集合中的元素均为协议类型:</p>
<pre><code>let things: TextRepresentable[] = [game,d12,simoTheHamster]
</code></pre><p><code>things</code>数组可以被直接遍历,并调用其中元素的<code>asText()</code>函数:</p>
<pre><code>for thing in things {
println(thing.asText())
}
// A game of Snakes and Ladders with 25 squares
// A 12-sided dice
// A hamster named Simon
</code></pre><p>上文代码中,<code>thing</code>被认为是<code>TextRepresentable</code>类型而不是<code>Dice</code>,<code>DiceGame</code>,<code>Hamster</code>等类型.因此,可以在循环中调用它们的<code>asText</code>方法</p>
<h2 id="-">协议的继承</h2>
<p>协议可以通过继承一个或多个其他协议.继承协议的语法和继承类的语法相似,多个协议间用逗号<code>,</code>分隔</p>
<pre><code>protocol InheritingProtocol: SomeProtocol, AnotherProtocol {
// 协议定义
}
</code></pre><p>下边是一个继承了<code>TextRepresentable</code>的协议</p>
<pre><code>protocol PrettyTextRepresentable: TextRepresentable {
func asPrettyText() -&gt; String
}
</code></pre><p><code>PrettyTextRepresentable</code>协议继承自<code>TextRepresentable</code>协议.任何实现<code>PrettyTextRepresentable</code>协议的类型,也需要<code>遵循</code>TextRepresentable`协议.</p>
<p>用延展为<code>SnakesAndLadders</code>遵循<code>PrettyTextRepresentable</code>协议:</p>
<pre><code>extension SnakesAndLadders: PrettyTextRepresentable {
func asPrettyText() -&gt; String {
var output = asText() + &quot;:\n&quot;
for index in 1...finalSquare {
switch board[index] {
case let ladder where ladder &gt; 0:
output += &quot;&quot;
case let snake where snake &lt; 0:
output += &quot;&quot;
default:
output += &quot;&quot;
}
}
return output
}
}
</code></pre><p>上边的延展为<code>SnakesAndLadders</code>遵循了<code>PrettyTextRepresentabel</code>协议.在<code>for in</code>中迭代出了<code>board</code>数组中的每一个元素:</p>
<ul>
<li>当数组中元素的值大于0时,用<code></code>表示</li>
<li>当数组中元素的值小于0时,用<code></code>表示</li>
<li>当数组中元素的值等于0时,用<code></code>表示</li>
</ul>
<p>任意<code>SankesAndLadders</code>的实例都可以使用<code>asPrettyText()</code>方法.</p>
<pre><code>println(game.asPrettyText())
// A game of Snakes and Ladders with 25 squares:
// ○ ○ ▲ ○ ○ ▲ ○ ○ ▲ ▲ ○ ○ ○ ▼ ○ ○ ○ ○ ▼ ○ ○ ▼ ○ ▼ ○
</code></pre><h2 id="-">协议合成</h2>
<p>一个协议可由多个协议组成,称为<code>协议合成(protocol composition)</code>,采用<code>protocol&lt;SomeProtocol, AnotherProtocol&gt;</code>这样的语法.当有多个协议时,中间以<code>,</code>分隔.</p>
<p>举个栗子:</p>
<pre><code>protocol Named {
var name: String { get }
}
protocol Aged {
var age: Int { get }
}
struct Person: Named, Aged {
var name: String
var age: Int
}
func wishHappyBirthday(celebrator: protocol&lt;Named, Aged&gt;) {
println(&quot;Happy birthday \(celebrator.name) - you&#39;re \(celebrator.age)!&quot;)
}
let birthdayPerson = Person(name: &quot;Malcolm&quot;, age: 21)
wishHappyBirthday(birthdayPerson)
// 输出 &quot;Happy birthday Malcolm - you&#39;re 21!
</code></pre><p>上例中,<code>Named</code>协议中含有<code>String</code>类型的<code>name</code>属性;<code>Aged</code>协议中含有<code>Int</code>类型的<code>age</code>属性.<code>Person</code>结构体<code>遵循</code>了这两个协议.</p>
<p>此外还定义了<code>wishHappyBirthday</code>函数,该函数的形参<code>celebrator</code>的类型为<code>protocol&lt;Named,Aged&gt;</code>,也就是说可以接受任意<code>遵循</code>了这两个协议的<code>类型</code>.</p>
<blockquote>
<p>笔记: <code>协议合成</code>并不会生成一个新协议,而是将多个协议合成为一个临时的协议.</p>
</blockquote>
<h2 id="-">检验协议的一致性</h2>
<p>使用<code>is</code><code>as</code>可以检验协议一致性,也可以将协议转换为特定的其他协议类型.检验与转换的语法和之前相同(<em>详情查看<a href="5">Typy Casting章节</a></em>):</p>
<ul>
<li><code>is</code>操作符用来检查实例是否<code>遵循</code>了某个<code>协议</code>. </li>
<li><code>as?</code>返回一个可选值,当实例<code>遵循</code>协议时,返回该协议类型;否则返回<code>nil</code></li>
<li><p><code>as</code>可以用来强制向下转型.</p>
<p> @objc protocol HasArea {</p>
<pre><code> var area: Double { get }
</code></pre><p> }</p>
</li>
</ul>
<blockquote>
<p>笔记: <code>@objc</code>用来表示协议是可选的,也可以用来表示暴露给<code>Objective-C</code>的代码,在<em>[Using Siwft with Cocoa and Objectivei-c]</em>(6)一节中有详细介绍.
<code>@objc</code>型协议只对<code></code>有效,因此只能在<code></code>中检查协议的一致性.</p>
</blockquote>
<pre><code>class Circle: HasArea {
let pi = 3.1415927
var radius: Double
var area: Double { return pi * radius * radius }
init(radius: Double) { self.radius = radius }
}
class Country: HasArea {
var area: Double
init(area: Double) { self.area = area }
}
</code></pre><p><code>Circle</code><code>Country</code>都遵循了<code>HasArea</code>协议,前者把<code>area</code>写为<code>计算型属性</code>,后者则把<code>area</code>写为<code>存储型属性</code></p>
<p>下边是一个没事实现<code>HasArea</code>协议的<code>Animal</code>类:</p>
<pre><code>class Animal {
var legs: Int
init(legs: Int) { self.legs = legs }
}
</code></pre><p><code>Circle,Country,Animal</code>并没有一个相同的基础类,可以使用<code>AnyObject</code>类型的数组来装在他们的实例:</p>
<pre><code>let objects: AnyObject[] = [
Circle(radius: 2.0),
Country(area: 243_610),
Animal(legs: 4)
]
</code></pre><p>在迭代时可以检查<code>object</code>数组的元素是否<code>遵循</code><code>HasArea</code>协议:</p>
<pre><code>for object in objects {
if let objectWithArea = object as? HasArea {
println(&quot;Area is \(objectWithArea.area)&quot;)
} else {
println(&quot;Something that doesn&#39;t have an area&quot;)
}
}
// Area is 12.5663708
// Area is 243610.0
// Something that doesn&#39;t have an area
</code></pre><p>当数组中的元素遵循<code>HasArea</code>协议时,通过<code>as?</code>操作符将其<code>可选绑定(optional binding)</code><code>objectWithArea</code>常量上.</p>
<p><code>objects</code>数组中元素的类型并不会被改变,但是当它们被赋值给<code>objectWithArea</code>时只被视为<code>HasArea</code>类型,并且只有<code>area</code>属性可以被访问.</p>
<h2 id="-">可选协议要求</h2>
<p>可选协议含有可选成员,其<code>遵循者</code>可以选择是否实现这些成员.在协议中使用<code>@optional</code>关键字作为前缀来定义可选成员.</p>
<p>可选协议在调用时使用<code>可选链</code>,详细内容在<a href="7">Optional Chaning</a>章节中查看.</p>
<p><code>someOptionalMethod?(someArgument)</code>一样,你可以在可选方法名称后加上<code>?</code>来检查该方法是否被实现.<code>可选方法</code><code>可选属性</code>都会返回一个<code>可选值(optional value)</code>,当其不可访问时,<code>?</code>之后语句不会执行,并返回<code>nil</code></p>
<blockquote>
<p>笔记: 可选协议只能在含有<code>@objc</code>前缀的协议中生效.且<code>@objc</code>的协议只能被<code></code>遵循</p>
</blockquote>
<p>下问定义了整型计数器<code>Counter</code>类,该类使用外部的数据源来提供<code>增量值(increment amount)</code>. 数据源定义为<code>CounterDataSource</code>类型的协议,如下所示</p>
<pre><code>@objc protocol CounterDataSource {
@optional func incrementForCount(count: Int) -&gt; Int
@optional var fixedIncrement: Int { get }
}
</code></pre><p><code>CounterDataSource</code>含有<code>incrementForCount</code><code>可选方法</code><code>fiexdIncrement</code><code>可选属性</code>.</p>
<blockquote>
<p>笔记: <code>CounterDataSource</code>中的属性和方法都是可选的,因此可以不提供所需要的属性或方法来实现它.尽管技术上允许,但这并不提倡这样用.</p>
</blockquote>
<p><code>Counter</code>类的定义在下边,它含有一个名为<code>dataSource</code>,<code>CounterDataSource?</code>类型的可选属性:</p>
<pre><code>@objc class Counter {
var count = 0
var dataSource: CounterDataSource?
func increment() {
if let amount = dataSource?.incrementForCount?(count) {
count += amount
} else if let amount = dataSource?.fixedIncrement? {
count += amount
}
}
}
</code></pre><p><code>count</code>属性用于存储当前的值,<code>increment</code>方法用来为<code>count</code>赋值.</p>
<p><code>increment</code>方法通过<code>可选链</code>,尝试从两种<code>可选成员</code>中获取<code>count</code>.</p>
<ol>
<li>由于<code>dataSource</code>可能为<code>nil</code>,因此在<code>dataSource</code>后边加上了<code>?</code>标记来表明只在<code>dataSource</code>非空时才去调用incrementForCount`方法.</li>
<li>即使<code>dataSource</code>存在,但是也无法保证其是否实现了<code>incrementForCount</code>方法,因此在<code>incrementForCount</code>方法后边也加有<code>?</code>标记</li>
</ol>
<p>在调用<code>incrementForCount</code>方法后,<code>Int</code><code>可选值</code>通过<code>可选绑定(optional binding)</code>自动拆包并赋值给常量<code>amount</code>.</p>
<p><code>incrementForCount</code>不能被调用时,尝试使用<code>可选属性``fixedIncrement</code>来代替.</p>
<p>下边是一个简单的<code>CounterDataSource</code>协议的实现.</p>
<pre><code>class ThreeSource: CounterDataSource {
let fixedIncrement = 3
}
</code></pre><p>可以使用<code>ThreeSource</code>作为数据源开实例化一个<code>Counter</code>:</p>
<pre><code>var counter = Counter()
counter.dataSource = ThreeSource()
for _ in 1...4 {
counter.increment()
println(counter.count)
}
// 3
// 6
// 9
// 12
</code></pre><p>下边是一个更为复杂的数据源实现:</p>
<pre><code>class TowardsZeroSource: CounterDataSource {
func incrementForCount(count: Int) -&gt; Int {
if count == 0 {
return 0
} else if count &lt; 0 {
return 1
} else {
return -1
}
}
}
</code></pre><p><code>TowardZeroSource</code>类实现了<code>CounterDataSource</code><code>可选方法``incrementForCount</code>.</p>
<p>下边是执行的代码:</p>
<pre><code>counter.count = -4
counter.dataSource = TowardsZeroSource()
for _ in 1...5 {
counter.increment()
println(counter.count)
}
// -3
// -2
// -1
// 0
// 0
</code></pre>
</section>
</div>
</div>
</div>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.22" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="2.22" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.23" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="2.23" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="2" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3.1" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="3.1" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3.2" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="3.2" data-basepath=".." data-revision="1402311241483">
<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,6 +587,199 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_50">
<h1 id="-">语法结构</h1>
<p>本页包含内容:</p>
<ul>
<li>空白与注释(<em>Whitespace and Comments</em></li>
<li>标识符(<em>Identifiers</em></li>
<li>关键字(<em>Keywords</em></li>
<li>字面量(<em>Literals</em></li>
<li>运算符(<em>Operators</em></li>
</ul>
<p>Swift 的“语法结构(<em>lexical structure</em>)”描述了如何在该语言中用字符序列构建合法标记,组成该语言中最底层的代码块,并在之后的章节中用于描述语言的其他部分。</p>
<p>通常,标记在随后介绍的语法约束下,由 Swift 源文件的输入文本中提取可能的最长子串生成。这种方法称为“最长匹配项(<em>longest match</em>)”,或者“最大适合”(<em>maximal munch</em>)。</p>
<h2 id="-">空白与注释</h2>
<p>空白(<em>whitespace</em>)有两个用途:分隔源文件中的标记和区分运算符属于前缀还是后缀,(参见 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/doc/uid/TP40014097-CH30-XID_871" target="_blank">运算符</a>)在其他情况下则会被忽略。以下的字符会被当作空白:空格(<em>space</em>U+0020、换行符<em>line feed</em>U+000A、回车符<em>carriage return</em>U+000D、水平 tab<em>horizontal tab</em>U+0009、垂直 tab<em>vertical tab</em>U+000B、换页符<em>form feed</em>U+000C以及空<em>null</em>U+0000</p>
<p>注释(<em>comments</em>)被编译器当作空白处理。单行注释由 <code>//</code> 开始直到该行结束。多行注释由 <code>/*</code> 开始,以 <code>*/</code> 结束。可以嵌套注释,但注意注释标记必须匹配。</p>
<h2 id="-">标识符</h2>
<p>标识符(<em>identifiers</em>)可以由以下的字符开始:大写或小写的字母 <code>A</code><code>Z</code>、下划线 <code>_</code>、基本多语言面(<em>Basic Multilingual Plane</em>)中的 Unicode 非组合字符以及基本多语言面以外的非专用区(<em>Private Use Area</em>)字符。首字符之后,标识符允许使用数字和 Unicode 字符组合。</p>
<p>使用保留字(<em>reserved word</em>)作为标识符,需要在其前后增加反引号 <code>`</code>。例如,<code>class</code> 不是合法的标识符,但可以使用 <code>`class`</code>。反引号不属于标识符的一部分,<code>`x`</code><code>x</code> 表示同一标识符。</p>
<p>闭包(<em>closure</em>)中如果没有明确指定参数名称,参数将被隐式命名为 <code>$0</code><code>$1</code><code>$2</code>... 这些命名在闭包作用域内是合法的标识符。</p>
<blockquote>
<p>标识符语法</p>
<p><em>identifier</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-head" target="_blank">identifier-head­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-characters" target="_blank">identifier-characters</a>­ <em>opt</em></p>
<p><em>identifier</em> → `­ <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-head" target="_blank">identifier-head­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-characters" target="_blank">identifier-characters­</a> <em>opt­</em> `­</p>
<p><em>identifier</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/implicit-parameter-name" target="_blank">implicit-parameter-name­</a></p>
<p><em>identifier-list</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">identifier­</a> | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier" target="_blank">identifier­</a> , <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-list" target="_blank">­identifier-list</a>­</p>
<p><em>identifier-head</em> → A 到 Z 大写或小写字母</p>
<p><em>identifier-head</em> → U+00A8, U+00AA, U+00AD, U+00AF, U+00B2U+00B5, 或 U+00B7U+00BA</p>
<p><em>identifier-head</em> → U+00BCU+00BE, U+00C0U+00D6, U+00D8U+00F6, 或 U+00F8U+00FF</p>
<p><em>identifier-head</em> → U+0100U+02FF, U+0370U+167F, U+1681U+180D, 或 U+180FU+1DBF</p>
<p><em>identifier-head</em> → U+1E00U+1FFF</p>
<p><em>identifier-head</em> → U+200BU+200D, U+202AU+202E, U+203FU+2040, U+2054, 或 U+2060U+206F</p>
<p><em>identifier-head</em> → U+2070U+20CF, U+2100U+218F, U+2460U+24FF, 或 U+2776U+2793</p>
<p><em>identifier-head</em> → U+2C00U+2DFF 或 U+2E80U+2FFF</p>
<p><em>identifier-head</em> → U+3004U+3007, U+3021U+302F, U+3031U+303F, 或 U+3040U+D7FF</p>
<p><em>identifier-head</em> → U+F900U+FD3D, U+FD40U+FDCF, U+FDF0U+FE1F, 或 U+FE30U+FE44</p>
<p><em>identifier-head</em> → U+FE47U+FFFD</p>
<p><em>identifier-head</em> → U+10000U+1FFFD, U+20000U+2FFFD, U+30000U+3FFFD, 或 U+40000U+4FFFD</p>
<p><em>identifier-head</em> → U+50000U+5FFFD, U+60000U+6FFFD, U+70000U+7FFFD, 或 U+80000U+8FFFD</p>
<p><em>identifier-head</em> → U+90000U+9FFFD, U+A0000U+AFFFD, U+B0000U+BFFFD, 或 U+C0000U+CFFFD</p>
<p><em>identifier-head</em> → U+D0000U+DFFFD 或 U+E0000U+EFFFD</p>
<p><em>identifier-character</em> → 数字 0 到 9</p>
<p><em>identifier-character</em> → U+0300U+036F, U+1DC0U+1DFF, U+20D0U+20FF, or U+FE20U+FE2F</p>
<p><em>identifier-character</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-head" target="_blank">identifier-head­</a></p>
<p><em>identifier-characters</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-character" target="_blank">identifier-character</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-characters" target="_blank">­identifier-characters­</a> <em>opt­</em></p>
<p><em>implicit-parameter-name</em><strong>$­</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-digits" target="_blank">decimal-digits­</a></p>
</blockquote>
<h2 id="-">关键字</h2>
<p>被保留的关键字(<em>keywords</em>)不允许用作标识符,除非被反引号转义,参见 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/doc/uid/TP40014097-CH30-XID_796" target="_blank">标识符</a></p>
<ul>
<li><p><strong>用作声明的关键字:</strong> <em>class</em><em>deinit</em><em>enum</em><em>extension</em><em>func</em><em>import</em><em>init</em><em>let</em><em>protocol</em><em>static</em><em>struct</em><em>subscript</em><em>typealias</em><em>var</em> </p>
</li>
<li><p><strong>用作语句的关键字:</strong> <em>break</em><em>case</em><em>continue</em><em>default</em><em>do</em><em>else</em><em>fallthrough</em><em>if</em><em>in</em><em>for</em><em>return</em><em>switch</em><em>where</em><em>while</em></p>
</li>
<li><p><strong>用作表达和类型的关键字:</strong> <em>as</em><em>dynamicType</em><em>is</em><em>new</em><em>super</em><em>self</em><em>Self</em><em>Type</em><em>__COLUMN__</em><em>__FILE__</em><em>__FUNCTION__</em><em>__LINE__</em></p>
</li>
<li><p><strong>特定上下文中被保留的关键字:</strong> <em>associativity</em><em>didSet</em><em>get</em><em>infix</em><em>inout</em><em>left</em><em>mutating</em><em>none</em><em>nonmutating</em><em>operator</em><em>override</em><em>postfix</em><em>precedence</em><em>prefix</em><em>right</em><em>set</em><em>unowned</em><em>unowned(safe)</em><em>unowned(unsafe)</em><em>weak</em><em>willSet</em>,这些关键字在特定上下文之外可以被用于标识符。</p>
</li>
</ul>
<h2 id="-">字面量</h2>
<p>字面值表示整型、浮点型数字或文本类型的值,举例如下:</p>
<pre><code>42 // 整型字面量
3.14159 // 浮点型字面量
&quot;Hello, world!&quot; // 文本型字面量
</code></pre><blockquote>
<p>字面量语法</p>
<p><em>literal</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/integer-literal" target="_blank">integer-literal­</a> | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/floating-point-literal" target="_blank">floating-point-literal</a>­ | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/string-literal" target="_blank">string-literal­</a></p>
</blockquote>
<h3 id="-">整型字面量</h3>
<p>整型字面量(<em>integer literals</em>)表示未指定精度整型数的值。整型字面量默认用十进制表示,可以加前缀来指定其他的进制,二进制字面量加 <code>0b</code>,八进制字面量加 <code>0o</code>,十六进制字面量加 <code>0x</code></p>
<p>十进制字面量包含数字 <code>0</code><code>9</code>。二进制字面量只包含 <code>0</code><code>1</code>,八进制字面量包含数字 <code>0</code><code>7</code>,十六进制字面量包含数字 <code>0</code><code>9</code> 以及字母 <code>A</code><code>F</code> (大小写均可)。</p>
<p>负整数的字面量在数字前加减号 <code>-</code>,比如 <code>-42</code></p>
<p>允许使用下划线 <code>_</code> 来增加数字的可读性,下划线不会影响字面量的值。整型字面量也可以在数字前加 <code>0</code>,同样不会影响字面量的值。</p>
<pre><code>1000_000 // 等于 1000000
005 // 等于 5
</code></pre><p>除非特殊指定,整型字面量的默认类型为 Swift 标准库类型中的 <code>Int</code>。Swift 标准库还定义了其他不同长度以及是否带符号的整数类型,请参考 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_411" target="_blank">整数类型</a></p>
<blockquote>
<p>整型字面量语法</p>
<p><em>integer-literal</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/binary-literal" target="_blank">binary-literal­</a></p>
<p><em>integer-literal</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/octal-literal" target="_blank">octal-literal­</a></p>
<p><em>integer-literal</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-literal" target="_blank">decimal-literal­</a></p>
<p><em>integer-literal</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-literal" target="_blank">hexadecimal-literal</a>­</p>
<p><em>binary-literal</em><strong>0b</strong>­ <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/binary-digit" target="_blank">binary-digit</a> ­<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/binary-literal-characters" target="_blank">binary-literal-characters­</a> <em>opt­</em></p>
<p><em>binary-digit</em> → 数字 0 或 1</p>
<p><em>binary-literal-character</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/binary-digit" target="_blank">binary-digit</a>­ | _­</p>
<p><em>binary-literal-characters</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/binary-literal-character" target="_blank">binary-literal-character</a> ­<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/binary-literal-characters" target="_blank">binary-literal-characters­</a> <em>opt­</em></p>
<p><em>octal-literal</em><strong>0o</strong>­ <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/octal-digit" target="_blank">octal-digit</a> ­<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/octal-literal-characters" target="_blank">octal-literal-characters­</a> <em>opt­</em></p>
<p><em>octal-digit</em> → 数字 0 至 7</p>
<p><em>octal-literal-character</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/octal-digit" target="_blank">octal-digit</a>­ | _­</p>
<p><em>octal-literal-characters</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/octal-literal-character" target="_blank">octal-literal-character</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/octal-literal-characters" target="_blank">­octal-literal-characters­</a> <em>opt­</em></p>
<p><em>decimal-literal</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-digit" target="_blank">decimal-digit</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-literal-characters" target="_blank">­decimal-literal-characters­</a> <em>opt­</em></p>
<p><em>decimal-digit</em> → 数字 0 至 9</p>
<p><em>decimal-digits</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-digit" target="_blank">decimal-digit­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-digits" target="_blank">decimal-digits­</a> <em>opt­</em></p>
<p><em>decimal-literal-character</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-digit" target="_blank">decimal-digit­</a> | _­</p>
<p><em>decimal-literal-characters</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-literal-character" target="_blank">decimal-literal-character</a> ­<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-literal-characters" target="_blank">decimal-literal-characters­</a> <em>opt­</em></p>
<p><em>hexadecimal-literal</em><strong>0x</strong> ­<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-literal-characters" target="_blank">­hexadecimal-literal-characters­</a> <em>opt­</em></p>
<p><em>hexadecimal-digit</em> → 数字 0 到 9, a 到 f, 或 A 到 F</p>
<p><em>hexadecimal-literal-character</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit</a>­ | _­</p>
<p><em>hexadecimal-literal-characters</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-literal-character" target="_blank">hexadecimal-literal-character­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-literal-characters" target="_blank">hexadecimal-literal-characters</a> ­<em>opt­</em></p>
</blockquote>
<h3 id="-">浮点型字面量</h3>
<p>浮点型字面量(<em>floating-point literals</em>)表示未指定精度浮点数的值。</p>
<p>浮点型字面量默认用十进制表示(无前缀),也可以用十六进制表示(加前缀 <code>0x</code>)。</p>
<p>十进制浮点型字面量(<em>decimal floating-point literals</em>)由十进制数字串后跟小数部分或指数部分(或两者皆有)组成。十进制小数部分由小数点 <code>.</code> 后跟十进制数字串组成。指数部分由大写或小写字母 <code>e</code> 后跟十进制数字串组成,这串数字表示 <code>e</code> 之前的数量乘以 10 的几次方。例如:<code>1.25e2</code> 表示 <code>1.25 ⨉ 10^2</code>,也就是 <code>125.0</code>;同样,<code>1.25e2</code> 表示 <code>1.25 ⨉ 10^2</code>,也就是 <code>0.0125</code></p>
<p>十六进制浮点型字面量(<em>hexadecimal floating-point literals</em>)由前缀 <code>0x</code> 后跟可选的十六进制小数部分以及十六进制指数部分组成。十六进制小数部分由小数点后跟十六进制数字串组成。指数部分由大写或小写字母 <code>p</code> 后跟十进制数字串组成,这串数字表示 <code>p</code> 之前的数量乘以 2 的几次方。例如:<code>0xFp2</code> 表示 <code>15 ⨉ 2^2</code>,也就是 <code>60</code>;同样,<code>0xFp-2</code> 表示 <code>15 ⨉ 2^-2</code>,也就是 <code>3.75</code></p>
<p>与整型字面量不同,负的浮点型字面量由一元运算符减号 <code>-</code> 和浮点型字面量组成,例如 <code>-42.0</code>。这代表一个表达式,而不是一个浮点整型字面量。</p>
<p>允许使用下划线 <code>_</code> 来增强可读性,下划线不会影响字面量的值。浮点型字面量也可以在数字前加 <code>0</code>,同样不会影响字面量的值。</p>
<pre><code>10_000.56 // 等于 10000.56
005000.76 // 等于 5000.76
</code></pre><p>除非特殊指定,浮点型字面量的默认类型为 Swift 标准库类型中的 <code>Double</code>表示64位浮点数。Swift 标准库也定义 <code>Float</code> 类型表示32位浮点数。</p>
<blockquote>
<p>浮点型字面量语法</p>
<p><em>floating-point-literal</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-literal" target="_blank">decimal-literal</a> ­<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-fraction" target="_blank">decimal-fraction­</a> <em>opt</em> ­<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-exponent" target="_blank">decimal-exponent­</a> <em>opt­</em></p>
<p><em>floating-point-literal</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-literal" target="_blank">hexadecimal-literal</a> ­<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-fraction" target="_blank">hexadecimal-fraction</a> ­<em>opt­</em> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-exponent" target="_blank">hexadecimal-exponent</a>­</p>
<p><em>decimal-fraction</em> → . <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-literal" target="_blank">­decimal-literal­</a></p>
<p><em>decimal-exponent</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/floating-point-e" target="_blank">floating-point-e</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/sign" target="_blank">­sign­</a> <em>opt­</em> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-literal" target="_blank">decimal-literal</a>­</p>
<p><em>hexadecimal-fraction</em> → . <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-literal" target="_blank">­hexadecimal-literal­</a> <em>opt­</em></p>
<p><em>hexadecimal-exponent</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/floating-point-p" target="_blank">floating-point-p­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/sign" target="_blank">sign­</a> <em>opt­</em> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-literal" target="_blank">hexadecimal-literal­</a></p>
<p><em>floating-point-e</em><strong>e­</strong> | <strong>E</strong>­</p>
<p><em>floating-point-p</em><strong>p</strong>­ | <strong>P</strong>­</p>
<p><em>sign</em><strong>+</strong>­ | <strong>-</strong>­</p>
</blockquote>
<h3 id="-">文本型字面量</h3>
<p>文本型字面量(<em>string literal</em>)由双引号中的字符串组成,形式如下:</p>
<pre><code>&quot;characters&quot;
</code></pre><p>文本型字面量中不能包含未转义的双引号 <code>&quot;</code>、未转义的反斜线<code>\</code>、回车符(<em>carriage return</em>)或换行符(<em>line feed</em>)。</p>
<p>可以在文本型字面量中使用的转义特殊符号如下:</p>
<ul>
<li>空字符Null Character<code>\0</code></li>
<li>反斜线Backslash<code>\\</code></li>
<li>水平 Tab Horizontal Tab<code>\t</code></li>
<li>换行符Line Feed<code>\n</code></li>
<li>回车符Carriage Return<code>\r</code></li>
<li>双引号Double Quote<code>\&quot;</code></li>
<li>单引号Single Quote<code>\&#39;</code></li>
</ul>
<p>字符也可以用以下方式表示:</p>
<ul>
<li><code>\x</code> 后跟两位十六进制数字</li>
<li><code>\u</code> 后跟四位十六进制数字</li>
<li><code>\U</code> 后跟八位十六进制数字</li>
</ul>
<p>后跟的数字表示一个 Unicode 码点。</p>
<p>文本型字面量允许在反斜线小括号 <code>\()</code> 中插入表达式的值。插入表达式(<em>interpolated expression</em>)不能包含未转义的双引号 <code>&quot;</code>、反斜线 <code>\</code>、回车符或者换行符。表达式值的类型必须在 <em>String</em> 类中有对应的初始化方法。</p>
<p>例如,以下所有文本型字面量的值相同:</p>
<pre><code>&quot;1 2 3&quot;
&quot;1 2 \(3)&quot;
&quot;1 2 \(1 + 2)&quot;
var x = 3; &quot;1 2 \(x)&quot;
</code></pre><p>文本型字面量的默认类型为 <code>String</code>。组成字符串的字符类型为 <code>Character</code>。更多有关 <code>String</code><code>Character</code> 的信息请参照 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html#//apple_ref/doc/uid/TP40014097-CH7-XID_368" target="_blank">字符串和字符</a></p>
<blockquote>
<p>文本型字面量语法</p>
<p><em>string-literal</em><strong>&quot;­</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/quoted-text" target="_blank">quoted-text</a> <strong>­&quot;­</strong></p>
<p><em>quoted-text</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/quoted-text-item" target="_blank">quoted-text-item­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/quoted-text" target="_blank">quoted-text</a> ­<em>opt­</em></p>
<p><em>quoted-text-item</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/escaped-character" target="_blank">escaped-character­</a></p>
<p><em>quoted-text-item</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">expression­</a> <strong>)</strong>­</p>
<p><em>quoted-text-item</em> → 除 <code>&quot;</code><code>\</code>­、<code>U+000A</code><code>U+000D</code> 以外的任何 Unicode 扩展字符集</p>
<p><em>escaped-character</em><strong>\0­</strong> | <strong>\</strong>­ | <strong>\t­</strong> | <strong>\n</strong>­ | <strong>\r­</strong> | <strong>\&quot;­</strong> | <strong>\&#39;</strong>­</p>
<p><em>escaped-character</em><strong>\x­</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit</a>­ <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit­</a></p>
<p><em>escaped-character</em><strong>\u</strong> ­<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">­hexadecimal-digit</a>­ <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit­</a></p>
<p><em>escaped-character</em><strong>\U­</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit­</a></p>
</blockquote>
<h2 id="-">运算符</h2>
<p>Swift 标准库定义了许多可供使用的运算符,其中大部分在 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html#//apple_ref/doc/uid/TP40014097-CH6-XID_70" target="_blank">基础运算符</a><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html#//apple_ref/doc/uid/TP40014097-CH27-XID_28" target="_blank">高级运算符</a> 中进行了阐述。这里将描述哪些字符能用作运算符。</p>
<p>运算符由一个或多个以下字符组成:
<code>/</code><code>=</code><code>-</code><code>+</code><code>!</code><code>*</code><code>%</code><code>&lt;</code><code>&gt;</code><code>&amp;</code><code>|</code><code>^</code><code>~</code><code>.</code>。也就是说,标记 <code>=</code>, <code>-&gt;</code><code>//</code><code>/*</code><code>*/</code><code>.</code> 以及一元前缀运算符 <code>&amp;</code> 属于保留字,这些标记不能被重写或用于自定义运算符。</p>
<p>运算符两侧的空白被用来区分该运算符是否为前缀运算符(<em>prefix operator</em>)、后缀运算符(<em>postfix operator</em>)或二元运算符(<em>binary operator</em>)。规则总结如下:</p>
<ul>
<li><p>如果运算符两侧都有空白或两侧都无空白,将被看作二元运算符。例如:<code>a+b</code><code>a + b</code> 中的运算符 <code>+</code> 被看作二元运算符。</p>
</li>
<li><p>如果运算符只有左侧空白,将被看作前缀一元运算符。例如 <code>a ++b</code> 中的 <code>++</code> 被看作前缀一元运算符。</p>
</li>
<li><p>如果运算符只有右侧空白,将被看作后缀一元运算符。例如 <code>a++ b</code> 中的 <code>++</code> 被看作后缀一元运算符。</p>
</li>
<li><p>如果运算符左侧没有空白并紧跟 <code>.</code>,将被看作后缀一元运算符。例如 <code>a++.b</code> 中的 <code>++</code> 被看作后缀一元运算符(同理, <code>a++ . b</code> 中的 <code>++</code> 是后缀一元运算符而 <code>a ++ .b</code> 中的 <code>++</code> 不是).</p>
</li>
</ul>
<p>鉴于这些规则,运算符前的字符 <code>(</code><code>[</code><code>{</code> ;运算符后的字符 <code>)</code><code>]</code><code>}</code> 以及字符 <code>,</code><code>;</code><code>:</code> 都将用于空白检测。</p>
<p>以上规则需注意一点,如果运算符 <code>!</code><code>?</code> 左侧没有空白,则不管右侧是否有空白都将被看作后缀运算符。如果将 <code>?</code> 用作可选类型(<em>optional type</em>)修饰,左侧必须无空白。如果用于条件运算符 <code>? :</code>,必须两侧都有空白。 </p>
<p>在特定构成中 ,以 <code>&lt;</code><code>&gt;</code> 开头的运算符会被分离成两个或多个标记,剩余部分以同样的方式会被再次分离。因此,在 <code>Dictionary&lt;String, Array&lt;Int&gt;&gt;</code> 中没有必要添加空白来消除闭合字符 <code>&gt;</code> 的歧义。在这个例子中, 闭合字符 <code>&gt;</code> 被看作单字符标记,而不会被误解为移位运算符 <code>&gt;&gt;</code></p>
<p>要学习如何自定义新的运算符,请参考 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html#//apple_ref/doc/uid/TP40014097-CH27-XID_48" target="_blank">自定义操作符</a><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/doc/uid/TP40014097-CH34-XID_644" target="_blank">运算符声明</a>。学习如何重写现有运算符,请参考 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html#//apple_ref/doc/uid/TP40014097-CH27-XID_43" target="_blank">运算符方法</a></p>
<blockquote>
<p>运算符语法</p>
<p><em>operator</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/operator-character" target="_blank">operator-character­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/operator" target="_blank">operator­</a> <em>opt­</em></p>
<p><em>operator-character</em><strong>/­</strong> | <strong>=­</strong> | <strong>-­</strong> | <strong>+­</strong> | <strong>!­</strong> | <strong>*­</strong> | <strong>%­</strong> | <strong>&lt;­</strong> | <strong>&gt;­</strong> | <strong>&amp;­</strong> | <strong>|­</strong> | <strong>^­</strong> | <strong>~­</strong> | <strong>.­</strong></p>
<p><em>binary-operator</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/operator" target="_blank">operator­</a></p>
<p><em>prefix-operator</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/operator" target="_blank">operator­</a></p>
<p><em>postfix-operator</em><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/operator" target="_blank">operator­</a></p>
</blockquote>
</section>
</div>
</div>
</div>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3.3" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="3.3" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3.4" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="3.4" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3.6" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="3.6" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3.7" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="3.7" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3.8" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="3.8" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3.9" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="3.9" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -44,7 +44,7 @@
<div class="book" data-level="3.10" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="3.10" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3.5" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="3.5" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3" data-basepath=".." data-revision="1402305571512">
<div class="book" data-level="3" data-basepath=".." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -44,7 +44,7 @@
<div class="book" data-level="0" data-basepath="." data-revision="1402305571512">
<div class="book" data-level="0" data-basepath="." data-revision="1402311241483">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

@ -1,5 +1,5 @@
CACHE MANIFEST
# Revision 1402305571513
# Revision 1402311241484
CACHE:
index.html

File diff suppressed because one or more lines are too long