update 02/04

This commit is contained in:
numbbbbb
2014-06-10 15:52:22 +08:00
parent 49c28ff603
commit b5bf26fee7
44 changed files with 1473 additions and 320 deletions

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3.1" data-basepath=".." data-revision="1402377857835">
<div class="book" data-level="3.1" data-basepath=".." data-revision="1402386668059">
<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_46">
<section class="normal" id="section-gitbook_433">
<h1 id="-">关于语言附注</h1>
<p>本书的这一节描述了Swift编程语言的形式语法。这里描述的语法是为了帮助您更详细的了解该语言而不是让您直接实现一个解析器或编译器。</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3.2" data-basepath=".." data-revision="1402377857835">
<div class="book" data-level="3.2" data-basepath=".." data-revision="1402386668059">
<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_51">
<section class="normal" id="section-gitbook_439">
<h1 id="-">语法结构</h1>
<p>本页包含内容:</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3.3" data-basepath=".." data-revision="1402377857835">
<div class="book" data-level="3.3" data-basepath=".." data-revision="1402386668059">
<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_47">
<section class="normal" id="section-gitbook_434">
<h1 id="-types-">类型Types</h1>
<hr>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3.4" data-basepath=".." data-revision="1402377857835">
<div class="book" data-level="3.4" data-basepath=".." data-revision="1402386668059">
<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,562 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_436">
<h1 id="-expressions-">表达式(Expressions)</h1>
<hr>
<p>Swift 中存在四种表达式: 前缀(prefix)表达式,二元(binary)表达式,主要(primary)表达式和后缀(postfix)表达式。表达式可以返回一个值,以及运行某些逻辑(causes a side effect)。</p>
<p>前缀表达式和二元表达式就是对某些表达式使用各种运算符(operators)。 主要表达式是最短小的表达式,它提供了获取(变量的)值的一种途径。 后缀表达式则允许你建立复杂的表达式,例如配合函数调用和成员访问。 每种表达式都在下面有详细论述~</p>
<blockquote>
<p>表达式的语法</p>
<p><em>expression</em><em>prefix-expression</em>­<em>binary-expressions</em>(opt)
<em>expression-list</em><em>expression</em>­| <em>expression</em>­,­<em>expression-list</em></p>
</blockquote>
<h2 id="-prefix-expressions-">前缀表达式(Prefix Expressions)</h2>
<p>前缀表达式由 前缀符号和表达式组成。(这个前缀符号只能接收一个参数)</p>
<p>Swift 标准库支持如下的前缀操作符:</p>
<ul>
<li>++ 自增1 (increment)</li>
<li>-- 自减1 (decrement)</li>
<li>! 逻辑否 (Logical NOT )</li>
<li>~ 按位否 (Bitwise NOT )</li>
<li>+ 加(Unary plus)</li>
<li>- 减(Unary minus)</li>
</ul>
<p>对于这些操作符的使用,请参见: Basic Operators and Advanced Operators</p>
<p>作为对上面标准库运算符的补充,你也可以对 某个函数的参数使用 &#39;&amp;&#39;运算符。 更多信息,请参见: &quot;In-Out parameters&quot;.</p>
<blockquote>
<p>前缀表达式的语法</p>
<p><em>prefix-expression</em><em>prefix-operator</em> (opt) <em>postfix-expression</em>
<em>prefix-expression</em><em>in-out-expression</em>­
<em>in-out-expression</em>&amp;­<em>identifier</em>­</p>
</blockquote>
<h2 id="-binary-expressions-">二元表达式( Binary Expressions)</h2>
<p>二元表达式由 &quot;左边参数&quot; + &quot;二元运算符&quot; + &quot;右边参数&quot; 组成, 它有如下的形式:</p>
<p> <code>left-hand argument</code> <code>operator</code> <code>right-hand argument</code></p>
<p>Swift 标准库提供了如下的二元运算符:</p>
<ul>
<li>求幂相关无结合优先级160<ul>
<li>&lt;&lt; 按位左移(Bitwise left shift)</li>
<li><blockquote>
<blockquote>
<p>按位右移(Bitwise right shift)</p>
</blockquote>
</blockquote>
</li>
</ul>
</li>
<li>乘除法相关左结合优先级150<ul>
<li>* 乘</li>
<li>/ 除</li>
<li>% 求余</li>
<li>&amp;* 乘法,忽略溢出( Multiply, ignoring overflow)</li>
<li>&amp;/ 除法,忽略溢出(Divide, ignoring overflow)</li>
<li>&amp;% 求余, 忽略溢出( Remainder, ignoring overflow)</li>
<li>&amp; 位与( Bitwise AND)</li>
</ul>
</li>
<li>加减法相关(左结合, 优先级140)<ul>
<li>+ 加</li>
<li>- 减</li>
<li>&amp;+ Add with overflow</li>
<li>&amp;- Subtract with overflow</li>
<li>| 按位或(Bitwise OR )</li>
<li>^ 按位异或(Bitwise XOR)</li>
</ul>
</li>
<li>Range (无结合,优先级 135)<ul>
<li>.. 半闭值域 Half-closed range</li>
<li>... 全闭值域 Closed range</li>
</ul>
</li>
<li>类型转换 (无结合,优先级 132)<ul>
<li>is 类型检查( type check)</li>
<li>as 类型转换( type cast)</li>
</ul>
</li>
<li>Comparative (无结合,优先级 130)<ul>
<li>&lt; 小于</li>
<li>&lt;= 小于等于</li>
<li><blockquote>
<p>大于</p>
</blockquote>
</li>
<li><blockquote>
<p>= 大于等于</p>
</blockquote>
</li>
<li>== 等于</li>
<li>!= 不等</li>
<li>=== 恒等于</li>
<li>!== 不恒等</li>
<li>~= 模式匹配( Pattern match)</li>
</ul>
</li>
<li>合取( Conjunctive) (左结合,优先级 120)<ul>
<li>&amp;&amp; 逻辑与(Logical AND)</li>
</ul>
</li>
<li>析取(Disjunctive) (左结合,优先级 110)<ul>
<li>|| 逻辑或( Logical OR)</li>
</ul>
</li>
<li>三元条件(Ternary Conditional )(右结合,优先级 100)<ul>
<li>?: 三元条件 Ternary conditional</li>
</ul>
</li>
<li>赋值 (Assignment) (右结合, 优先级 90)<ul>
<li>= 赋值(Assign)</li>
<li>*= Multiply and assign</li>
<li>/= Divide and assign</li>
<li>%= Remainder and assign</li>
<li>+= Add and assign</li>
<li>-= Subtract and assign</li>
<li>&lt;&lt;= Left bit shift and assign</li>
<li><blockquote>
<blockquote>
<p>= Right bit shift and assign</p>
</blockquote>
</blockquote>
</li>
<li>&amp;= Bitwise AND and assign</li>
<li>^= Bitwise XOR and assign</li>
<li>|= Bitwise OR and assign</li>
<li>&amp;&amp;= Logical AND and assign</li>
<li>||= Logical OR and assign</li>
</ul>
</li>
</ul>
<p>关于这些运算符(operators)的更多信息请参见Basic Operators and Advanced Operators.</p>
<blockquote>
<blockquote>
<p>注意</p>
<p>在解析时, 一个二元表达式表示为一个一级数组(a flat list), 这个数组(List)根据运算符的先后顺序被转换成了一个tree. 例如: 2 + 3 <em> 5 首先被认为是: 2, + , <code>3</code>, </em>, 5. 随后它被转换成 tree (2 + (3 * 5))</p>
</blockquote>
<p>二元表达式的语法</p>
<p><em>binary-expression</em><em>binary-operator</em>­<em>prefix-expression</em>­
<em>binary-expression</em><em>assignment-operator</em>­prefix-expression<em>
</em>binary-expression<em></em>conditional-operator<em>­prefix-expression</em>
<em>binary-expression</em><em>type-casting-operator</em>­
<em>binary-expression</em>s → <em>binary-expression</em>­<em>binary-expressions</em>(opt­)</p>
</blockquote>
<h2 id="-assignment-operator-">赋值表达式( Assignment Operator)</h2>
<p>The assigment operator sets a new value for a given expression. It has the following form:
赋值表达式会对某个给定的表达式赋值。 它有如下的形式;</p>
<p><code>expression</code> = <code>value</code></p>
<p>就是把右边的 <em>value</em> 赋值给左边的 <em>expression</em>. 如果左边的<em>expression</em> 需要接收多个参数是一个tuple )那么右边必须也是一个具有同样数量参数的tuple. (允许嵌套的tuple)</p>
<pre><code class="lang-swift">(a, _, (b, c)) = (&quot;test&quot;, 9.45, (12, 3))
// a is &quot;test&quot;, b is 12, c is 3, and 9.45 is ignored
</code></pre>
<p>赋值运算符不返回任何值。</p>
<blockquote>
<p>赋值表达式的语法</p>
<p><em>assignment-operator</em> → =­</p>
</blockquote>
<h2 id="-ternary-conditional-operator-">三元条件运算符(Ternary Conditional Operator)</h2>
<p>三元条件运算符 是根据条件来获取值。 形式如下:</p>
<pre><code>`condition` ? `expression used if true` : `expression used if false`
</code></pre><p>如果 <code>condition</code> 是true, 那么返回 第一个表达式的值(此时不会调用第二个表达式), 否则返回第二个表达式的值(此时不会调用第一个表达式)。</p>
<p>想看三元条件运算符的例子,请参见: Ternary Conditional Operator.</p>
<blockquote>
<p>三元条件表达式</p>
<p><code>conditional-operator</code> → ?­<code>expression</code>­:­</p>
</blockquote>
<h2 id="-type-casting-operators-">类型转换运算符(Type-Casting Operators)</h2>
<p>有两种类型转换操作符: as 和 is. 它们有如下的形式:</p>
<pre><code>`expression` as `type`
`expression` as? `type`
`expression` is `type`
</code></pre><p>as 运算符会把<code>目标表达式</code>转换成指定的<code>类型</code>(specified type),过程如下:</p>
<ul>
<li><p>如果类型转换成功, 那么目标表达式就会返回指定类型的实例(instance). 例如:把子类(subclass)变成父类(superclass)时.</p>
</li>
<li><p>如果转换失败,则会抛出编译错误( compile-time error)。</p>
</li>
<li><p>如果上述两个情况都不是(也就是说,编译器在编译时期无法确定转换能否成功,) 那么目标表达式就会变成指定的类型的optional. (is an optional of the specified type ) 然后在运行时,如果转换成功, 目标表达式就会作为 optional的一部分来返回 否则目标表达式返回nil. 对应的例子是: 把一个 superclass 转换成一个 subclass.</p>
</li>
</ul>
<pre><code class="lang-swift">class SomeSuperType {}
class SomeType: SomeSuperType {}
class SomeChildType: SomeType {}
let s = SomeType()
let x = s as SomeSuperType // known to succeed; type is SomeSuperType
let y = s as Int // known to fail; compile-time error
let z = s as SomeChildType // might fail at runtime; type is SomeChildType?
</code></pre>
<p>使用&#39;as&#39;做类型转换跟正常的类型声明,对于编译器来说是一样的。例如:</p>
<pre><code class="lang-swift">let y1 = x as SomeType // Type information from &#39;as&#39;
let y2: SomeType = x // Type information from an annotation
</code></pre>
<p>&#39;is&#39; 运算符在“运行时(runtime)”会做检查。 成功会返回true, 否则 false</p>
<p>The check must not be known to be true or false at compile time. The following are invalid:
上述检查在“编译时(compile time)”不能使用。 例如下面的使用是错误的:</p>
<pre><code class="lang-swift">&quot;hello&quot; is String
&quot;hello&quot; is Int
</code></pre>
<p>关于类型转换的更多内容和例子,请参见: Type Casting.</p>
<blockquote>
<p>类型转换的语法</p>
<p><em>type-casting-operator</em> → is­<em>type</em>­| as­?(opt)­<em>type</em></p>
</blockquote>
<h2 id="-primary-expressions-">主要表达式(Primary Expressions)</h2>
<p><code>主要表达式</code>是最基本的表达式。 它们可以跟 前缀表达式,二元表达式,后缀表达式以及其他主要表达式组合使用。</p>
<blockquote>
<p>主要表达式的语法</p>
<p><em>primary-expression</em><em>identifier</em>­<em>generic-argument-clause</em>­(opt)
<em>primary-expression</em><em>literal-expression</em>­
<em>primary-expression</em><em>self-expression</em>­
<em>primary-expression</em><em>superclass-expression</em>­
<em>primary-expression</em><em>closure-expression</em>­
<em>primary-expression</em><em>parenthesized-expression</em>­
<em>primary-expression</em><em>implicit-member-expression</em>
<em>primary-expression</em><em>wildcard-expression</em></p>
</blockquote>
<h3 id="-literal-expression-">字符型表达式(Literal Expression)</h3>
<p>由这些内容组成普通的字符string, number) , 一个字符的字典或者数组,或者下面列表中的特殊字符。</p>
<table>
<thead>
<tr>
<th>字符(Literal)</th>
<th>类型(Type)</th>
<th>值(Value)</th>
</tr>
</thead>
<tbody>
<tr>
<td>_<em>FILE_</em></td>
<td>String</td>
<td>所在的文件名</td>
</tr>
<tr>
<td>_<em>LINE_</em></td>
<td>Int</td>
<td>所在的行数</td>
</tr>
<tr>
<td>_<em>COLUMN_</em></td>
<td>Int</td>
<td>所在的列数</td>
</tr>
<tr>
<td>_<em>FUNCTION_</em></td>
<td>String</td>
<td>所在的function 的名字</td>
</tr>
</tbody>
</table>
<p>在某个函数(function)中,<code>__FUNCTION__</code> 会返回当前函数的名字。 在某个方法(method)中,它会返回当前方法的名字。 在某个property 的getter/setter中会返回这个属性的名字。 在init/subscript中 只有的特殊成员(member)中会返回这个keyword的名字在某个文件的顶端(the top level of a file)它返回的是当前module的名字。</p>
<p>一个array literal是一个有序的值的集合。 它的形式是:</p>
<pre><code>[`value 1`, `value 2`, `...`]
</code></pre><p>数组中的最后一个表达式可以紧跟一个逗号(&#39;,&#39;). []表示空数组 。 array literal的type是 T[], 这个T就是数组中元素的type. 如果该数组中有多种type, T则是跟这些type的公共supertype最接近的type.(closest common supertype)</p>
<p>一个<code>dictionary literal</code> 是一个包含无序的键值对(key-value pairs)的集合,它的形式是:</p>
<pre><code>[`key 1`: `value 1`, `key 2`: `value 2`, `...`]
</code></pre><p>dictionary 的最后一个表达式可以是一个逗号(&#39;,&#39;). [:] 表示一个空的dictionary. 它的type是 Dictionary<KeyType, ValueType> (这里KeyType表示 key的type, ValueType表示 value的type) 如果这个dictionary 中包含多种 types, 那么KeyType, Value 则对应着它们的公共supertype最接近的type( closest common supertype).</p>
<blockquote>
<p>字符型表达式的语法</p>
<p><em>literal-expression</em><em>literal</em>
<em>literal-expression</em><em>array-literal</em>­| <em>dictionary-literal</em>­
<em>literal-expression</em><em>_<em>FILE_</em></em>­| <em>_<em>LINE_</em></em>­| <em>_<em>COLUMN_</em></em>­| <em>_<em>FUNCTION_</em></em>­
<em>array-literal</em> → [­<em>array-literal-items</em>­opt­]­
<em>array-literal-items</em><em>array-literal-item</em>­,­(opt) | ­<em>array-literal-item</em>­,­<em>array-literal-items</em>­
<em>array-literal-item</em><em>expression</em>­
<em>dictionary-literal</em> → [­<em>dictionary-literal-items</em>­]­ [­:­]­
<em>dictionary-literal-items</em><em>dictionary-literal-item</em>,­(opt)­| <em>dictionary-literal-item</em>­,­<em>dictionary-literal-items</em>­
<em>dictionary-literal-item</em><em>expression</em>­:­<em>expression</em>­</p>
</blockquote>
<h3 id="self-self-expression-">self表达式(Self Expression)</h3>
<p>self表达式是对 当前type 或者当前instance的引用。它的形式如下</p>
<blockquote>
<p>self
self.<code>member name</code>
self[<code>subscript index</code>]
self(<code>initializer arguments</code>)
self.init(<code>initializer arguments</code>)</p>
</blockquote>
<p>如果在 initializer, subscript, instance method中self等同于当前type的instance. 在一个静态方法(static method), 类方法(class method)中, self等同于当前的type.</p>
<p>当访问 member成员变量时 self 用来区分重名变量(例如函数的参数). 例如,
(下面的 self.greeting 指的是 var greeting: String, 而不是 init(greeting: String) )</p>
<pre><code class="lang-swift">class SomeClass {
var greeting: String
init(greeting: String) {
self.greeting = greeting
}
}
</code></pre>
<p>在mutating 方法中, 你可以使用self 对 该instance进行赋值。</p>
<pre><code class="lang-swift">struct Point {
var x = 0.0, y = 0.0
mutating func moveByX(deltaX: Double, y deltaY: Double) {
self = Point(x: x + deltaX, y: y + deltaY)
}
}
</code></pre>
<blockquote>
<p>self表达式的语法</p>
<p><em>self-expression</em> → self­
<em>self-expression</em> → self­.­<em>identifier</em>­
<em>self-expression</em> → self­[­<em>expression</em>­]­
<em>self-expression</em> → self­.­init­</p>
</blockquote>
<h3 id="-superclass-expression-">超类表达式(Superclass Expression)</h3>
<p>超类表达式可以使我们在某个class中访问它的超类. 它有如下形式:</p>
<pre><code>super.`member name`
super[`subscript index`]
super.init(`initializer arguments`)
</code></pre><p>形式1 用来访问超类的某个成员(member). 形式2 用来访问该超类的 subscript 实现。 形式3 用来访问该超类的 initializer.</p>
<p>子类(subclass)可以通过超类(superclass)表达式在它们的 member, subscripting 和 initializers 中来利用它们超类中的某些实现(既有的方法或者逻辑)。</p>
<blockquote>
<p>GRAMMAR OF A SUPERCLASS EXPRESSION</p>
<p><em>superclass-expression</em><em>superclass-method-expression</em> | <em>superclass-subscript-expression</em>­| <em>superclass-initializer-expression</em>
<em>superclass-method-expression</em> → super­.­<em>identifier</em>
<em>superclass-subscript-expression</em> → super­[­<em>expression</em>­]­
<em>superclass-initializer-expression</em> → super­.­init­</p>
</blockquote>
<h3 id="-closure-expression-">闭包表达式(Closure Expression)</h3>
<p>闭包(closure) 表达式可以建立一个闭包(在其他语言中也叫 lambda, 或者 匿名函数(anonymous function)). 跟函数(function)的声明一样, 闭包(closure)包含了可执行的代码(跟方法主体(statement)类似) 以及接收(capture)的参数。 它的形式如下:</p>
<pre><code class="lang-swift"> { (parameters) -&gt; return type in
statements
}
</code></pre>
<p>闭包的参数声明形式跟方法中的声明一样, 请参见Function Declaration.</p>
<p>闭包还有几种特殊的形式, 让使用更加简洁:</p>
<ul>
<li>闭包可以省略 它的参数的type 和返回值的type. 如果省略了参数和参数类型,就也要省略 &#39;in&#39;关键字。 如果被省略的type 无法被编译器获知(inferred) ,那么就会抛出编译错误。</li>
<li>闭包可以省略参数,转而在方法体(statement)中使用 $0, $1, $2 来引用出现的第一个,第二个,第三个参数。</li>
<li>如果闭包中只包含了一个表达式,那么该表达式就会自动成为该闭包的返回值。 在执行 &#39;type inference &#39;时,该表达式也会返回。</li>
</ul>
<p>下面几个 闭包表达式是 等价的:</p>
<pre><code class="lang-swift">myFunction {
(x: Int, y: Int) -&gt; Int in
return x + y
}
myFunction {
(x, y) in
return x + y
}
myFunction { return $0 + $1 }
myFunction { $0 + $1 }
</code></pre>
<p>关于 向闭包中传递参数的内容,参见: Function Call Expression.</p>
<p>闭包表达式可以通过一个参数列表(capture list) 来显式指定它需要的参数。 参数列表 由中括号 [] 括起来,里面的参数由逗号&#39;,&#39;分隔。一旦使用了参数列表,就必须使用&#39;in&#39;关键字(在任何情况下都得这样做包括忽略参数的名字type, 返回值时等等)。</p>
<p>在闭包的参数列表( capture list)中, 参数可以声明为 &#39;weak&#39; 或者 &#39;unowned&#39; .</p>
<pre><code class="lang-swift">myFunction { print(self.title) } // strong capture
myFunction { [weak self] in print(self!.title) } // weak capture
myFunction { [unowned self] in print(self.title) } // unowned capture
</code></pre>
<p>在参数列表中,也可以使用任意表达式来赋值. 该表达式会在 闭包被执行时赋值,然后按照不同的力度来获取(这句话请慎重理解)。(captured with the specified strength. ) 例如:</p>
<pre><code class="lang-swift">// Weak capture of &quot;self.parent&quot; as &quot;parent&quot;
myFunction { [weak parent = self.parent] in print(parent!.title) }
</code></pre>
<p>关于闭包表达式的更多信息和例子,请参见: Closure Expressions.</p>
<blockquote>
<p>闭包表达式的语法</p>
<p><em>closure-expression</em> → {­<em>closure-signature</em>­opt­<em>statements</em>­}­
<em>closure-signature</em><em>parameter-clause</em>­<em>function-result</em>­(opt)­in­
<em>closure-signature</em><em>identifier-list</em>­<em>function-result</em>­(opt)­in­
<em>closure-signature</em><em>capture-list</em>­<em>parameter-clause</em>­<em>function-result</em>­(opt)­in­
<em>closure-signature</em><em>capture-list</em>­<em>identifier-list</em>­<em>function-result</em>­(opt)­in­
<em>closure-signature</em><em>capture-list</em>­in­
<em>capture-list</em> → [­<em>capture-specifier</em>­<em>expression</em>­]­
<em>capture-specifier</em> → weak­| unowned­| unowned(safe)­| unowned(unsafe)­</p>
</blockquote>
<h3 id="-implicit-member-expression-">隐式成员表达式(Implicit Member Expression)</h3>
<p>在可以判断出类型(type)的上下文(context)中隐式成员表达式是访问某个type的member( 例如 class method, enumeration case) 的简洁方法。 它的形式是:</p>
<p>.<code>member name</code></p>
<p>例子:</p>
<pre><code class="lang-swift">var x = MyEnumeration.SomeValue
x = .AnotherValue
</code></pre>
<blockquote>
<p> 隐式成员表达式的语法</p>
<p> <em>implicit-member-expression</em> → .­<em>identifier</em></p>
</blockquote>
<h3 id="-parenthesized-expression-">圆括号表达式(Parenthesized Expression)</h3>
<p>圆括号表达式由多个子表达式和逗号&#39;,&#39;组成。 每个子表达式前面可以有 identifier x: 这样的可选前缀。形式如下:</p>
<p>(<code>identifier 1</code>: <code>expression 1</code>, <code>identifier 2</code>: <code>expression 2</code>, <code>...</code>)</p>
<p>圆括号表达式用来建立tuples 然后把它做为参数传递给 function. 如果某个圆括号表达式中只有一个 子表达式那么它的type就是 子表达式的type。例如 (1)的 type是Int, 而不是(Int)</p>
<blockquote>
<p>圆括号表达式的语法</p>
<p><em>parenthesized-expression</em> → (­<em>expression-element-list</em> (opt)­)­
<em>expression-element-list</em><em>expression-element</em>­| <em>expression-element</em>­,­<em>expression-element-list</em>­
<em>expression-element</em><em>expression</em>­| <em>identifier</em>­:­<em>expression</em></p>
</blockquote>
<h3 id="-wildcard-expression-">通配符表达式( Wildcard Expression)</h3>
<p>通配符表达式用来忽略传递进来的某个参数。例如下面的代码中10被传递给x, 20被忽略译注好奇葩的语法。。。</p>
<pre><code class="lang-swift">(x, _) = (10, 20)
// x is 10, 20 is ignored
</code></pre>
<blockquote>
<p>通配符表达式的语法</p>
<p><em>wildcard-expression</em> → _­</p>
</blockquote>
<h2 id="-postfix-expressions-">后缀表达式( Postfix Expressions)</h2>
<p>后缀表达式就是在某个表达式的后面加上 操作符。 严格的讲,每个主要表达式(primary expression)都是一个后缀表达式</p>
<p>Swift 标准库提供了下列后缀表达式:</p>
<ul>
<li>++ Increment</li>
<li>-- Decrement</li>
</ul>
<p>对于这些操作符的使用,请参见: Basic Operators and Advanced Operators</p>
<blockquote>
<p>后缀表达式的语法</p>
<p><em>postfix-expression</em><em>primary-expression</em>
<em>postfix-expression</em><em>postfix-expression</em>­<em>postfix-operator</em>
<em>postfix-expression</em><em>function-call-expression</em>­
<em>postfix-expression</em><em>initializer-expression</em>­
<em>postfix-expression</em><em>explicit-member-expression</em>­
<em>postfix-expression</em><em>postfix-self-expression</em>­
<em>postfix-expression</em><em>dynamic-type-expression</em>­
<em>postfix-expression</em><em>subscript-expression</em>­
<em>postfix-expression</em><em>forced-value-expression</em>­
<em>postfix-expression</em><em>optional-chaining-expression</em>­</p>
</blockquote>
<h3 id="-function-call-expression-">函数调用表达式( Function Call Expression)</h3>
<p>函数调用表达式由函数名和参数列表组成。它的形式如下:</p>
<p><code>function name</code>(<code>argument value 1</code>, <code>argument value 2</code>)</p>
<p>The function name can be any expression whose value is of a function type.
(不用翻译了, 太罗嗦)</p>
<p>如果该function 的声明中指定了参数的名字,那么在调用的时候也必须得写出来. 例如:</p>
<p><code>function name</code>(<code>argument name 1</code>: <code>argument value 1</code>, <code>argument name 2</code>: <code>argument value 2</code>)</p>
<p>可以在 函数调用表达式的尾部(最后一个参数之后)加上 一个闭包(closure) 该闭包会被目标函数理解并执行。它具有如下两种写法:</p>
<pre><code class="lang-swift">// someFunction takes an integer and a closure as its arguments
someFunction(x, {$0 == 13})
someFunction(x) {$0 == 13}
</code></pre>
<p>如果闭包是该函数的唯一参数,那么圆括号可以省略。</p>
<pre><code class="lang-swift">// someFunction takes a closure as its only argument
myData.someMethod() {$0 == 13}
myData.someMethod {$0 == 13}
</code></pre>
<blockquote>
<p>GRAMMAR OF A FUNCTION CALL EXPRESSION</p>
<p><em>function-call-expression</em><em>postfix-expression</em>­<em>parenthesized-expression</em>
<em>function-call-expression</em><em>postfix-expression</em>­<em>parenthesized-expression</em>­(opt)­<em>trailing-closure</em>­
<em>trailing-closure</em><em>closure-expression</em>­</p>
</blockquote>
<h3 id="-initializer-expression-">初始化函数表达式(Initializer Expression)</h3>
<p>Initializer表达式用来给某个Type初始化。 它的形式如下:</p>
<p><code>expression</code>.init(<code>initializer arguments</code>)</p>
<p>(Initializer表达式用来给某个Type初始化。) 跟函数(function)不同, initializer 不能返回值。</p>
<pre><code class="lang-swift">var x = SomeClass.someClassFunction // ok
var y = SomeClass.init // error
```swift
可以通过 initializer 表达式来委托调用(delegate to )到superclass的initializers.
```swift
class SomeSubClass: SomeSuperClass {
init() {
// subclass initialization goes here
super.init()
}
}
</code></pre>
<blockquote>
<p>initializer表达式的语法</p>
<p><em>initializer-expression</em><em>postfix-expression</em>­.­init­</p>
</blockquote>
<h3 id="-explicit-member-expression-">显式成员表达式(Explicit Member Expression)</h3>
<p>显示成员表达式允许我们访问type, tuple, module的成员变量。它的形式如下</p>
<p><code>expression</code>.<code>member name</code></p>
<p>该member 就是某个type在声明时候所定义(declaration or extension) 的变量, 例如:</p>
<pre><code class="lang-swift">class SomeClass {
var someProperty = 42
}
let c = SomeClass()
let y = c.someProperty // Member access
</code></pre>
<p>对于tuple, 要根据它们出现的顺序(0, 1, 2...)来使用:</p>
<pre><code class="lang-swift">var t = (10, 20, 30)
t.0 = t.1
// Now t is (20, 20, 30)
</code></pre>
<p>The members of a module access the top-level declarations of that module.
(不确定对于某个module的member的调用只能调用在top-level声明中的member.)</p>
<blockquote>
<p>显示成员表达式的语法</p>
<p><em>explicit-member-expression</em><em>postfix-expression</em>­.­<em>decimal-digit</em>­
<em>explicit-member-expression</em><em>postfix-expression</em>­.­<em>identifier</em>­<em>generic-argument-clause</em>(opt)</p>
</blockquote>
<h3 id="-self-postfix-self-expression-">后缀self表达式(Postfix Self Expression)</h3>
<p>后缀表达式由 某个表达式 + &#39;.self&#39; 组成. 形式如下:</p>
<p><code>expression</code>.self
<code>type</code>.self</p>
<p>形式1 表示会返回 expression 的值。例如: x.self 返回 x</p>
<p>形式2返回对应的type。我们可以用它来动态的获取某个instance的type。</p>
<blockquote>
<p>后缀self表达式的语法</p>
<p><em>postfix-self-expression</em><em>postfix-expression</em>­.­self­</p>
</blockquote>
<h3 id="dynamic-dynamic-type-expression-">dynamic表达式(Dynamic Type Expression)</h3>
<p>(因为dynamicType是一个独有的方法所以这里保留了英文单词未作翻译, --- 类似与self expression)</p>
<p>dynamicType 表达式由 某个表达式 + &#39;.dynamicType&#39; 组成。</p>
<p><code>expression</code>.dynamicType</p>
<p>上面的形式中, expression 不能是某type的名字(当然了,如果我都知道它的名字了还需要动态来获取它吗)。动态类型表达式会返回&quot;运行时&quot;某个instance的type, 具体请看下面的列子:</p>
<pre><code class="lang-swift">class SomeBaseClass {
class func printClassName() {
println(&quot;SomeBaseClass&quot;)
}
}
class SomeSubClass: SomeBaseClass {
override class func printClassName() {
println(&quot;SomeSubClass&quot;)
}
}
let someInstance: SomeBaseClass = SomeSubClass()
// someInstance is of type SomeBaseClass at compile time, but
// someInstance is of type SomeSubClass at runtime
someInstance.dynamicType.printClassName()
// prints &quot;SomeSubClass&quot;
</code></pre>
<blockquote>
<p>dynamic type 表达式</p>
<p><em>dynamic-type-expression</em><em>postfix-expression</em>­.­dynamicType­</p>
</blockquote>
<h3 id="-subscript-expression-">下标表达式( Subscript Expression)</h3>
<p>下标表达式提供了通过下标访问getter/setter 的方法。它的形式是:</p>
<p><code>expression</code>[<code>index expressions</code>]</p>
<p>可以通过下标表达式通过getter获取某个值或者通过setter赋予某个值.</p>
<p>关于subscript的声明请参见 Protocol Subscript Declaration.</p>
<blockquote>
<p>下标表达式的语法</p>
<p><em>subscript-expression</em><em>postfix-expression</em>­[­<em>expression-list</em>­]­</p>
</blockquote>
<h3 id="-forced-value-expression-">强制取值表达式(Forced-Value Expression)</h3>
<p>强制取值表达式用来获取某个目标表达式的值(该目标表达式的值必须不是nil )。它的形式如下:</p>
<p><code>expression</code>!</p>
<p>如果该表达式的值不是nil, 则返回对应的值。 否则,抛出运行时错误(runtime error)。</p>
<blockquote>
<p>强制取值表达式的语法</p>
<p><em>forced-value-expression</em><em>postfix-expression</em>­!­</p>
</blockquote>
<h3 id="-optional-chaining-expression-">可选链表达式(Optional-Chaining Expression)</h3>
<p>可选链表达式由目标表达式 + &#39;?&#39; 组成,形式如下:</p>
<p><code>expression</code>?</p>
<p>后缀&#39;?&#39; 返回目标表达式的值,把它做为可选的参数传递给后续的表达式</p>
<p>如果某个后缀表达式包含了可选链表达式,那么它的执行过程就比较特殊: 首先先判断该可选链表达式的值,如果是 nil, 整个后缀表达式都返回 nil, 如果该可选链的值不是nil, 则正常返回该后缀表达式的值(依次执行它的各个子表达式。在这两种情况下该后缀表达式仍然是一个optional type(In either case, the value of the postfix expression is still of an optional type)</p>
<p>如果某个&quot;后缀表达式&quot;&quot;子表达式&quot;中包含了&quot;可选链表达式&quot;那么只有最外层的表达式返回的才是一个optional type. 例如,在下面的例子中, 如果c 不是nil, 那么 c?.property.performAction() 这句代码在执行时就会先获得c 的property方法然后调用 performAction()方法。 然后对于 &quot;c?.property.performAction()&quot; 这个整体它的返回值是一个optional type.</p>
<pre><code class="lang-swift">var c: SomeClass?
var result: Bool? = c?.property.performAction()
</code></pre>
<p>如果不使用可选链表达式,那么 上面例子的代码跟下面例子等价:</p>
<pre><code class="lang-swift">if let unwrappedC = c {
result = unwrappedC.property.performAction()
}
</code></pre>
<blockquote>
<p>可选链表达式的语法</p>
<p><em>optional-chaining-expression</em><em>postfix-expression</em>­?­</p>
</blockquote>
</section>
</div>
</div>
</div>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3.6" data-basepath=".." data-revision="1402377857835">
<div class="book" data-level="3.6" data-basepath=".." data-revision="1402386668059">
<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="1402377857835">
<div class="book" data-level="3.7" data-basepath=".." data-revision="1402386668059">
<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_45">
<section class="normal" id="section-gitbook_432">
<h1 id="-">特性</h1>
<p>特性提供了关于声明和类型的更多信息。在Swift中有两类特性用于修饰声明的以及用于修饰类型的。例如<code>required</code>特性,当应用于一个类的指定或便利初始化器声明时,表明它的每个子类都必须实现那个初始化器。再比如<code>noreturn</code>特性,当应用于函数或方法类型时,表明该函数或方法不会返回到它的调用者。</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3.8" data-basepath=".." data-revision="1402377857835">
<div class="book" data-level="3.8" data-basepath=".." data-revision="1402386668059">
<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_52">
<section class="normal" id="section-gitbook_440">
<h1 id="-patterns-">模式Patterns</h1>
<p>模式pattern代表了单个值或者复合值的结构。例如元组<code>(1, 2)</code>的结构是逗号分隔的,包含两个元素的列表。因为模式代表一种值的结构,而不是特定的某个值,你可以把模式和各种同类型的值匹配起来。比如,<code>(x, y)</code>可以匹配元组<code>(1, 2)</code>,以及任何含两个元素的元组。除了将模式与一个值匹配外,你可以从合成值中提取出部分或全部,然后分别把各个部分和一个常量或变量绑定起来。</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3.9" data-basepath=".." data-revision="1402377857835">
<div class="book" data-level="3.9" data-basepath=".." data-revision="1402386668059">
<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_53">
<section class="normal" id="section-gitbook_441">
<h1 id="-">泛型参数</h1>
<hr>

View File

@ -44,7 +44,7 @@
<div class="book" data-level="3.10" data-basepath=".." data-revision="1402377857835">
<div class="book" data-level="3.10" data-basepath=".." data-revision="1402386668059">
<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_55">
<section class="normal" id="section-gitbook_443">
<h1 id="-">语法总结</h1>
<p>本页包含内容:</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3.5" data-basepath=".." data-revision="1402377857835">
<div class="book" data-level="3.5" data-basepath=".." data-revision="1402386668059">
<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_56">
<section class="normal" id="section-gitbook_444">
<h1 id="-">语句</h1>
<p>在 Swift 中有两种类型的语句简单语句和控制流语句。简单语句是最常见的用于构造表达式和声明。控制流语句则用于控制程序执行的流程Swift 中有三种类型的控制流语句:循环语句、分支语句和控制传递语句。</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3" data-basepath=".." data-revision="1402377857835">
<div class="book" data-level="3" data-basepath=".." data-revision="1402386668059">
<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>