add [expression] header links

This commit is contained in:
stanzhai
2014-06-13 15:43:19 +08:00
parent d8073ee492
commit 1f5f7d19e0
3 changed files with 715 additions and 14 deletions

View File

@ -0,0 +1,700 @@
<!DOCTYPE html><html><head><meta charset="utf-8"><style>html { font-size: 100%; overflow-y: scroll; -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; }
body{
color:#444;
font-family:Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman',
"Hiragino Sans GB", "STXihei", "微软雅黑", serif;
font-size:12px;
line-height:1.5em;
background:#fefefe;
width: 45em;
margin: 10px auto;
padding: 1em;
outline: 1300px solid #FAFAFA;
}
a{ color: #0645ad; text-decoration:none;}
a:visited{ color: #0b0080; }
a:hover{ color: #06e; }
a:active{ color:#faa700; }
a:focus{ outline: thin dotted; }
a:hover, a:active{ outline: 0; }
span.backtick {
border:1px solid #EAEAEA;
border-radius:3px;
background:#F8F8F8;
padding:0 3px 0 3px;
}
::-moz-selection{background:rgba(255,255,0,0.3);color:#000}
::selection{background:rgba(255,255,0,0.3);color:#000}
a::-moz-selection{background:rgba(255,255,0,0.3);color:#0645ad}
a::selection{background:rgba(255,255,0,0.3);color:#0645ad}
p{
margin:1em 0;
}
img{
max-width:100%;
}
h1,h2,h3,h4,h5,h6{
font-weight:normal;
color:#111;
line-height:1em;
}
h4,h5,h6{ font-weight: bold; }
h1{ font-size:2.5em; }
h2{ font-size:2em; border-bottom:1px solid silver; padding-bottom: 5px; }
h3{ font-size:1.5em; }
h4{ font-size:1.2em; }
h5{ font-size:1em; }
h6{ font-size:0.9em; }
blockquote{
color:#666666;
margin:0;
padding-left: 3em;
border-left: 0.5em #EEE solid;
}
hr { display: block; height: 2px; border: 0; border-top: 1px solid #aaa;border-bottom: 1px solid #eee; margin: 1em 0; padding: 0; }
pre , code, kbd, samp {
color: #000;
font-family: monospace;
font-size: 0.88em;
border-radius:3px;
background-color: #F8F8F8;
border: 1px solid #CCC;
}
pre { white-space: pre; white-space: pre-wrap; word-wrap: break-word; padding: 5px 12px;}
pre code { border: 0px !important; padding: 0;}
code { padding: 0 3px 0 3px; }
b, strong { font-weight: bold; }
dfn { font-style: italic; }
ins { background: #ff9; color: #000; text-decoration: none; }
mark { background: #ff0; color: #000; font-style: italic; font-weight: bold; }
sub, sup { font-size: 75%; line-height: 0; position: relative; vertical-align: baseline; }
sup { top: -0.5em; }
sub { bottom: -0.25em; }
ul, ol { margin: 1em 0; padding: 0 0 0 2em; }
li p:last-child { margin:0 }
dd { margin: 0 0 0 2em; }
img { border: 0; -ms-interpolation-mode: bicubic; vertical-align: middle; }
table { border-collapse: collapse; border-spacing: 0; }
td { vertical-align: top; }
@media only screen and (min-width: 480px) {
body{font-size:14px;}
}
@media only screen and (min-width: 768px) {
body{font-size:16px;}
}
@media print {
* { background: transparent !important; color: black !important; filter:none !important; -ms-filter: none !important; }
body{font-size:12pt; max-width:100%; outline:none;}
a, a:visited { text-decoration: underline; }
hr { height: 1px; border:0; border-bottom:1px solid black; }
a[href]:after { content: " (" attr(href) ")"; }
abbr[title]:after { content: " (" attr(title) ")"; }
.ir a:after, a[href^="javascript:"]:after, a[href^="#"]:after { content: ""; }
pre, blockquote { border: 1px solid #999; padding-right: 1em; page-break-inside: avoid; }
tr, img { page-break-inside: avoid; }
img { max-width: 100% !important; }
@page :left { margin: 15mm 20mm 15mm 10mm; }
@page :right { margin: 15mm 10mm 15mm 20mm; }
p, h2, h3 { orphans: 3; widows: 3; }
h2, h3 { page-break-after: avoid; }
}
</style><title>04_Expressions</title></head><body><blockquote>
<p>翻译sg552</p>
<p>校对numbbbbb</p>
</blockquote>
<h1 id="表达式expressions">表达式Expressions</h1>
<hr />
<p>本页包含内容:</p>
<ul>
<li><a href="#prefix_expressions">前缀表达式Prefix Expressions</a></li>
<li><a href="#binary_exprssions">二元表达式Binary Expressions</a></li>
<li><a href="#assignment_operator">赋值表达式Assignment Operator</a></li>
<li><a href="#ternary_conditional_operator">三元条件运算符Ternary Conditional Operator</a></li>
<li><a href="#type-casting_operators">类型转换运算符Type-Casting Operators</a></li>
<li><a href="#primary_expressions">主要表达式Primary Expressions</a></li>
<li><a href="#postfix_expressions">后缀表达式Postfix Expressions</a></li>
</ul>
<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>
<p><a name="prefix_expressions"></a></p>
<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>作为对上面标准库运算符的补充,你也可以对 某个函数的参数使用 '&amp;'运算符。 更多信息,请参见: "In-Out parameters".</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>
<p><a name="binary_expressions"></a></p>
<h2 id="二元表达式binary-expressions">二元表达式Binary Expressions</h2>
<p>二元表达式由 "左边参数" + "二元运算符" + "右边参数" 组成, 它有如下的形式:</p>
<p><code>left-hand argument</code> <code>operator</code> <code>right-hand argument</code></p>
<p>Swift 标准库提供了如下的二元运算符:</p>
<ul>
<li>求幂相关无结合优先级160</li>
<li>&lt;&lt; 按位左移Bitwise left shift</li>
<li>
<blockquote>
<blockquote>
<p>按位右移Bitwise right shift</p>
</blockquote>
</blockquote>
</li>
<li>乘除法相关左结合优先级150</li>
<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>
<li>加减法相关(左结合, 优先级140</li>
<li>+ 加</li>
<li>- 减</li>
<li>&amp;+ Add with overflow</li>
<li>&amp;- Subtract with overflow</li>
<li>| 按位或Bitwise OR </li>
<li>^ 按位异或Bitwise XOR</li>
<li>Range (无结合,优先级 135</li>
<li>.. 半闭值域 Half-closed range</li>
<li>... 全闭值域 Closed range</li>
<li>类型转换 (无结合,优先级 132</li>
<li>is 类型检查( type check</li>
<li>as 类型转换( type cast</li>
<li>Comparative (无结合,优先级 130</li>
<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>
<li>合取( Conjunctive (左结合,优先级 120</li>
<li>&amp;&amp; 逻辑与Logical AND</li>
<li>析取Disjunctive (左结合,优先级 110</li>
<li>|| 逻辑或( Logical OR</li>
<li>三元条件Ternary Conditional )(右结合,优先级 100</li>
<li>?: 三元条件 Ternary conditional</li>
<li>赋值 Assignment (右结合, 优先级 90</li>
<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>
<p>关于这些运算符operators的更多信息请参见Basic Operators and Advanced Operators.</p>
<blockquote>
<blockquote>
<p>注意</p>
<p>在解析时, 一个二元表达式表示为一个一级数组a flat list, 这个数组List根据运算符的先后顺序被转换成了一个tree. 例如: 2 + 3 * 5 首先被认为是: 2, + , <code>3</code>, *, 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="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="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>使用'as'做类型转换跟正常的类型声明,对于编译器来说是一样的。例如:</p>
<pre><code class="swift">let y1 = x as SomeType // Type information from 'as'
let y2: SomeType = x // Type information from an annotation
</code></pre>
<p>'is' 运算符在“运行时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="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>__FILE__</td>
<td>String</td>
<td>所在的文件名</td>
</tr>
<tr>
<td>__LINE__</td>
<td>Int</td>
<td>所在的行数</td>
</tr>
<tr>
<td>__COLUMN__</td>
<td>Int</td>
<td>所在的列数</td>
</tr>
<tr>
<td>__FUNCTION__</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>数组中的最后一个表达式可以紧跟一个逗号(','. []表示空数组 。 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 的最后一个表达式可以是一个逗号(','. [:] 表示一个空的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>__FILE__</em>­| <em>__LINE__</em>­| <em>__COLUMN__</em>­| <em>__FUNCTION__</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, 而不是 initgreeting: String </p>
<pre><code class="swift">class SomeClass {
var greeting: String
initgreeting: String {
self.greeting = greeting
}
}
</code></pre>
<p>在mutating 方法中, 你可以使用self 对 该instance进行赋值。</p>
<pre><code class="swift">struct Point {
var x = 0.0, y = 0.0
mutating func moveByXdeltaX: Double, y deltaY: Double {
self = Pointx: 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="swift"> { parameters -&gt; return type in
statements
}
</code></pre>
<p>闭包的参数声明形式跟方法中的声明一样, 请参见Function Declaration.</p>
<p>闭包还有几种特殊的形式, 让使用更加简洁:</p>
<ul>
<li>闭包可以省略 它的参数的type 和返回值的type. 如果省略了参数和参数类型,就也要省略 'in'关键字。 如果被省略的type 无法被编译器获知inferred ,那么就会抛出编译错误。</li>
<li>闭包可以省略参数转而在方法体statement中使用 $0, $1, $2 来引用出现的第一个,第二个,第三个参数。</li>
<li>如果闭包中只包含了一个表达式,那么该表达式就会自动成为该闭包的返回值。 在执行 'type inference '时,该表达式也会返回。</li>
</ul>
<p>下面几个 闭包表达式是 等价的:</p>
<pre><code class="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 来显式指定它需要的参数。 参数列表 由中括号 [] 括起来,里面的参数由逗号','分隔。一旦使用了参数列表,就必须使用'in'关键字在任何情况下都得这样做包括忽略参数的名字type, 返回值时等等)。</p>
<p>在闭包的参数列表( capture list 参数可以声明为 'weak' 或者 'unowned' .</p>
<pre><code class="swift">myFunction { printself.title } // strong capture
myFunction { [weak self] in printself!.title } // weak capture
myFunction { [unowned self] in printself.title } // unowned capture
</code></pre>
<p>在参数列表中,也可以使用任意表达式来赋值. 该表达式会在 闭包被执行时赋值然后按照不同的力度来获取这句话请慎重理解captured with the specified strength. 例如:</p>
<pre><code class="swift">// Weak capture of &quot;self.parent&quot; as &quot;parent&quot;
myFunction { [weak parent = self.parent] in printparent!.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­| unownedsafe­| unownedunsafe­</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="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>圆括号表达式由多个子表达式和逗号','组成。 每个子表达式前面可以有 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="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="swift">// someFunction takes an integer and a closure as its arguments
someFunctionx, {$0 == 13}
someFunctionx {$0 == 13}
</code></pre>
<p>如果闭包是该函数的唯一参数,那么圆括号可以省略。</p>
<pre><code class="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="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="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="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>后缀表达式由 某个表达式 + '.self' 组成. 形式如下:</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 表达式由 某个表达式 + '.dynamicType' 组成。</p>
<p><code>expression</code>.dynamicType</p>
<p>上面的形式中, expression 不能是某type的名字当然了如果我都知道它的名字了还需要动态来获取它吗。动态类型表达式会返回"运行时"某个instance的type, 具体请看下面的列子:</p>
<pre><code class="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>可选链表达式由目标表达式 + '?' 组成,形式如下:</p>
<p><code>expression</code>?</p>
<p>后缀'?' 返回目标表达式的值,把它做为可选的参数传递给后续的表达式</p>
<p>如果某个后缀表达式包含了可选链表达式,那么它的执行过程就比较特殊: 首先先判断该可选链表达式的值,如果是 nil, 整个后缀表达式都返回 nil, 如果该可选链的值不是nil, 则正常返回该后缀表达式的值依次执行它的各个子表达式。在这两种情况下该后缀表达式仍然是一个optional typeIn either case, the value of the postfix expression is still of an optional type</p>
<p>如果某个"后缀表达式"的"子表达式"中包含了"可选链表达式"那么只有最外层的表达式返回的才是一个optional type. 例如,在下面的例子中, 如果c 不是nil, 那么 c?.property.performAction 这句代码在执行时就会先获得c 的property方法然后调用 performAction方法。 然后对于 "c?.property.performAction" 这个整体它的返回值是一个optional type.</p>
<pre><code class="swift">var c: SomeClass?
var result: Bool? = c?.property.performAction
</code></pre>
<p>如果不使用可选链表达式,那么 上面例子的代码跟下面例子等价:</p>
<pre><code class="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></body></html>

View File

@ -7,13 +7,13 @@
本页包含内容:
- [前缀表达式Prefix Expressions]
- [二元表达式Binary Expressions]
- [赋值表达式Assignment Operator]
- [三元条件运算符Ternary Conditional Operator]
- [类型转换运算符Type-Casting Operators]
- [主要表达式Primary Expressions]
- [后缀表达式Postfix Expressions]
- [前缀表达式Prefix Expressions](#prefix_expressions)
- [二元表达式Binary Expressions](#binary_exprssions)
- [赋值表达式Assignment Operator](#assignment_operator)
- [三元条件运算符Ternary Conditional Operator](#ternary_conditional_operator)
- [类型转换运算符Type-Casting Operators](#type-casting_operators)
- [主要表达式Primary Expressions](#primary_expressions)
- [后缀表达式Postfix Expressions](#postfix_expressions)
Swift 中存在四种表达式: 前缀prefix表达式二元binary表达式主要primary表达式和后缀postfix表达式。表达式可以返回一个值以及运行某些逻辑causes a side effect
@ -24,6 +24,7 @@ Swift 中存在四种表达式: 前缀prefix表达式二元binary
> *expression* → *prefix-expression*­*binary-expressions(*opt)
> *expression-list* → *expression*­| *expression*­,­*expression-list*
<a name="prefix_expressions"></a>
## 前缀表达式Prefix Expressions
前缀表达式由 前缀符号和表达式组成。(这个前缀符号只能接收一个参数)
@ -47,6 +48,7 @@ Swift 标准库支持如下的前缀操作符:
> *prefix-expression* → *in-out-expression*­
> *in-out-expression* → &­*identifier*­
<a name="binary_expressions"></a>
## 二元表达式Binary Expressions
二元表达式由 "左边参数" + "二元运算符" + "右边参数" 组成, 它有如下的形式:
@ -124,6 +126,7 @@ Swift 标准库提供了如下的二元运算符:
> *binary-expression* → *type-casting-operator*­
> *binary-expression*s → *binary-expression*­*binary-expressions*(opt)
<a name="assignment_operator"></a>
## 赋值表达式Assignment Operator
The assigment operator sets a new value for a given expression. It has the following form:
@ -144,6 +147,7 @@ The assigment operator sets a new value for a given expression. It has the follo
>
> *assignment-operator* → =­
<a name="ternary_conditional_operator"></a>
## 三元条件运算符Ternary Conditional Operator
三元条件运算符 是根据条件来获取值。 形式如下:
@ -158,6 +162,7 @@ The assigment operator sets a new value for a given expression. It has the follo
>
> `conditional-operator` → ?­`expression`­:­
<a name="type-casting_operators"></a>
## 类型转换运算符Type-Casting Operators
有两种类型转换操作符: as 和 is. 它们有如下的形式:
@ -208,6 +213,7 @@ The check must not be known to be true or false at compile time. The following a
>
> *type-casting-operator* → is­*type*­| as­?(opt)­*type*
<a name="primary_expressions"></a>
## 主要表达式Primary Expressions
`主要表达式`是最基本的表达式。 它们可以跟 前缀表达式,二元表达式,后缀表达式以及其他主要表达式组合使用。
@ -433,6 +439,7 @@ x = .AnotherValue
>
> *wildcard-expression* → _­
<a name="postfix_expressions"></a>
## 后缀表达式Postfix Expressions
后缀表达式就是在某个表达式的后面加上 操作符。 严格的讲每个主要表达式primary expression都是一个后缀表达式

View File

@ -108,10 +108,4 @@
> generic-argument-list → generic-argument­ generic-argument­,­generic-argument-list­
> generic-argument → type
==============================================
上篇:[模式](07_Patterns.md)
下篇:[语法总结](09_Summary_of_the_Grammar.md)
> generic-argument → type