update 02/03

This commit is contained in:
numbbbbb
2014-06-08 15:48:15 +08:00
parent d82107830d
commit a0ba0ada03
41 changed files with 129 additions and 115 deletions

View File

@ -46,7 +46,7 @@
<div class="book" data-level="1.1" data-basepath=".." data-revision="1402209202237">
<div class="book" data-level="1.1" data-basepath=".." data-revision="1402213634991">
<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_6">
<section class="normal" id="section-gitbook_4">
<h1 id="-swift">关于 Swift</h1>
<p>Swift 是一种新的编程语言,用于编写 iOS 和 OS X 应用程序。Swift 结合了 C 和 Objective-C 的优点并且不受C的兼容性的限制。Swift 使用安全的编程模式并添加了很多新特性这将使编程更简单扩展性更强也更有趣。除此之外Swift 还支持人见人爱的 Cocoa 和 Cocoa Touch 框架。拥有了这些特性Swift将重新定义软件开发。</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="1.2" data-basepath=".." data-revision="1402209202237">
<div class="book" data-level="1.2" data-basepath=".." data-revision="1402213634991">
<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_7">
<section class="normal" id="section-gitbook_5">
<h1 id="swift-">Swift 初见</h1>
<p>本页内容包括:</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="1" data-basepath=".." data-revision="1402209202237">
<div class="book" data-level="1" data-basepath=".." data-revision="1402213634991">
<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_8">
<section class="normal" id="section-gitbook_6">
<h1 id="-swift">欢迎使用 Swift</h1>
<p>在本章中您将了解 Swift 的特性和开发历史,并对 Swift 有一个初步的了解。</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.1" data-basepath=".." data-revision="1402209202237">
<div class="book" data-level="2.1" data-basepath=".." data-revision="1402213634991">
<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_10">
<section class="normal" id="section-gitbook_8">
<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="1402209202237">
<div class="book" data-level="2.2" data-basepath=".." data-revision="1402213634991">
<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_21">
<section class="normal" id="section-gitbook_30">
<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="1402209202237">
<div class="book" data-level="2.3" data-basepath=".." data-revision="1402213634991">
<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_11">
<section class="normal" id="section-gitbook_9">
<h1 id="-strings-and-characters-">字符串和字符 (Strings and Characters)</h1>
<p>本页包含内容:</p>
@ -604,82 +604,91 @@
<li>字符串大小写</li>
<li>Unicode</li>
</ul>
<p><strong>String</strong> 是一个有序的字符集合,例如 &quot;hello, world&quot;, &quot;albatross&quot;
Swift 字符串通过 <strong>String</strong> 类型来表示,也可以表示为 <strong>Character</strong> 类型值的集合。</p>
<hr>
<p><strong>String</strong> 是例如 &quot;hello, world&quot;, &quot;海贼王&quot; 这样的有序的 <strong>Character</strong> (字符) 类型值的集合,通过 <strong>String</strong> 类型来表示</p>
<p>Swift 的 <strong>String</strong><strong>Character</strong> 类型提供了一个快速的,兼容 Unicode 的方式来处理代码中的文本信息。
创建和操作字符串的语法与 C的操作方式相似,轻量并且易读。
创建和操作字符串的语法与 C 语言中字符串操作相似,轻量并且易读。
字符串连接操作只需要简单地通过 <code>+</code> 号将两个字符串相连即可。
与 Swift 中其他值一样,能否更改字符串的值,取决于其被定义为常量还是变量。</p>
<p>尽管语法简易,但 <strong>String</strong> 类型是一种快速、现代化的字符串实现。
每一个字符串都是由独立编码的 Unicode 字符组成,并提供了用于访问这些字符在不同Unicode表示的支持。</p>
<p><strong>String</strong> 也可以用于在常量、变量、字面量和表达式中进行字符串插值,这使得创建用于展示、存储和打印的字符串变得轻松自如</p>
每一个字符串都是由独立编码的 Unicode 字符组成,并提供了不同 Unicode 表示 (representations) 来访问这些字符的支持。</p>
<p>Swift可以在常量、变量、字面量和表达式中进行字符串插值操作,可以轻松创建用于展示、存储和打印的自定义字符串。</p>
<blockquote>
<p>注意:</p>
<p>Swift 的 <strong>String</strong> 类型与 Foundation NSString 类进行了无缝桥接。
如果您利用 Cocoa 或 Cocoa Touch 中的 Foundation 框架进行工作,整个 NSString API 都可以调用您创建的任意 String 类型的值,您额外还可以在任意 API 中使用本章介绍的 <strong>String</strong> 特性
您也可以在任意要求传入NSString 实例作为参数的 API 中使用 <strong>String</strong> 类型的值进行替换。</p>
如果您利用 Cocoa 或 Cocoa Touch 中的 Foundation 框架进行工作。
所有 <strong>NSString</strong> API 都可以调用您创建的任意 <strong>String</strong> 类型的值
除此之外,还可以使用本章介绍的 <strong>String</strong> 特性。
您也可以在任意要求传入 <strong>NSString</strong> 实例作为参数的 API 中使用 <strong>String</strong> 类型的值作为替代。</p>
<p>更多关于在 Foundation 和 Cocoa 中使用 <strong>String</strong> 的信息请查看 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/index.html#//apple_ref/doc/uid/TP40014216" target="_blank">Using Swift with Cocoa and Objective-C</a></p>
</blockquote>
<hr>
<h3 id="-">字符串字面量</h3>
<p>您可以在您的代码中包含一段预定义的字符串值作为字符串字面量。
字符串字面量是由双引号包裹着的具有固定顺序的文本字符集。</p>
字符串字面量是由双引号 (&quot;&quot;) 包裹着的具有固定顺序的文本字符集。</p>
<p>字符串字面量可以用于为常量和变量提供初始值。</p>
<pre><code>let someString = &quot;Some string literal value&quot;
</code></pre><blockquote>
<p>注意:</p>
<p><code>someString</code> 变量通过字符串字面量进行初始化Swift 因此推断<strong>String</strong> 类型。</p>
<p><code>someString</code> 变量通过字符串字面量进行初始化Swift 因此推断该变量<strong>String</strong> 类型。</p>
</blockquote>
<p>字符串字面量可以包含以下特殊字符:</p>
<ul>
<li>移特殊字符 <code>\0</code> (空字符)、<code>\\</code>(反斜线)、<code>\t</code> (水平制表符)、<code>\n</code> (换行符)、<code>\r</code> (回车符)、<code>\&quot;</code> (双引号)、<code>\&#39;</code> (单引号)。</li>
<li>单字节 Unicode 标量,写成 <code>\xnn</code>,其中 nn 为两位十六进制数。</li>
<li>双字节 Unicode 标量,写成 <code>\unnnn</code>,其中 nnnn 为四位十六进制数。</li>
<li>四字节 Unicode 标量,写成 <code>\Unnnnnnnn</code>,其中 nnnnnnnn 为八位十六进制数。</li>
<li>字符 <code>\0</code> (空字符)、<code>\\</code>(反斜线)、<code>\t</code> (水平制表符)、<code>\n</code> (换行符)、<code>\r</code> (回车符)、<code>\&quot;</code> (双引号)、<code>\&#39;</code> (单引号)。</li>
<li>单字节 Unicode 标量,写成 <code>\xnn</code>,其中 <code>nn</code> 为两位十六进制数。</li>
<li>双字节 Unicode 标量,写成 <code>\unnnn</code>,其中 <code>nnnn</code> 为四位十六进制数。</li>
<li>四字节 Unicode 标量,写成 <code>\Unnnnnnnn</code>,其中 <code>nnnnnnnn</code> 为八位十六进制数。</li>
</ul>
<p>下面的代码为各种特殊字符的使用示例。</p>
<p><code>wiseWords</code> 常量包含了两个转移特殊字符 (双括号)<code>dollarSign</code><code>blackHeart</code><code>sparklingHeart</code> 常量演示了三种不同格式的 Unicode 标量:</p>
<pre><code>let wiseWords = &quot;\&quot;Imagination is more important than knowledge\&quot; - Einstein&quot;
// &quot;Imagination is more important than knowledge&quot; - Einstein
let dollarSign = &quot;\x24&quot; // $, Unicode scalar U+0024
let blackHeart = &quot;\u2665&quot; // , Unicode scalar U+2665
let sparklingHeart = &quot;\U0001F496&quot; // 💖, Unicode scalar U+1F496
</code></pre><h3 id="-">初始化空字符串</h3>
<p>下面的代码为各种特殊字符的使用示例。
<code>wiseWords</code> 常量包含了两个转移特殊字符 (双括号)
<code>dollarSign</code><code>blackHeart</code><code>sparklingHeart</code> 常量演示了三种不同格式的 Unicode 标量:</p>
<pre><code>let wiseWords = &quot;\&quot;我是要成为海贼王的男人\&quot; - 路飞&quot;
// &quot;我是要成为海贼王的男人&quot; - 路飞
let dollarSign = &quot;\x24&quot; // $, Unicode 标量 U+0024
let blackHeart = &quot;\u2665&quot; // ♥, Unicode 标量 U+2665
let sparklingHeart = &quot;\U0001F496&quot; // 💖, Unicode 标量 U+1F496
</code></pre><hr>
<h3 id="-">初始化空字符串</h3>
<p>为了构造一个很长的字符串,可以创建一个空字符串作为初始值。
可以将空的字符串字面量赋值给变量,也可以初始化一个新的 <strong>String</strong> 实例:</p>
<pre><code>var emptyString = &quot;&quot; // empty string literal
var anotherEmptyString = String() // initializer syntax
// 两个字符串为空,并且两者等价
<pre><code>var emptyString = &quot;&quot; // 空字符串字面量
var anotherEmptyString = String() // 初始化 String 实例
// 两个字符串为空等价
</code></pre><p>您可以通过检查其 <strong>Boolean</strong> 类型的 <code>isEmpty</code> 属性来判断该字符串是否为空:</p>
<pre><code>if emptyString.isEmpty {
println(&quot;Nothing to see here&quot;)
println(&quot;什么都没有&quot;)
}
// 打印 &quot;Nothing to see here&quot;
</code></pre><h3 id="-">字符串可变性</h3>
// 输出 &quot;什么都没有&quot;
</code></pre><hr>
<h3 id="-">字符串可变性</h3>
<p>您可以通过将一个特定字符串分配给一个变量来对其进行修改,或者分配给一个常量来保证其不会被修改:</p>
<pre><code>var variableString = &quot;Horse&quot;
variableString += &quot; and carriage&quot;
// variableString 现在为 &quot;Horse and carriage&quot;
let constantString = &quot;Highlander&quot;
constantString += &quot; and another Highlander&quot;
// 这会报告一个编译错误(compile-time error) - 常量不可以被修改。
// 这会报告一个编译错误 (compile-time error) - 常量不可以被修改。
</code></pre><blockquote>
<p>注意:</p>
<p>在 Objective-C 和 Cocoa 中,您通过选择两个不同的类( NSString 和 NSMutableString )来指定该字符串是否可以被修改Swift中的字符串是否可以修改仅通过定义的是变量还是常量来决定实现了多种类型可变性操作的统一。</p>
<p>在 Objective-C 和 Cocoa 中,您通过选择两个不同的类( <code>NSString</code><code>NSMutableString</code> )来指定该字符串是否可以被修改Swift 中的字符串是否可以修改仅通过定义的是变量还是常量来决定,实现了多种类型可变性操作的统一。</p>
</blockquote>
<hr>
<h3 id="-">字符串是值类型</h3>
<p>Swift 的 <strong>String</strong> 类型是值类型。
如果您创建了一个新的字符串,那么当其进行常量、变量赋值操作或在函数/方法中传递时,会进行值拷贝。
任何情况下,都会对已有字符串值创建新副本,并对该新副本进行传递或赋值。
任何情况下,都会对已有字符串值创建新副本,并对该新副本进行传递或赋值操作
值类型在 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html#//apple_ref/doc/uid/TP40014097-CH13-XID_104" target="_blank">Structures and Enumerations Are Value Types</a> 中进行了说明。</p>
<blockquote>
<p>注意:</p>
<p>与 Cocoa 中的 NSString 不同,当您在 Cocoa 中创建了一个 NSString 实例,并将其传递给一个函数/方法,或者赋值给一个变量,您永远都是传递或赋值同一个 NSString 实例的一个引用,除非您特别要求进行值拷贝,否则字符串不会进行赋值新副本操作。</p>
<p>与 Cocoa 中的 NSString 不同,当您在 Cocoa 中创建了一个 NSString 实例,并将其传递给一个函数/方法,或者赋值给一个变量,您传递或赋值的是该 NSString 实例的一个引用,除非您特别要求进行值拷贝,否则字符串不会生成新的副本来进行赋值操作。</p>
</blockquote>
<p>Swift 默认字符串拷贝的方式保证了在函数/方法中传递的是字符串的值,其明确了无论该值来自于哪里,都是您独自拥有的
<p>Swift 默认字符串拷贝的方式保证了在函数/方法中传递的是字符串的值。
很明显无论该值来自于哪里,都是您独自拥有的。
您可以放心您传递的字符串本身不会被更改。</p>
<p>在实际编译时Swift编译器会优化字符串的使用使实际的复制只发生在绝对必要的情况下这意味着您始终可以将字符串作为值类型的同时获得极高的性能。</p>
<h4 id="-characters-">使用字符(Characters)</h4>
<p>Swift 的 <strong>String</strong> 类型表示特定序列的字符值的集合。
<p>在实际编译时Swift 编译器会优化字符串的使用,使实际的复制只发生在绝对必要的情况下,这意味着您将字符串作为值类型的同时可以获得极高的性能。</p>
<hr>
<h3 id="-characters-">使用字符(Characters)</h3>
<p>Swift 的 <strong>String</strong> 类型表示特定序列的 <strong>Character</strong> (字符) 类型值的集合。
每一个字符值代表一个 Unicode 字符。
您可利用 for-in 循环来遍历字符串中的每一个字符:</p>
<pre><code>for character in &quot;Dog!🐶&quot; {
@ -693,8 +702,9 @@ constantString += &quot; and another Highlander&quot;
</code></pre><p>for-in 循环在<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ControlFlow.html#//apple_ref/doc/uid/TP40014097-CH9-XID_154" target="_blank">For Loops</a>中进行了详细描述。</p>
<p>另外,通过标明一个 <strong>Character</strong> 类型注解并通过字符字面量进行赋值,可以建立一个独立的字符常量或变量:</p>
<pre><code>let yenSign: Character = &quot;¥&quot;
</code></pre><h3 id="-">计算字符数量</h3>
<p>通过调用全局 <code>countElements</code> 函数并将字符串作为参数进行传递可以获取该字符串的字符数量</p>
</code></pre><hr>
<h3 id="-">计算字符数量</h3>
<p>通过调用全局 <code>countElements</code> 函数,并将字符串作为参数进行传递,可以获取该字符串的字符数量。</p>
<pre><code>let unusualMenagerie = &quot;Koala 🐨, Snail 🐌, Penguin 🐧, Dromedary 🐪&quot;
println(&quot;unusualMenagerie has \(countElements(unusualMenagerie)) characters&quot;)
// prints &quot;unusualMenagerie has 40 characters&quot;
@ -708,8 +718,9 @@ println(&quot;unusualMenagerie has \(countElements(unusualMenagerie)) characters
NSString 的 <code>length</code> 属性是基于利用 UTF-16 表示的十六位代码单元数字,而不是基于 Unicode 字符。
为了解决这个问题NSString 的 <code>length</code> 属性在被 Swift的 <strong>String</strong> 访问时会成为 <code>utf16count</code></p>
</blockquote>
<hr>
<h3 id="-">连接字符串和字符</h3>
<p>字符串和字符的值可以通过加法运算符 (+) 相加在一起并创建一个新的字符串值:</p>
<p>字符串和字符的值可以通过加法运算符 (<code>+</code>) 相加在一起并创建一个新的字符串值:</p>
<pre><code>let string1 = &quot;hello&quot;
let string2 = &quot; there&quot;
let character1: Character = &quot;!&quot;
@ -731,12 +742,13 @@ welcome += character1
<p>注意:</p>
<p>您不能将一个字符串或者字符添加到一个已经存在的字符变量上,因为字符变量只能包含一个字符。</p>
</blockquote>
<hr>
<h3 id="-">字符串插值</h3>
<p>字符串插值是一种全新的构建字符串的方式,可以在其中包含常量、变量、字面量和表达式。
<p>字符串插值是一种构建字符串的方式,可以在其中包含常量、变量、字面量和表达式。
您插入的字符串字面量的每一项都被包裹在以反斜线为前缀的圆括号中:</p>
<pre><code>let multiplier = 3
let message = &quot;\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)&quot;
// message is &quot;3 times 2.5 is 7.5&quot;
let message = &quot;\(multiplier) 乘以 2.5 \(Double(multiplier) * 2.5)&quot;
// message is &quot;3 乘以 2.5 7.5&quot;
</code></pre><p>在上面的例子中,<code>multiplier</code> 作为 <code>\(multiplier)</code> 被插入到一个字符串字面量中。
当创建字符串执行插值计算时此占位符会被替换为 <code>multiplier</code> 实际的值。</p>
<p><code>multiplier</code> 的值也作为字符串中后面表达式的一部分。
@ -744,18 +756,19 @@ let message = &quot;\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)&quot;
在这个例子中,表达式写为 <code>\(Double(multiplier) * 2.5)</code> 并包含在字符串字面量中。</p>
<blockquote>
<p>注意:</p>
<p>您插值字符串中写在括号中的表达式不能包含非转义双引号 (&quot;) 和反斜杠 (\),并且不能包含回车或换行符。</p>
<p>您插值字符串中写在括号中的表达式不能包含非转义双引号 (<code>&quot;</code>) 和反斜杠 (<code>\</code>),并且不能包含回车或换行符。</p>
</blockquote>
<hr>
<h3 id="-">比较字符串</h3>
<p>Swift 提供了三种方式来比较字符串的值:字符串相等,前缀相等和后缀相等。</p>
<h5 id="-">字符串相等</h5>
<p>如果两个字符串以同一顺序包含完全相同的字符,则认为两者字符串相等:</p>
<pre><code>let quotation = &quot;We&#39;re a lot alike, you and I.&quot;
let sameQuotation = &quot;We&#39;re a lot alike, you and I.&quot;
<pre><code>let quotation = &quot;我们是一样一样滴.&quot;
let sameQuotation = &quot;我们是一样一样滴.&quot;
if quotation == sameQuotation {
println(&quot;These two strings are considered equal&quot;)
println(&quot;这两个字符串被认为是相同的&quot;)
}
// prints &quot;These two strings are considered equal&quot;
// prints &quot;这两个字符串被认为是相同的&quot;
</code></pre><h5 id="-">前缀/后缀相等</h5>
<p>通过调用字符串的 <code>hasPrefix</code>/<code>hasSuffix</code> 方法来检查字符串是否拥有特定前缀/后缀。
两个方法均需要以字符串作为参数传入并传出 <strong>Boolean</strong> 值。
@ -784,16 +797,17 @@ for scene in romeoAndJuliet {
println(&quot;There are \(act1SceneCount) scenes in Act 1&quot;)
// prints &quot;There are 5 scenes in Act 1&quot;
</code></pre><h5 id="-">大写和小写字符串</h5>
<p>您可以通过字符串的 <code>uppercaseString</code><code>lowercaseString</code> 属性来访问一个字符串的大写/小写版本。</p>
<p>您可以通过字符串的 <code>uppercaseString</code><code>lowercaseString</code> 属性来访问大写/小写版本的字符串</p>
<pre><code>let normal = &quot;Could you help me, please?&quot;
let shouty = normal.uppercaseString
// shouty 值为 &quot;COULD YOU HELP ME, PLEASE?&quot;
let whispered = normal.lowercaseString
// whispered 值为 &quot;could you help me, please?&quot;
</code></pre><h3 id="unicode">Unicode</h3>
<p>Unicode 是文本编码和表示的国际标准。
</code></pre><hr>
<h3 id="unicode">Unicode</h3>
<p>Unicode 是一个国际标准,用于文本的编码和表示。
它使您可以用标准格式表示来自任意语言几乎所有的字符,并能够对文本文件或网页这样的外部资源中的字符进行读写操作。</p>
<p>Swift 的字符串和字符类型是完全兼容 Unicode 的,它支持如下所述的一系列不同的 Unicode 编码。</p>
<p>Swift 的字符串和字符类型是完全兼容 Unicode 标准的,它支持如下所述的一系列不同的 Unicode 编码。</p>
<h6 id="unicode-terminology-">Unicode 术语(Terminology)</h6>
<p>Unicode 中每一个字符都可以被解释为一个或多个 unicode 标量。
字符的 unicode 标量是一个唯一的21位数字(和名称),例如 <code>U+0061</code> 表示小写的拉丁字母A (&quot;a&quot;)<code>U+1F425</code> 表示小幺鸡表情 (&quot;🐥&quot;)</p>
@ -808,18 +822,18 @@ let whispered = normal.lowercaseString
<li>UTF-16 代码单元集合 (利用字符串的 <code>utf16</code> 属性进行访问)</li>
<li>21位的 Unicode 标量值集合 (利用字符串的 <code>unicodeScalars</code> 属性进行访问)</li>
</ul>
<p>下面由 <code>D</code> <code>o</code> <code>g</code> <code>!</code><code>🐶</code> (狗脸表情Unicode 标量为 <code>U+1F436</code>)组成的字符串中的每一个字符代表着一种不同的表示:</p>
<p>下面由 <code>D</code> <code>o</code> <code>g</code> <code>!</code><code>🐶</code> (<code>DOG FACE</code>Unicode 标量为 <code>U+1F436</code>)组成的字符串中的每一个字符代表着一种不同的表示:</p>
<pre><code>let dogString = &quot;Dog!🐶&quot;
</code></pre><h5 id="utf-8">UTF-8</h5>
<p>您可以通过遍历字符串的 <code>utf8</code> 属性来访问它的 <code>UTF-8</code> 表示。
其为 <strong>UTF8View</strong> 类型的属性,<strong>UTF8View</strong> 是无符号8位 (<code>UInt8</code>) 值的集合,每一个 <code>UIn8</code> 都是一个字符的 UTF-8 表示:</p>
其为 <strong>UTF8View</strong> 类型的属性,<strong>UTF8View</strong> 是无符号8位 (<code>UInt8</code>) 值的集合,每一个 <code>UInt8</code> 都是一个字符的 UTF-8 表示:</p>
<pre><code>for codeUnit in dogString.utf8 {
print(&quot;\(codeUnit) &quot;)
}
print(&quot;\n&quot;)
// 68 111 103 33 240 159 144 182
</code></pre><p>上面的例子中前四个10进制代码单元值 (68, 111, 103, 33) 代表了字符 <code>D</code> <code>o</code> <code>g</code><code>!</code> ,他们的 UTF-8 表示与 ASCII 表示相同。
后四个代码单元值 (240, 159, 144, 182) 是 <code>狗脸表情</code> 的4位 UTF-8 表示。</p>
后四个代码单元值 (240, 159, 144, 182) 是 <code>DOG FACE</code> 的4位 UTF-8 表示。</p>
<h5 id="utf-16">UTF-16</h5>
<p>您可以通过遍历字符串的 <code>utf16</code> 属性来访问它的 <code>UTF-16</code> 表示。
其为 <strong>UTF16View</strong> 类型的属性,<strong>UTF16View</strong> 是无符号16位 (<code>UInt16</code>) 值的集合,每一个 <code>UInt16</code> 都是一个字符的 UTF-16 表示:</p>
@ -829,7 +843,7 @@ print(&quot;\n&quot;)
print(&quot;\n&quot;)
// 68 111 103 33 55357 56374
</code></pre><p>同样,前四个代码单元值 (68, 111, 103, 33) 代表了字符 <code>D</code> <code>o</code> <code>g</code><code>!</code> ,他们的 UTF-16 代码单元和 UTF-8 完全相同。</p>
<p>第五和第六个代码单元值 (55357 and 56374) 是 <code>狗脸表情</code> 字符的UTF-16 表示。
<p>第五和第六个代码单元值 (55357 and 56374) 是 <code>DOG FACE</code> 字符的UTF-16 表示。
第一个值为 <code>U+D83D</code> (十进制值为 55357),第二个值为 <code>U+DC36</code> (十进制值为 56374)。</p>
<h5 id="unicode-scalars-">Unicode 标量 (Scalars)</h5>
<p>您可以通过遍历字符串的 <code>unicodeScalars</code> 属性来访问它的 Unicode 标量表示。
@ -843,7 +857,7 @@ print(&quot;\n&quot;)
// 68 111 103 33 128054
</code></pre><p>同样,前四个代码单元值 (68, 111, 103, 33) 代表了字符 <code>D</code> <code>o</code> <code>g</code><code>!</code>
第五位数值128054是一个十六进制1F436的十进制表示。
其等同于 <code>狗脸表情</code> 的Unicode 标量 U+1F436。</p>
其等同于 <code>DOG FACE</code> 的Unicode 标量 U+1F436。</p>
<p>作为查询字符值属性的一种替代方法,每个 <code>UnicodeScalar</code> 值也可以用来构建一个新的字符串值,比如在字符串插值中使用:</p>
<pre><code>for scalar in dogString.unicodeScalars {
println(&quot;\(scalar) &quot;)

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.4" data-basepath=".." data-revision="1402209202237">
<div class="book" data-level="2.4" data-basepath=".." data-revision="1402213634991">
<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="1402209202237">
<div class="book" data-level="2.5" data-basepath=".." data-revision="1402213634991">
<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="1402209202237">
<div class="book" data-level="2.6" data-basepath=".." data-revision="1402213634991">
<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_14">
<section class="normal" id="section-gitbook_23">
<h1 id="-functions-">函数Functions</h1>
<p>本页包含内容:</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.7" data-basepath=".." data-revision="1402209202237">
<div class="book" data-level="2.7" data-basepath=".." data-revision="1402213634991">
<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_15">
<section class="normal" id="section-gitbook_24">
<h1 id="-">闭包</h1>
<p>本页内容包含:</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.8" data-basepath=".." data-revision="1402209202237">
<div class="book" data-level="2.8" data-basepath=".." data-revision="1402213634991">
<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="1402209202237">
<div class="book" data-level="2.9" data-basepath=".." data-revision="1402213634991">
<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_17">
<section class="normal" id="section-gitbook_26">
<h3 id="-">类和结构体</h3>
<p>本页包含内容:</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.10" data-basepath=".." data-revision="1402209202237">
<div class="book" data-level="2.10" data-basepath=".." data-revision="1402213634991">
<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="1402209202237">
<div class="book" data-level="2.11" data-basepath=".." data-revision="1402213634991">
<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="1402209202237">
<div class="book" data-level="2.12" data-basepath=".." data-revision="1402213634991">
<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="1402209202237">
<div class="book" data-level="2.13" data-basepath=".." data-revision="1402213634991">
<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="1402209202237">
<div class="book" data-level="2.14" data-basepath=".." data-revision="1402213634991">
<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="1402209202237">
<div class="book" data-level="2.15" data-basepath=".." data-revision="1402213634991">
<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_23">
<section class="normal" id="section-gitbook_32">
<h1 id="-">析构过程</h1>
<p>在一个类的实例被释放之前析构函数被立即调用。用关键字deinit来标示析构函数类似于初始化函数用init来标示。析构函数只适用于类类型。</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.16" data-basepath=".." data-revision="1402209202237">
<div class="book" data-level="2.16" data-basepath=".." data-revision="1402213634991">
<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_24">
<section class="normal" id="section-gitbook_33">
<h1 id="-">自动引用计数</h1>
<p>本页包含内容:</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.17" data-basepath=".." data-revision="1402209202237">
<div class="book" data-level="2.17" data-basepath=".." data-revision="1402213634991">
<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_25">
<section class="normal" id="section-gitbook_34">
<h1 id="optional-chaining">Optional Chaining</h1>
<p>可选链Optional Chaining是一种可以请求和调用属性、方法及子脚本的过程它的自判断性体现于请求或调用的目标当前可能为空<code>nil</code>)。如果自判断的目标有值,那么调用就会成功;相反,如果选择的目标为空(<code>nil</code>),则这种调用将返回空(<code>nil</code>)。多次请求或调用可以被链接在一起形成一个链,如果任何一个节点为空(<code>nil</code>)将导致整个链失效。</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.18" data-basepath=".." data-revision="1402209202237">
<div class="book" data-level="2.18" data-basepath=".." data-revision="1402213634991">
<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_26">
<section class="normal" id="section-gitbook_35">
<h1 id="-type-casting-">类型检查Type Casting</h1>
<p>ps为了方便各位检验所以保留了英文可删。

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.19" data-basepath=".." data-revision="1402209202237">
<div class="book" data-level="2.19" data-basepath=".." data-revision="1402213634991">
<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_27">
<section class="normal" id="section-gitbook_36">
<h1 id="-">类型嵌套</h1>
<p>本页包含内容:</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.20" data-basepath=".." data-revision="1402209202237">
<div class="book" data-level="2.20" data-basepath=".." data-revision="1402213634991">
<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_28">
<section class="normal" id="section-gitbook_37">
<h1 id="-extensions-">扩展Extensions</h1>
<hr>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.21" data-basepath=".." data-revision="1402209202237">
<div class="book" data-level="2.21" data-basepath=".." data-revision="1402213634991">
<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="1402209202237">
<div class="book" data-level="2.22" data-basepath=".." data-revision="1402213634991">
<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_31">
<section class="normal" id="section-gitbook_40">
<h1 id="-">泛型</h1>
<hr>

View File

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

View File

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

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3.2" data-basepath=".." data-revision="1402209202237">
<div class="book" data-level="3.2" data-basepath=".." data-revision="1402213634991">
<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.3" data-basepath=".." data-revision="1402209202237">
<div class="book" data-level="3.3" data-basepath=".." data-revision="1402213634991">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -44,7 +44,7 @@
<div class="book" data-level="0" data-basepath="." data-revision="1402209202237">
<div class="book" data-level="0" data-basepath="." data-revision="1402213634991">
<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_5">
<section class="normal" id="section-gitbook_3">
<h1 id="swift-">Swift 编程语言</h1>
<p>Swift 是苹果在 WWDC 2014 上发布的一款全新的编程语言,本书译自苹果官方的 Swift 教程《The Swift Programming Language》。</p>

View File

@ -1,5 +1,5 @@
CACHE MANIFEST
# Revision 1402209202238
# Revision 1402213634992
CACHE:
index.html

File diff suppressed because one or more lines are too long