update 02/18

This commit is contained in:
numbbbbb
2014-06-07 21:47:18 +08:00
parent e8fd898886
commit 715dc65f70
40 changed files with 277 additions and 55 deletions

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.1" data-basepath=".." data-revision="1402128272564">
<div class="book" data-level="2.1" data-basepath=".." data-revision="1402148739560">
<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>
@ -576,7 +576,7 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_49">
<section class="normal" id="section-gitbook_10">
<h1 id="-">基础部分</h1>
<p>Swift 是 iOS 和 OS X 应用开发的一门新语言。然而,如果你有 C 或者 Objective-C 开发经验的话,你会发现 Swift 的很多内容都是你熟悉的。</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.2" data-basepath=".." data-revision="1402128272564">
<div class="book" data-level="2.2" data-basepath=".." data-revision="1402148739560">
<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>
@ -576,7 +576,7 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_60">
<section class="normal" id="section-gitbook_21">
<h1 id="-">基础运算符</h1>
<p>运算符是检查, 改变, 合并值的特殊符号或短语. 例如, 加号 <code>+</code> 把计算两个数的和(如 <code>let i = 1 + 2</code>). 复杂些的运行算包括逻辑与<code>&amp;&amp;</code>(如 <code>if enteredDoorCode &amp;&amp; passedRetinaScan</code>), 还有自增运算符 <code>++i</code> 这样让自身加一的便捷运算.</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.3" data-basepath=".." data-revision="1402128272564">
<div class="book" data-level="2.3" data-basepath=".." data-revision="1402148739560">
<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>
@ -576,7 +576,7 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_50">
<section class="normal" id="section-gitbook_11">
<h1 id="-strings-and-characters-">字符串和字符 (Strings and Characters)</h1>
<p>本页包含内容:</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.4" data-basepath=".." data-revision="1402128272564">
<div class="book" data-level="2.4" data-basepath=".." data-revision="1402148739560">
<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="1402128272564">
<div class="book" data-level="2.5" data-basepath=".." data-revision="1402148739560">
<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="1402128272564">
<div class="book" data-level="2.6" data-basepath=".." data-revision="1402148739560">
<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>
@ -576,7 +576,7 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_53">
<section class="normal" id="section-gitbook_14">
<h1 id="-functions-">函数Functions</h1>
<p>本页包含内容:</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.7" data-basepath=".." data-revision="1402128272564">
<div class="book" data-level="2.7" data-basepath=".." data-revision="1402148739560">
<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>
@ -576,7 +576,7 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_54">
<section class="normal" id="section-gitbook_15">
<h1 id="-">闭包</h1>
<p>本页内容包含:</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.8" data-basepath=".." data-revision="1402128272564">
<div class="book" data-level="2.8" data-basepath=".." data-revision="1402148739560">
<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="1402128272564">
<div class="book" data-level="2.9" data-basepath=".." data-revision="1402148739560">
<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>
@ -576,7 +576,7 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_56">
<section class="normal" id="section-gitbook_17">
<h3 id="-">类和结构体</h3>
<p>本页包含内容:</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.10" data-basepath=".." data-revision="1402128272564">
<div class="book" data-level="2.10" data-basepath=".." data-revision="1402148739560">
<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="1402128272564">
<div class="book" data-level="2.11" data-basepath=".." data-revision="1402148739560">
<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="1402128272564">
<div class="book" data-level="2.12" data-basepath=".." data-revision="1402148739560">
<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="1402128272564">
<div class="book" data-level="2.13" data-basepath=".." data-revision="1402148739560">
<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="1402128272564">
<div class="book" data-level="2.14" data-basepath=".." data-revision="1402148739560">
<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="1402128272564">
<div class="book" data-level="2.15" data-basepath=".." data-revision="1402148739560">
<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="1402128272564">
<div class="book" data-level="2.16" data-basepath=".." data-revision="1402148739560">
<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>
@ -576,7 +576,7 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_63">
<section class="normal" id="section-gitbook_24">
<h1 id="-">自动引用计数</h1>
<p>本页包含内容:</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.17" data-basepath=".." data-revision="1402128272564">
<div class="book" data-level="2.17" data-basepath=".." data-revision="1402148739560">
<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="1402128272564">
<div class="book" data-level="2.18" data-basepath=".." data-revision="1402148739560">
<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>
@ -576,6 +576,228 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_26">
<h1 id="-type-casting-">类型检查Type Casting</h1>
<p>ps为了方便各位检验所以保留了英文可删。
<em>类型检查</em>是一种检查类实例的方式,并且哦或者也是让实例作为它的父类或者子类的一种方式。</p>
<p> <em>Type casting</em> is a way to check the type of an instance, and/or to treat that instance as if it is a different superclass or subclass from somewhere else in its own class hierarchy.</p>
<p> 类型检查在Swift中使用<code>is</code><code>as</code>操作符实现。这两个操作符提供了一种简单达意的方式去检查值的类型或者转换它的类型。</p>
<p> Type casting in Swift is implemented with the <code>is</code> and <code>as</code> operators. These two operators provide a simple and expressive way to check the type of a value or cast a value to a different type.</p>
<p> 你也可以用来检查一个类是否实现了某个协议,就像在 <a href="Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-XID_363">Protocols》Checking for Protocol Conformance</a>部分讲述的一样。</p>
<p> You can also use type casting to check whether a type conforms to a protocol, as described in <span class="x-name"><a href="Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-XID_363">Checking for Protocol Conformance</a></span>.</p>
<h3 id="-defining-a-class-hierarchy-for-type-casting">定义一个类层次作为例子Defining a Class Hierarchy for Type Casting</h3>
<p> 你可以将它用在类和子类的层次结构上检查特定类实例的类型并且转换这个类实例的类型成为这个层次结构中的其他类型。这下面的三个代码段定义了一个类层次和一个array包含了几个这些类的实例作为类型检查的例子。</p>
<p> You can use type casting with a hierarchy of classes and subclasses to check the type of a particular class instance and to cast that instance to another class within the same hierarchy. The three code snippets below define a hierarchy of classes and an array containing instances of those classes, for use in an example of type casting.</p>
<p> 第一个代码片段定义了一个新的基础类<code>MediaItem</code>。这个类为任何出现在数字媒体库的项提供基础功能。特别的,它声明了一个 <code>String</code> 类型的 <code>name</code> 属性,和一个<code>init name</code>初始化器。(它假定所有的媒体项都有个名称。)</p>
<p> The first snippet defines a new base class called <code>MediaItem</code>. This class provides basic functionality for any kind of item that appears in a digital media library. Specifically, it declares a <code>name</code> property of type <code>String</code>, and an <code>init name</code> initializer. (It is assumed that all media items, including all movies and songs, will have a name.)</p>
<pre><code>class MediaItem {
var name: String
init(name: String) {
self.name = name
}
}
</code></pre><p> 下一个代码段定义了 <code>MediaItem</code> 的两个子类。第一个子类<code>Movie</code>,在父类(或者说基类)的基础上增加了一个 <code>director</code>(导演) 属性,和相应的初始化器。第二个类在父类的基础上增加了一个 <code>artist</code>(艺术家) 属性,和相应的初始化器:</p>
<p> The next snippet defines two subclasses of <code>MediaItem</code>. The first subclass, <code>Movie</code>, encapsulates additional information about a movie or film. It adds a <code>director</code> property on top of the base <code>MediaItem</code> class, with a corresponding initializer. The second subclass, <code>Song</code>, adds an <code>artist</code> property and initializer on top of the base class:</p>
<pre><code>class Movie: MediaItem {
var director: String
init(name: String, director: String) {
self.director = director
super.init(name: name)
}
}
class Song: MediaItem {
var artist: String
init(name: String, artist: String) {
self.artist = artist
super.init(name: name)
}
}
</code></pre><p> 最后一个代码段创建了一个array常量 <code>library</code> ,包含两个<code>Movie</code>实例和三个<code>Song</code>实例。<code>library</code>的类型是在它被初始化时根据它的array标记符和里面的内容ps: literal 标记符其实就是指“[”和“]”虽然苹果官方的翻译里翻译为字面当总感觉不好理解有点奇怪。不如翻译为标记符推断来的。Swift的类型检测器能够演绎出<code>Movie</code><code>Song</code> 有共同的父类 <code>MediaItem</code> ,所以它推断出 <code>MediaItem[]</code> 类作为 <code>library</code> 的类型。</p>
<p> The final snippet creates a constant array called <code>library</code>, which contains two <code>Movie</code> instances and three <code>Song</code> instances. The type of the <code>library</code> array is inferred by initializing it with the contents of an array literal. Swifts type checker is able to deduce that <code>Movie</code> and <code>Song</code> have a common superclass of <code>MediaItem</code>, and so it infers a type of <code>MediaItem[]</code> for the <code>library</code> array:</p>
<pre><code>let library = [
Movie(name: &quot;Casablanca&quot;, director: &quot;Michael Curtiz&quot;),
Song(name: &quot;Blue Suede Shoes&quot;, artist: &quot;Elvis Presley&quot;),
Movie(name: &quot;Citizen Kane&quot;, director: &quot;Orson Welles&quot;),
Song(name: &quot;The One And Only&quot;, artist: &quot;Chesney Hawkes&quot;),
Song(name: &quot;Never Gonna Give You Up&quot;, artist: &quot;Rick Astley&quot;)
]
// the type of &quot;library&quot; is inferred to be MediaItem[]
</code></pre><p> 在幕后<code>library</code> 里存储的项依然是 <code>Movie</code><code>Song</code> 类型的,但是,若你迭代它,取出的实例会是 <code>MediaItem</code> 类型的,而不是 <code>Movie</code><code>Song</code> 类型的。为了让它们作为它们本来的类型工作,你需要检查它们的类型或者向下转换它们的类型到其它类型,就像下面描述的一样。</p>
<p> The items stored in <code>library</code> are still <code>Movie</code> and <code>Song</code> instances behind the scenes. However, if you iterate over the contents of this array, the items you receive back are typed as <code>MediaItem</code>, and not as <code>Movie</code> or <code>Song</code>. In order to work with them as their native type, you need to <em>check</em> their type, or <em>downcast</em> them to a different type, as described below.</p>
<h3 id="-checking-type">检查类型 Checking Type</h3>
<p> 用类型检查操作符(<code>is</code>)来检查一个实例是否属于特定子类型。类型检查操作符返回 <code>true</code> 若实例属于那个子类型,若不属于返回 <code>false</code></p>
<p> Use the <em>type check operator</em> (<code>is</code>) to check whether an instance is of a certain subclass type. The type check operator returns <code>true</code> if the instance is of that subclass type and <code>false</code> if it is not.</p>
<p> 下面的例子定义了连个变量,<code>movieCount</code><code>songCount</code>,用来计算数组<code>library</code><code>Movie</code><code>Song</code> 类型的实例数量。</p>
<p> The example below defines two variables, <code>movieCount</code> and <code>songCount</code>, which count the number of <code>Movie</code> and <code>Song</code> instances in the <code>library</code> array:</p>
<pre><code>var movieCount = 0
var songCount = 0
for item in library {
if item is Movie {
++movieCount
} else if item is Song {
++songCount
}
}
println(&quot;Media library contains \(movieCount) movies and \(songCount) songs&quot;)
// prints &quot;Media library contains 2 movies and 3 songs&quot;
</code></pre><p> 示例迭代了数组 <code>library</code> 中的所有项。每一次, <code>for</code>-<code>in</code> 循环设置
<code>item</code> 常量的值为数组中的下一个 <code>MediaItem</code></p>
<p> This example iterates through all items in the <code>library</code> array. On each pass, the <code>for</code>-<code>in</code> loop sets the <code>item</code> constant to the next <code>MediaItem</code> in the array.</p>
<p> 若当前 <code>MediaItem</code> 是一个 <code>Movie</code> 类型的实例, <code>item is Movie</code> 返回 <code>true</code>,相反返回 <code>false</code>。同样的,<code>item is Song</code>检查item是否为<code>Song</code>类型的实例。在循环末尾,<code>movieCount</code><code>songCount</code>的值就是被找到属于各自的类型的实例数量。</p>
<p> <code>item is Movie</code> returns <code>true</code> if the current <code>MediaItem</code> is a <code>Movie</code> instance and <code>false</code> if it is not. Similarly, <code>item is Song</code> checks whether the item is a <code>Song</code> instance. At the end of the <code>for</code>-<code>in</code> loop, the values of <code>movieCount</code> and <code>songCount</code> contain a count of how many <code>MediaItem</code> instances were found of each type.</p>
<h3 id="-downcasting">向下转型(简称下转) Downcasting</h3>
<p> 某类型的一个常量或变量可能在幕后实际上属于一个子类。你可以相信,上面就是这种情况。你可以尝试向下转到它的子类型,用类型检查操作符(<code>as</code>)</p>
<p> A constant or variable of a certain class type may actually refer to an instance of a subclass behind the scenes. Where you believe this is the case, you can try to <em>downcast</em> to the subclass type with the <em>type cast operator</em> (<code>as</code>).</p>
<p> 因为向下转型可能会失败,类型检查操作符带有两种不同形式。可选形式( optional form <code>as?</code> 返回一个你试图下转成的类型的可选值optional value。强制形式 <code>as</code> 把试图向下转型和强制解包force-unwraps结果作为一个混合动作。</p>
<p> Because downcasting can fail, the type cast operator comes in two different forms. The optional form, <code>as?</code>, returns an optional value of the type you are trying to downcast to. The forced form, <code>as</code>, attempts the downcast and force-unwraps the result as a single compound action.</p>
<p> 当你不确定下转可以成功时,用类型检查的可选形式(<code>as?</code>)。可选形式的类型检查总是返回一个可选值optional value并且若下转是不可能的可选值将是 <code>nil</code> 。这使你能够检查下转是否成功。</p>
<p> Use the optional form of the type cast operator (<code>as?</code>) when you are not sure if the downcast will succeed. This form of the operator will always return an optional value, and the value will be <code>nil</code> if the downcast was not possible. This enables you to check for a successful downcast.</p>
<p> 只有你可以确定下转一定会成功时才使用强制形式。当你试图下转为一个不正确的类型时强制形式的类型检查会触发一个runtime error。</p>
<p> Use the forced form of the type cast operator (<code>as</code>) only when you are sure that the downcast will always succeed. This form of the operator will trigger a runtime error if you try to downcast to an incorrect class type.</p>
<p> 下面的例子,迭代了<code>library</code>里的每一个 <code>MediaItem</code> 并打印出适当的描述。要这样做item需要真正作为<code>Movie</code><code>Song</code>的类型来使用。不仅仅是作为 <code>MediaItem</code>。为了能够使用<code>Movie</code><code>Song</code><code>director</code><code>artist</code>属性,这是必要的。</p>
<p> The example below iterates over each <code>MediaItem</code> in <code>library</code>, and prints an appropriate description for each item. To do this, it needs to access each item as a true <code>Movie</code> or <code>Song</code>, and not just as a <code>MediaItem</code>. This is necessary in order for it to be able to access the <code>director</code> or <code>artist</code> property of a <code>Movie</code> or <code>Song</code> for use in the description.</p>
<p> 在这个示例中数组中的每一个item可能是 <code>Movie</code><code>Song</code>。 事前你不知道每个item的真实类型所以这里使用可选形式的类型检查 (<code>as?</code>)去检查循环里的每次下转。</p>
<p> In this example, each item in the array might be a <code>Movie</code>, or it might be a <code>Song</code>. You dont know in advance which actual class to use for each item, and so it is appropriate to use the optional form of the type cast operator (<code>as?</code>) to check the downcast each time through the loop:</p>
<pre><code>for item in library {
if let movie = item as? Movie {
println(&quot;Movie: &#39;\(movie.name)&#39;, dir. \(movie.director)&quot;)
} else if let song = item as? Song {
println(&quot;Song: &#39;\(song.name)&#39;, by \(song.artist)&quot;)
}
}
// Movie: &#39;Casablanca&#39;, dir. Michael Curtiz
// Song: &#39;Blue Suede Shoes&#39;, by Elvis Presley
// Movie: &#39;Citizen Kane&#39;, dir. Orson Welles
// Song: &#39;The One And Only&#39;, by Chesney Hawkes
// Song: &#39;Never Gonna Give You Up&#39;, by Rick Astley
</code></pre><p> 示例首先试图将 <code>item</code> 下转为 <code>Movie</code>。因为 <code>item</code> 是一个 <code>MediaItem</code> 类型的实例,它可能是一个<code>Movie</code>;同样,它可能是一个 <code>Song</code>,或者仅仅是基类 <code>MediaItem</code>。因为不确定,<code>as?</code>形式试图下转时返还一个可选值。 <code>item as Movie</code> 的返回值是<code>Movie?</code>类型或 “optional <code>Movie</code>”。</p>
<p> The example starts by trying to downcast the current <code>item</code> as a <code>Movie</code>. Because <code>item</code> is a <code>MediaItem</code> instance, its possible that it <em>might</em> be a <code>Movie</code>; equally, its also possible that it might a <code>Song</code>, or even just a base <code>MediaItem</code>. Because of this uncertainty, the <code>as?</code> form of the type cast operator returns an <em>optional</em> value when attempting to downcast to a subclass type. The result of <code>item as Movie</code> is of type <code>Movie?</code>, or “optional <code>Movie</code>”.</p>
<p> 当应用在两个<code>Song</code>实例时,下转为 <code>Movie</code> 失败。为了处理这种情况上面的实例使用了可选绑定optional binding来检查optional <code>Movie</code>真的包含一个值(这个是为了判断下转是否成功。)可选绑定是这样写的“<code>if let movie = item as? Movie</code>”,可以这样解读:</p>
<p> Downcasting to <code>Movie</code> fails when applied to the two <code>Song</code> instances in the library array. To cope with this, the example above uses optional binding to check whether the optional <code>Movie</code> actually contains a value (that is, to find out whether the downcast succeeded.) This optional binding is written “<code>if let movie = item as? Movie</code>”, which can be read as:</p>
<p> “尝试将 <code>item</code> 转为 <code>Movie</code>类型。若成功,设置一个新的临时常量 <code>movie</code> 来存储返回的optional <code>Movie</code></p>
<p> “Try to access <code>item</code> as a <code>Movie</code>. If this is successful, set a new temporary constant called <code>movie</code> to the value stored in the returned optional <code>Movie</code>.”</p>
<p> 若下转成功,然后<code>movie</code>的属性将用于打印一个<code>Movie</code>实例的描述,包括它的导演的名字<code>director</code>。当<code>Song</code>被找到时,一个相近的原理被用来检测 <code>Song</code> 实例和打印它的描述。</p>
<p> If the downcasting succeeds, the properties of <code>movie</code> are then used to print a description for that <code>Movie</code> instance, including the name of its <code>director</code>. A similar principle is used to check for <code>Song</code> instances, and to print an appropriate description (including <code>artist</code> name) whenever a <code>Song</code> is found in the library.</p>
<pre><code>注意
转换没有真的改变实例或它的值。潜在的根本的实例保持不变;只是简单地把它作为它被转换成的类来使用。
NOTE
Casting does not actually modify the instance or change its values. The underlying instance remains the same; it is simply treated and accessed as an instance of the type to which it has been cast.
</code></pre><h3 id="any-anyobject-type-casting-for-any-and-anyobject">Any和AnyObject的转换 Type Casting for Any and AnyObject</h3>
<p> Swift为不确定类型提供了两种特殊类型别名</p>
<ul>
<li><p><code>AnyObject</code>可以代表任何class类型的实例。</p>
</li>
<li><p><code>Any</code>可以表示任何类型除了方法类型function types</p>
<p>Swift provides two special type aliases for working with non-specific types:</p>
</li>
</ul>
<ul>
<li><code>AnyObject</code> can represent an instance of any class type.</li>
<li><p><code>Any</code> can represent an instance of any type at all, apart from function types.</p>
<pre><code>注意
只有当你明确的需要它的行为和功能时才使用Any和AnyObject。在你的代码里使用你期望的明确的类型总是更好的。
NOTE
Use Any and AnyObject only when you explicitly need the behavior and capabilities they provide. It is always better to be specific about the types you expect to work with in your code.
</code></pre></li>
</ul>
<h3 id="anyobject-">AnyObject类型</h3>
<p> 当需要在工作中使用Cocoa APIs它一般接收一个<code>AnyObject[]</code>类型的数组或者说“一个任何对象类型的数组”。这是因为OC没有明确的类型化数组。但是你常常可以确定包含在仅从你知道的API信息提供的这样一个数组中的对象的类型。</p>
<p> When working with Cocoa APIs, it is common to receive an array with a type of <code>AnyObject[]</code>, or “an array of values of any object type”. This is because Objective-C does not have explicitly typed arrays. However, you can often be confident about the type of objects contained in such an array just from the information you know about the API that provided the array.</p>
<p> 在这些情况下,你可以使用强制形式的类型检查(<code>as</code>)来下转在数组中的每一项到比 <code>AnyObject</code> 更明确的类型不需要可选解包optional unwrapping</p>
<p> In these situations, you can use the forced version of the type cast operator (<code>as</code>) to downcast each item in the array to a more specific class type than <code>AnyObject</code>, without the need for optional unwrapping.</p>
<p> 下面的示例定义了一个 <code>AnyObject[]</code> 类型的数组并填入三个<code>Movie</code>类型的实例:</p>
<p> The example below defines an array of type <code>AnyObject[]</code> and populates this array with three instances of the <code>Movie</code> class:</p>
<pre><code>let someObjects: AnyObject[] = [
Movie(name: &quot;2001: A Space Odyssey&quot;, director: &quot;Stanley Kubrick&quot;),
Movie(name: &quot;Moon&quot;, director: &quot;Duncan Jones&quot;),
Movie(name: &quot;Alien&quot;, director: &quot;Ridley Scott&quot;)
]
</code></pre><p> 因为知道这个数组只包含 <code>Movie</code> 实例,你可以直接用(<code>as</code>)下转并解包到不可选的<code>Movie</code>类型ps其实就是我们常用的正常类型这里是为了和可选类型相对比</p>
<p> Because this array is known to contain only <code>Movie</code> instances, you can downcast and unwrap directly to a non-optional <code>Movie</code> with the forced version of the type cast operator (<code>as</code>):</p>
<pre><code>for object in someObjects {
let movie = object as Movie
println(&quot;Movie: &#39;\(movie.name)&#39;, dir. \(movie.director)&quot;)
}
// Movie: &#39;2001: A Space Odyssey&#39;, dir. Stanley Kubrick
// Movie: &#39;Moon&#39;, dir. Duncan Jones
// Movie: &#39;Alien&#39;, dir. Ridley Scott
</code></pre><p> 为了变为一个更短的形式,下转<code>someObjects</code>类型成功 <code>Movie[]</code> 类型代替下转每一项。</p>
<p> For an even shorter form of this loop, downcast the <code>someObjects</code> array to a type of <code>Movie[]</code> instead of downcasting each item:</p>
<pre><code>for movie in someObjects as Movie[] {
println(&quot;Movie: &#39;\(movie.name)&#39;, dir. \(movie.director)&quot;)
}
// Movie: &#39;2001: A Space Odyssey&#39;, dir. Stanley Kubrick
// Movie: &#39;Moon&#39;, dir. Duncan Jones
// Movie: &#39;Alien&#39;, dir. Ridley Scott
</code></pre><h3 id="any-">Any类型</h3>
<p> 这里有个示例,使用 <code>Any</code> 类型来和混合的不同类型一起工作包括非class类型。它创建了一个可以存储<code>Any</code>类型的数组 <code>things</code></p>
<p> Heres an example of using <code>Any</code> to work with a mix of different types, including non-class types. The example creates an array called <code>things</code>, which can store values of type <code>Any</code>:</p>
<pre><code>var things = Any[]()
things.append(0)
things.append(0.0)
things.append(42)
things.append(3.14159)
things.append(&quot;hello&quot;)
things.append((3.0, 5.0))
things.append(Movie(name: &quot;Ghostbusters&quot;, director: &quot;Ivan Reitman&quot;))
</code></pre><p> <code>things</code> 数组包含两个 <code>Int</code>2个 <code>Double</code>1个 <code>String</code> 值,一个元组 <code>(Double, Double)</code> Ivan Reitman导演的电影“Ghostbusters”。</p>
<p> The <code>things</code> array contains two <code>Int</code> values, two <code>Double</code> values, a <code>String</code> value, a tuple of type <code>(Double, Double)</code>, and the movie “Ghostbusters”, directed by Ivan Reitman.</p>
<p> 你可以在 <code>switch</code> <code>cases</code>里用<code>is</code><code>as</code> 操作符来发觉只知道是 <code>Any</code><code>AnyObject</code>的常量或变量的类型。 下面的示例迭代 <code>things</code>数组中的每一项的并用<code>switch</code>语句查找每一项的类型。这几种<code>switch</code>语句的情形绑定它们匹配的值到一个规定类型的常量,让它们可以打印它们的值:</p>
<p> You can use the <code>is</code> and <code>as</code> operators in a <code>switch</code> statements cases to discover the specific type of a constant or variable that is known only to be of type <code>Any</code> or <code>AnyObject</code>. The example below iterates over the items in the <code>things</code> array and queries the type of each item with a <code>switch</code> statement. Several of the <code>switch</code> statements cases bind their matched value to a constant of the specified type to enable its value to be printed:</p>
<pre><code>for thing in things {
switch thing {
case 0 as Int:
println(&quot;zero as an Int&quot;)
case 0 as Double:
println(&quot;zero as a Double&quot;)
case let someInt as Int:
println(&quot;an integer value of \(someInt)&quot;)
case let someDouble as Double where someDouble &gt; 0:
println(&quot;a positive double value of \(someDouble)&quot;)
case is Double:
println(&quot;some other double value that I don&#39;t want to print&quot;)
case let someString as String:
println(&quot;a string value of \&quot;\(someString)\&quot;&quot;)
case let (x, y) as (Double, Double):
println(&quot;an (x, y) point at \(x), \(y)&quot;)
case let movie as Movie:
println(&quot;a movie called &#39;\(movie.name)&#39;, dir. \(movie.director)&quot;)
default:
println(&quot;something else&quot;)
}
}
// zero as an Int
// zero as a Double
// an integer value of 42
// a positive double value of 3.14159
// a string value of &quot;hello&quot;
// an (x, y) point at 3.0, 5.0
// a movie called &#39;Ghostbusters&#39;, dir. Ivan Reitman
</code></pre><p></p>
<pre><code>注意
在一个switch语句的case中使用强制形式的类型检查操作符(as, 而不是 as?) 来检查和转换到一个规定的类型。在switch case 语句的内容中这种检查总是安全的。
NOTE
The cases of a switch statement use the forced version of the type cast operator (as, not as?) to check and cast to a specific type. This check is always safe within the context of a switch case statement.
</code></pre>
</section>
</div>
</div>
</div>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.19" data-basepath=".." data-revision="1402128272564">
<div class="book" data-level="2.19" data-basepath=".." data-revision="1402148739560">
<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>
@ -576,7 +576,7 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_66">
<section class="normal" id="section-gitbook_27">
<h1 id="-">类型嵌套</h1>
<p>本页包含内容:</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.20" data-basepath=".." data-revision="1402128272564">
<div class="book" data-level="2.20" data-basepath=".." data-revision="1402148739560">
<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="1402128272564">
<div class="book" data-level="2.21" data-basepath=".." data-revision="1402148739560">
<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.22" data-basepath=".." data-revision="1402128272564">
<div class="book" data-level="2.22" data-basepath=".." data-revision="1402148739560">
<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>
@ -576,7 +576,7 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_69">
<section class="normal" id="section-gitbook_30">
<h1 id="-">泛型</h1>
<hr>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.23" data-basepath=".." data-revision="1402128272564">
<div class="book" data-level="2.23" data-basepath=".." data-revision="1402148739560">
<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="1402128272564">
<div class="book" data-level="2" data-basepath=".." data-revision="1402148739560">
<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>
@ -576,7 +576,7 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_73">
<section class="normal" id="section-gitbook_34">
<h1 id="swift-">Swift 教程</h1>
<p>本章介绍了 Swift 的各种特性及其使用方法,是全书的核心部分。</p>