make gitbook
This commit is contained in:
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.3" data-basepath=".." data-revision="1402750255397">
|
||||
<div class="book" data-level="3.3" data-basepath=".." data-revision="1402759431779">
|
||||
<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,11 +587,10 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_85">
|
||||
<section class="normal" id="section-gitbook_561">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:lyuka</p>
|
||||
<p>校对:numbbbbb</p>
|
||||
<p>翻译:lyuka<br>校对:numbbbbb, stanzhai</p>
|
||||
</blockquote>
|
||||
<h1 id="-types-">类型(Types)</h1>
|
||||
<hr>
|
||||
@ -614,36 +613,32 @@
|
||||
<p><em>复合型类型</em>是没有名字的类型,它由 Swift 本身定义。Swift 存在两种复合型类型:函数类型和元组类型。一个复合型类型可以包含命名型类型和其它复合型类型。例如,元组类型<code>(Int, (Int, Int))</code>包含两个元素:第一个是命名型类型<code>Int</code>,第二个是另一个复合型类型<code>(Int, Int)</code>.</p>
|
||||
<p>本节讨论 Swift 语言本身定义的类型,并描述 Swift 中的类型推断行为。</p>
|
||||
<blockquote>
|
||||
<p>类型的语法:
|
||||
<em>type</em> → <em>array-type</em> | <em>function-type</em> | <em>type-identifier</em> | <em>tuple-type</em> | <em>optional-type</em> | <em>implicitly-unwrapped-optional-type</em> | protocol-composition-type | metatype-type</p>
|
||||
<p>类型语法<br><em>类型</em> → <a href="..\chapter3\03_Types.html#array_type"><em>数组类型</em></a> | <a href="..\chapter3\03_Types.html#function_type"><em>函数类型</em></a> | <a href="..\chapter3\03_Types.html#type_identifier"><em>类型标识</em></a> | <a href="..\chapter3\03_Types.html#tuple_type"><em>元组类型</em></a> | <a href="..\chapter3\03_Types.html#optional_type"><em>可选类型</em></a> | <a href="..\chapter3\03_Types.html#implicitly_unwrapped_optional_type"><em>隐式解析可选类型</em></a> | <a href="..\chapter3\03_Types.html#protocol_composition_type"><em>协议合成类型</em></a> | <a href="..\chapter3\03_Types.html#metatype_type"><em>元型类型</em></a> </p>
|
||||
</blockquote>
|
||||
<p><a name="type_annotation"></a></p>
|
||||
<h2 id="-">类型注解</h2>
|
||||
<p>类型注解显式地指定一个变量或表达式的值。类型注解始于冒号<code>:</code>终于类型,比如下面两个例子:</p>
|
||||
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> someTuple:(Double, Double) = (<span class="hljs-number">3.14159</span>, <span class="hljs-number">2.71828</span>)
|
||||
func someFunction(a: Int){ <span class="hljs-comment">/* ... */</span> }
|
||||
<pre><code class="lang-swift">let someTuple:(Double, Double) = (3.14159, 2.71828)
|
||||
func someFunction(a: Int){ /* ... */ }
|
||||
</code></pre>
|
||||
<p>在第一个例子中,表达式<code>someTuple</code>的类型被指定为<code>(Double, Double)</code>。在第二个例子中,函数<code>someFunction</code>的参数<code>a</code>的类型被指定为<code>Int</code>。</p>
|
||||
<p>类型注解可以在类型之前包含一个类型特性(type attributes)的可选列表。</p>
|
||||
<blockquote>
|
||||
<p>类型注解的语法:
|
||||
<em>type-annotation</em> → :<em>attributes</em>[opt] <em>type</em></p>
|
||||
<p>类型注解语法<br><em>类型注解</em> → <strong>:</strong> <a href="..\chapter3\06_Attributes.html#attributes"><em>特性(Attributes)列表</em></a> <em>可选</em> <a href="..\chapter3\03_Types.html#type"><em>类型</em></a> </p>
|
||||
</blockquote>
|
||||
<p><a name="type_identifier"></a></p>
|
||||
<h2 id="-">类型标识符</h2>
|
||||
<p>类型标识符引用命名型类型或者是命名型/复合型类型的别名。</p>
|
||||
<p>大多数情况下,类型标识符引用的是同名的命名型类型。例如类型标识符<code>Int</code>引用命名型类型<code>Int</code>,同样,类型标识符<code>Dictionary<String, Int></code>引用命名型类型<code>Dictionary<String, Int></code>。</p>
|
||||
<p>在两种情况下类型标识符引用的不是同名的类型。情况一,类型标识符引用的是命名型/复合型类型的类型别名。比如,在下面的例子中,类型标识符使用<code>Point</code>来引用元组<code>(Int, Int)</code>:</p>
|
||||
<pre><code class="lang-javascript">typealias Point = (Int, Int)
|
||||
<span class="hljs-keyword">let</span> origin: Point = (<span class="hljs-number">0</span>, <span class="hljs-number">0</span>)
|
||||
<pre><code class="lang-swift">typealias Point = (Int, Int)
|
||||
let origin: Point = (0, 0)
|
||||
</code></pre>
|
||||
<p>情况二,类型标识符使用dot(<code>.</code>)语法来表示在其它模块(modules)或其它类型嵌套内声明的命名型类型。例如,下面例子中的类型标识符引用在<code>ExampleModule</code>模块中声明的命名型类型<code>MyType</code>:</p>
|
||||
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> someValue: ExampleModule.MyType
|
||||
<pre><code class="lang-swift">var someValue: ExampleModule.MyType
|
||||
</code></pre>
|
||||
<blockquote>
|
||||
<p>类型标识符的语法:
|
||||
<em>type-identifier</em> → <em>type-name generic-argument-clause</em>[opt] | <em>type-name generic-argument-clause</em>[opt].<em>type-identifier</em>
|
||||
<em>type-name</em> → <em>identifier</em></p>
|
||||
<p>类型标识语法<br><em>类型标识</em> → <a href="..\chapter3\03_Types.html#type_name"><em>类型名称</em></a> <a href="GenericParametersAndArguments.html#generic_argument_clause"><em>泛型参数子句</em></a> <em>可选</em> | <a href="..\chapter3\03_Types.html#type_name"><em>类型名称</em></a> <a href="GenericParametersAndArguments.html#generic_argument_clause"><em>泛型参数子句</em></a> <em>可选</em> <strong>.</strong> <a href="..\chapter3\03_Types.html#type_identifier"><em>类型标识</em></a><br><em>类名</em> → <a href="LexicalStructure.html#identifier"><em>标识符</em></a> </p>
|
||||
</blockquote>
|
||||
<p><a name="tuple_type"></a></p>
|
||||
<h2 id="-">元组类型</h2>
|
||||
@ -651,12 +646,7 @@ func someFunction(a: Int){ <span class="hljs-comment">/* ... */</span> }
|
||||
<p>你可以使用元组类型作为一个函数的返回类型,这样就可以使函数返回多个值。你也可以命名元组类型中的元素,然后用这些名字来引用每个元素的值。元素的名字由一个标识符和<code>:</code>组成。“函数和多返回值”章节里有一个展示上述特性的例子。</p>
|
||||
<p><code>void</code>是空元组类型<code>()</code>的别名。如果括号内只有一个元素,那么该类型就是括号内元素的类型。比如,<code>(Int)</code>的类型是<code>Int</code>而不是<code>(Int)</code>。所以,只有当元组类型包含两个元素以上时才可以标记元组元素。</p>
|
||||
<blockquote>
|
||||
<p>元组类型语法:
|
||||
<em>tuple</em> → (<em>tuple-type-body</em>[opt])
|
||||
<em>tuple-type-body</em> → <em>tuple-type-element-list</em> ...[opt]
|
||||
<em>tuple-type-element-list</em> → <em>tuple-type-element</em> | <em>tuple-type-element</em>, <em>tuple-type-element-list</em>
|
||||
<em>tuple-type-element</em> → <em>attributes</em>[opt] <strong>inout</strong> [opt] <em>type</em> | <strong>inout</strong> [opt] <em>element-name type-annotation</em>
|
||||
<em>element-name</em> → <em>identifier</em></p>
|
||||
<p>元组类型语法<br><em>元组类型</em> → <strong>(</strong> <a href="..\chapter3\03_Types.html#tuple_type_body"><em>元组类型主体</em></a> <em>可选</em> <strong>)</strong><br><em>元组类型主体</em> → <a href="..\chapter3\03_Types.html#tuple_type_element_list"><em>元组类型的元素列表</em></a> <strong>...</strong> <em>可选</em><br><em>元组类型的元素列表</em> → <a href="..\chapter3\03_Types.html#tuple_type_element"><em>元组类型的元素</em></a> | <a href="..\chapter3\03_Types.html#tuple_type_element"><em>元组类型的元素</em></a> <strong>,</strong> <a href="..\chapter3\03_Types.html#tuple_type_element_list"><em>元组类型的元素列表</em></a><br><em>元组类型的元素</em> → <a href="..\chapter3\06_Attributes.html#attributes"><em>特性(Attributes)列表</em></a> <em>可选</em> <strong>inout</strong> <em>可选</em> <a href="..\chapter3\03_Types.html#type"><em>类型</em></a> | <strong>inout</strong> <em>可选</em> <a href="..\chapter3\03_Types.html#element_name"><em>元素名</em></a> <a href="..\chapter3\03_Types.html#type_annotation"><em>类型注解</em></a><br><em>元素名</em> → <a href="LexicalStructure.html#identifier"><em>标识符</em></a> </p>
|
||||
</blockquote>
|
||||
<p><a name="function_type"></a></p>
|
||||
<h2 id="-">函数类型</h2>
|
||||
@ -666,81 +656,78 @@ func someFunction(a: Int){ <span class="hljs-comment">/* ... */</span> }
|
||||
</ul>
|
||||
<p>由于 <em>参数类型</em> 和 <em>返回值类型</em> 可以是元组类型,所以函数类型可以让函数与方法支持多参数与多返回值。</p>
|
||||
<p>你可以对函数类型应用带有参数类型<code>()</code>并返回表达式类型的<code>auto_closure</code>属性(见类型属性章节)。一个自动闭包函数捕获特定表达式上的隐式闭包而非表达式本身。下面的例子使用<code>auto_closure</code>属性来定义一个很简单的assert函数:</p>
|
||||
<pre><code class="lang-javascript">func simpleAssert(condition: @auto_closure () -> Bool, message: <span class="hljs-built_in">String</span>){
|
||||
<span class="hljs-keyword">if</span> !condition(){
|
||||
<pre><code class="lang-swift">func simpleAssert(condition: @auto_closure () -> Bool, message: String){
|
||||
if !condition(){
|
||||
println(message)
|
||||
}
|
||||
}
|
||||
<span class="hljs-keyword">let</span> testNumber = <span class="hljs-number">5</span>
|
||||
simpleAssert(testNumber % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>, <span class="hljs-string">"testNumber isn't an even number."</span>)
|
||||
<span class="hljs-comment">// prints "testNumber isn't an even number."</span>
|
||||
let testNumber = 5
|
||||
simpleAssert(testNumber % 2 == 0, "testNumber isn't an even number.")
|
||||
// prints "testNumber isn't an even number."
|
||||
</code></pre>
|
||||
<p>函数类型可以拥有一个可变长参数作为参数类型中的最后一个参数。从语法角度上讲,可变长参数由一个基础类型名字和<code>...</code>组成,如<code>Int...</code>。可变长参数被认为是一个包含了基础类型元素的数组。即<code>Int...</code>就是<code>Int[]</code>。关于使用可变长参数的例子,见章节“可变长参数”。</p>
|
||||
<p>为了指定一个<code>in-out</code>参数,可以在参数类型前加<code>inout</code>前缀。但是你不可以对可变长参数或返回值类型使用<code>inout</code>。关于In-Out参数的讨论见章节In-Out参数部分。</p>
|
||||
<p>柯里化函数(curried function)的类型相当于一个嵌套函数类型。例如,下面的柯里化函数<code>addTwoNumber()()</code>的类型是<code>Int -> Int -> Int</code>:</p>
|
||||
<pre><code class="lang-javascript">func addTwoNumbers(a: Int)(b: Int) -> Int{
|
||||
<span class="hljs-keyword">return</span> a + b
|
||||
<pre><code class="lang-swift">func addTwoNumbers(a: Int)(b: Int) -> Int{
|
||||
return a + b
|
||||
}
|
||||
addTwoNumbers(<span class="hljs-number">4</span>)(<span class="hljs-number">5</span>) <span class="hljs-comment">// returns 9</span>
|
||||
addTwoNumbers(4)(5) // returns 9
|
||||
</code></pre>
|
||||
<p>柯里化函数的函数类型从右向左组成一组。例如,函数类型<code>Int -> Int -> Int</code>可以被理解为<code>Int -> (Int -> Int)</code>——也就是说,一个函数传入一个<code>Int</code>然后输出作为另一个函数的输入,然后又返回一个<code>Int</code>。例如,你可以使用如下嵌套函数来重写柯里化函数<code>addTwoNumbers()()</code>:</p>
|
||||
<pre><code class="lang-javascript">func addTwoNumbers(a: Int) -> (Int -> Int){
|
||||
<pre><code class="lang-swift">func addTwoNumbers(a: Int) -> (Int -> Int){
|
||||
func addTheSecondNumber(b: Int) -> Int{
|
||||
<span class="hljs-keyword">return</span> a + b
|
||||
return a + b
|
||||
}
|
||||
<span class="hljs-keyword">return</span> addTheSecondNumber
|
||||
return addTheSecondNumber
|
||||
}
|
||||
addTwoNumbers(<span class="hljs-number">4</span>)(<span class="hljs-number">5</span>) <span class="hljs-comment">// Returns 9</span>
|
||||
addTwoNumbers(4)(5) // Returns 9
|
||||
</code></pre>
|
||||
<blockquote>
|
||||
<p>函数类型的语法:
|
||||
<em>function-type</em> → <em>type</em> <strong>-></strong> <em>type</em></p>
|
||||
<p>函数类型语法<br><em>函数类型</em> → <a href="..\chapter3\03_Types.html#type"><em>类型</em></a> <strong>-></strong> <a href="..\chapter3\03_Types.html#type"><em>类型</em></a> </p>
|
||||
</blockquote>
|
||||
<p><a name="array_type"></a></p>
|
||||
<h2 id="-">数组类型</h2>
|
||||
<p>Swift语言使用类型名紧接中括号<code>[]</code>来简化标准库中定义的命名型类型<code>Array<T></code>。换句话说,下面两个声明是等价的:</p>
|
||||
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> someArray: <span class="hljs-built_in">String</span>[] = [<span class="hljs-string">"Alex"</span>, <span class="hljs-string">"Brian"</span>, <span class="hljs-string">"Dave"</span>]
|
||||
<span class="hljs-keyword">let</span> someArray: <span class="hljs-built_in">Array</span><<span class="hljs-built_in">String</span>> = [<span class="hljs-string">"Alex"</span>, <span class="hljs-string">"Brian"</span>, <span class="hljs-string">"Dave"</span>]
|
||||
<pre><code class="lang-swift">let someArray: String[] = ["Alex", "Brian", "Dave"]
|
||||
let someArray: Array<String> = ["Alex", "Brian", "Dave"]
|
||||
</code></pre>
|
||||
<p>上面两种情况下,常量<code>someArray</code>都被声明为字符串数组。数组的元素也可以通过<code>[]</code>获取访问:<code>someArray[0]</code>是指第0个元素<code>“Alex”</code>。</p>
|
||||
<p>上面的例子同时显示,你可以使用<code>[]</code>作为初始值构造数组,空的<code>[]</code>则用来来构造指定类型的空数组。</p>
|
||||
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> emptyArray: Double[] = []
|
||||
<pre><code class="lang-swift">var emptyArray: Double[] = []
|
||||
</code></pre>
|
||||
<p>你也可以使用链接起来的多个<code>[]</code>集合来构造多维数组。例如,下例使用三个<code>[]</code>集合来构造三维整型数组:</p>
|
||||
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> array3D: Int[][][] = [[[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>], [<span class="hljs-number">3</span>, <span class="hljs-number">4</span>]], [[<span class="hljs-number">5</span>, <span class="hljs-number">6</span>], [<span class="hljs-number">7</span>, <span class="hljs-number">8</span>]]]
|
||||
<pre><code class="lang-swift">var array3D: Int[][][] = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
|
||||
</code></pre>
|
||||
<p>访问一个多维数组的元素时,最左边的下标指向最外层数组的相应位置元素。接下来往右的下标指向第一层嵌入的相应位置元素,依次类推。这就意味着,在上面的例子中,<code>array3D[0]</code>是指<code>[[1, 2], [3, 4]]</code>,<code>array3D[0][1]</code>是指<code>[3, 4]</code>,<code>array3D[0][1][1]</code>则是指值<code>4</code>。</p>
|
||||
<p>关于Swift标准库中<code>Array</code>类型的细节讨论,见章节Arrays。</p>
|
||||
<blockquote>
|
||||
<p>数组类型的语法:
|
||||
<em>array-type</em> → <em>type</em><code>[ ]</code> | <em>array-type</em><code>[ ]</code></p>
|
||||
<p>数组类型语法<br><em>数组类型</em> → <a href="..\chapter3\03_Types.html#type"><em>类型</em></a> <strong>[</strong> <strong>]</strong> | <a href="..\chapter3\03_Types.html#array_type"><em>数组类型</em></a> <strong>[</strong> <strong>]</strong> </p>
|
||||
</blockquote>
|
||||
<p><a name="optional_type"></a></p>
|
||||
<h2 id="-">可选类型</h2>
|
||||
<p>Swift定义后缀<code>?</code>来作为标准库中的定义的命名型类型<code>Optional<T></code>的简写。换句话说,下面两个声明是等价的:</p>
|
||||
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> optionalInteger: Int?
|
||||
<span class="hljs-keyword">var</span> optionalInteger: Optional<Int>
|
||||
<pre><code class="lang-swift">var optionalInteger: Int?
|
||||
var optionalInteger: Optional<Int>
|
||||
</code></pre>
|
||||
<p>在上述两种情况下,变量<code>optionalInteger</code>都被声明为可选整型类型。注意在类型和<code>?</code>之间没有空格。</p>
|
||||
<p>类型<code>Optional<T></code>是一个枚举,有两种形式,<code>None</code>和<code>Some(T)</code>,又来代表可能出现或可能不出现的值。任意类型都可以被显式的声明(或隐式的转换)为可选类型。当声明一个可选类型时,确保使用括号给<code>?</code>提供合适的作用范围。比如说,声明一个整型的可选数组,应写作<code>(Int[])?</code>,写成<code>Int[]?</code>的话则会出错。</p>
|
||||
<p>如果你在声明或定义可选变量或特性的时候没有提供初始值,它的值则会自动赋成缺省值<code>nil</code>。</p>
|
||||
<p>可选符合<code>LogicValue</code>协议,因此可以出现在布尔值环境下。此时,如果一个可选类型<code>T?</code>实例包含有类型为<code>T</code>的值(也就是说值为<code>Optional.Some(T)</code>),那么此可选类型就为<code>true</code>,否则为<code>false</code>。</p>
|
||||
<p>如果一个可选类型的实例包含一个值,那么你就可以使用后缀操作符<code>!</code>来获取该值,正如下面描述的:</p>
|
||||
<pre><code class="lang-javascript">optionalInteger = <span class="hljs-number">42</span>
|
||||
optionalInteger! <span class="hljs-comment">// 42</span>
|
||||
<pre><code class="lang-swift">optionalInteger = 42
|
||||
optionalInteger! // 42
|
||||
</code></pre>
|
||||
<p>使用<code>!</code>操作符获取值为<code>nil</code>的可选项会导致运行错误(runtime error)。</p>
|
||||
<p>你也可以使用可选链和可选绑定来选择性的执行可选表达式上的操作。如果值为<code>nil</code>,不会执行任何操作因此也就没有运行错误产生。</p>
|
||||
<p>更多细节以及更多如何使用可选类型的例子,见章节“可选”。</p>
|
||||
<blockquote>
|
||||
<p>可选类型语法:
|
||||
<em>optional-type</em> → <em>type</em>?</p>
|
||||
<p>可选类型语法<br><em>可选类型</em> → <a href="..\chapter3\03_Types.html#type"><em>类型</em></a> <strong>?</strong> </p>
|
||||
</blockquote>
|
||||
<p><a name="implicitly_unwrapped_optional_type"></a></p>
|
||||
<h2 id="-">隐式解析可选类型</h2>
|
||||
<p>Swift语言定义后缀<code>!</code>作为标准库中命名类型<code>ImplicitlyUnwrappedOptional<T></code>的简写。换句话说,下面两个声明等价:</p>
|
||||
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> implicitlyUnwrappedString: <span class="hljs-built_in">String</span>!
|
||||
<span class="hljs-keyword">var</span> implicitlyUnwrappedString: ImplicitlyUnwrappedOptional<<span class="hljs-built_in">String</span>>
|
||||
<pre><code class="lang-swift">var implicitlyUnwrappedString: String!
|
||||
var implicitlyUnwrappedString: ImplicitlyUnwrappedOptional<String>
|
||||
</code></pre>
|
||||
<p>上述两种情况下,变量<code>implicitlyUnwrappedString</code>被声明为一个隐式解析可选类型的字符串。注意类型与<code>!</code>之间没有空格。</p>
|
||||
<p>你可以在使用可选的地方同样使用隐式解析可选。比如,你可以将隐式解析可选的值赋给变量、常量和可选特性,反之亦然。</p>
|
||||
@ -749,47 +736,42 @@ optionalInteger! <span class="hljs-comment">// 42</span>
|
||||
<p>使用可选链会选择性的执行隐式解析可选表达式上的某一个操作。如果值为<code>nil</code>,就不会执行任何操作,因此也不会产生运行错误。</p>
|
||||
<p>关于隐式解析可选的更多细节,见章节“隐式解析可选”。</p>
|
||||
<blockquote>
|
||||
<p>隐式解析可选的语法:
|
||||
implicitly-unwrapped-optional-type → type!</p>
|
||||
<p>隐式解析可选类型(Implicitly Unwrapped Optional Type)语法<br><em>隐式解析可选类型</em> → <a href="..\chapter3\03_Types.html#type"><em>类型</em></a> <strong>!</strong> </p>
|
||||
</blockquote>
|
||||
<p><a name="protocol_composition_type"></a></p>
|
||||
<h2 id="-">协议合成类型</h2>
|
||||
<p>协议合成类型是一种符合每个协议的指定协议列表类型。协议合成类型可能会用在类型注解和泛型参数中。</p>
|
||||
<p>协议合成类型的形式如下:</p>
|
||||
<pre><code class="lang-javascript">protocol<Protocol <span class="hljs-number">1</span>, Procotol <span class="hljs-number">2</span>>
|
||||
<pre><code class="lang-swift">protocol<Protocol 1, Procotol 2>
|
||||
</code></pre>
|
||||
<p>协议合成类型允许你指定一个值,其类型可以适配多个协议的条件,而且不需要定义一个新的命名型协议来继承其它想要适配的各个协议。比如,协议合成类型<code>protocol<Protocol A, Protocol B, Protocol C></code>等效于一个从<code>Protocol A</code>,<code>Protocol B</code>, <code>Protocol C</code>继承而来的新协议<code>Protocol D</code>,很显然这样做有效率的多,甚至不需引入一个新名字。</p>
|
||||
<p>协议合成列表中的每项必须是协议名或协议合成类型的类型别名。如果列表为空,它就会指定一个空协议合成列表,这样每个类型都能适配。</p>
|
||||
<blockquote>
|
||||
<p>协议合成类型的语法:
|
||||
<em>protocol-composition-type</em> → <strong>protocol</strong> <<em>protocol-identifier-list[opt]</em>>
|
||||
<em>protocol-identifier-list</em> → <em>protocol-identifier</em> | <em>protocol-identifier, protocol-identifier-list</em>
|
||||
<em>protocol-identifier</em> → <em>type-identifier</em></p>
|
||||
<p>协议合成类型语法<br><em>协议合成类型</em> → <strong>protocol</strong> <strong><</strong> <a href="..\chapter3\03_Types.html#protocol_identifier_list"><em>协议标识符列表</em></a> <em>可选</em> <strong>></strong><br><em>协议标识符列表</em> → <a href="..\chapter3\03_Types.html#protocol_identifier"><em>协议标识符</em></a> | <a href="..\chapter3\03_Types.html#protocol_identifier"><em>协议标识符</em></a> <strong>,</strong> <a href="..\chapter3\03_Types.html#protocol_identifier_list"><em>协议标识符列表</em></a><br><em>协议标识符</em> → <a href="..\chapter3\03_Types.html#type_identifier"><em>类型标识</em></a> </p>
|
||||
</blockquote>
|
||||
<p><a name="metatype_type"></a></p>
|
||||
<h2 id="-">元类型</h2>
|
||||
<p>元类型是指所有类型的类型,包括类、结构体、枚举和协议。</p>
|
||||
<p>类、结构体或枚举类型的元类型是相应的类型名紧跟<code>.Type</code>。协议类型的元类型——并不是运行时适配该协议的具体类型——是该协议名字紧跟<code>.Protocol</code>。比如,类<code>SomeClass</code>的元类型就是<code>SomeClass.Type</code>,协议<code>SomeProtocol</code>的元类型就是<code>SomeProtocal.Protocol</code>。</p>
|
||||
<p>你可以使用后缀<code>self</code>表达式来获取类型。比如,<code>SomeClass.self</code>返回<code>SomeClass</code>本身,而不是<code>SomeClass</code>的一个实例。同样,<code>SomeProtocol.self</code>返回<code>SomeProtocol</code>本身,而不是运行时适配<code>SomeProtocol</code>的某个类型的实例。还可以对类型的实例使用<code>dynamicType</code>表达式来获取该实例在运行阶段的类型,如下所示:</p>
|
||||
<pre><code class="lang-javascript"><span class="hljs-keyword">class</span> SomeBaseClass {
|
||||
<span class="hljs-keyword">class</span> func printClassName() {
|
||||
println(<span class="hljs-string">"SomeBaseClass"</span>)
|
||||
<pre><code class="lang-swift">class SomeBaseClass {
|
||||
class func printClassName() {
|
||||
println("SomeBaseClass")
|
||||
}
|
||||
}
|
||||
<span class="hljs-keyword">class</span> SomeSubClass: SomeBaseClass {
|
||||
override <span class="hljs-keyword">class</span> func printClassName() {
|
||||
println(<span class="hljs-string">"SomeSubClass"</span>)
|
||||
class SomeSubClass: SomeBaseClass {
|
||||
override class func printClassName() {
|
||||
println("SomeSubClass")
|
||||
}
|
||||
}
|
||||
<span class="hljs-keyword">let</span> someInstance: SomeBaseClass = SomeSubClass()
|
||||
<span class="hljs-comment">// someInstance is of type SomeBaseClass at compile time, but</span>
|
||||
<span class="hljs-comment">// someInstance is of type SomeSubClass at runtime</span>
|
||||
let someInstance: SomeBaseClass = SomeSubClass()
|
||||
// someInstance is of type SomeBaseClass at compile time, but
|
||||
// someInstance is of type SomeSubClass at runtime
|
||||
someInstance.dynamicType.printClassName()
|
||||
<span class="hljs-comment">// prints "SomeSubClass</span>
|
||||
// prints "SomeSubClass
|
||||
</code></pre>
|
||||
<blockquote>
|
||||
<p>元类型的语法:
|
||||
<em>metatype-type</em> → <em>type</em>.<strong>Type</strong> | <em>type</em>.<strong>Protocol</strong></p>
|
||||
<p>元(Metatype)类型语法<br><em>元类型</em> → <a href="..\chapter3\03_Types.html#type"><em>类型</em></a> <strong>.</strong> <strong>Type</strong> | <a href="..\chapter3\03_Types.html#type"><em>类型</em></a> <strong>.</strong> <strong>Protocol</strong> x</p>
|
||||
</blockquote>
|
||||
<p><a name="type_inheritance_clause"></a></p>
|
||||
<h2 id="-">类型继承子句</h2>
|
||||
@ -798,17 +780,15 @@ someInstance.dynamicType.printClassName()
|
||||
<p>其它命名型类型可能只继承或适配一个协议列表。协议类型可能继承于其它任意数量的协议。当一个协议类型继承于其它协议时,其它协议的条件集合会被集成在一起,然后其它从当前协议继承的任意类型必须适配所有这些条件。</p>
|
||||
<p>枚举定义中的类型继承子句可以是一个协议列表,或是指定原始值的枚举,一个单独的指定原始值类型的命名型类型。使用类型继承子句来指定原始值类型的枚举定义的例子,见章节“原始值”。</p>
|
||||
<blockquote>
|
||||
<p>类型继承子句的语法:
|
||||
<em>type-inheritance-clause</em> → :<em>type-inheritance-list</em>
|
||||
<em>type-inheritance-list</em> → <em>type-identifier</em> | <em>type-identifier</em>, <em>type-inheritance-list</em></p>
|
||||
<p>类型继承子句语法<br><em>类型继承子句</em> → <strong>:</strong> <a href="..\chapter3\03_Types.html#type_inheritance_list"><em>类型继承列表</em></a><br><em>类型继承列表</em> → <a href="..\chapter3\03_Types.html#type_identifier"><em>类型标识</em></a> | <a href="..\chapter3\03_Types.html#type_identifier"><em>类型标识</em></a> <strong>,</strong> <a href="..\chapter3\03_Types.html#type_inheritance_list"><em>类型继承列表</em></a></p>
|
||||
</blockquote>
|
||||
<p><a name="type_inference"></a></p>
|
||||
<h2 id="-">类型推断</h2>
|
||||
<p>Swift广泛的使用类型推断,从而允许你可以忽略很多变量和表达式的类型或部分类型。比如,对于<code>var x: Int = 0</code>,你可以完全忽略类型而简写成<code>var x = 0</code>——编译器会正确的推断出<code>x</code>的类型<code>Int</code>。类似的,当完整的类型可以从上下文推断出来时,你也可以忽略类型的一部分。比如,如果你写了<code>let dict: Dictionary = ["A": 1]</code>,编译提也能推断出<code>dict</code>的类型是<code>Dictionary<String, Int></code>。</p>
|
||||
<p>在上面的两个例子中,类型信息从表达式树(expression tree)的叶子节点传向根节点。也就是说,<code>var x: Int = 0</code>中<code>x</code>的类型首先根据<code>0</code>的类型进行推断,然后将该类型信息传递到根节点(变量<code>x</code>)。</p>
|
||||
<p>在Swift中,类型信息也可以反方向流动——从根节点传向叶子节点。在下面的例子中,常量<code>eFloat</code>上的显式类型注解(<code>:Float</code>)导致数字字面量<code>2.71828</code>的类型是<code>Float</code>而非<code>Double</code>。</p>
|
||||
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> e = <span class="hljs-number">2.71828</span> <span class="hljs-comment">// The type of e is inferred to be Double.</span>
|
||||
<span class="hljs-keyword">let</span> eFloat: Float = <span class="hljs-number">2.71828</span> <span class="hljs-comment">// The type of eFloat is Float.</span>
|
||||
<pre><code class="lang-swift">let e = 2.71828 // The type of e is inferred to be Double.
|
||||
let eFloat: Float = 2.71828 // The type of eFloat is Float.
|
||||
</code></pre>
|
||||
<p>Swift中的类型推断在单独的表达式或语句水平上进行。这意味着所有用于推断类型的信息必须可以从表达式或其某个子表达式的类型检查中获取。</p>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user