update 02/10

This commit is contained in:
numbbbbb
2014-06-09 17:19:57 +08:00
parent 8fafa19fe2
commit ca2b5892e7
41 changed files with 376 additions and 66 deletions

View File

@ -46,7 +46,7 @@
<div class="book" data-level="1.1" data-basepath=".." data-revision="1402300498179">
<div class="book" data-level="1.1" data-basepath=".." data-revision="1402305571512">
<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_118">
<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="1402300498179">
<div class="book" data-level="1.2" data-basepath=".." data-revision="1402305571512">
<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_119">
<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="1402300498179">
<div class="book" data-level="1" data-basepath=".." data-revision="1402305571512">
<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_120">
<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="1402300498179">
<div class="book" data-level="2.1" data-basepath=".." data-revision="1402305571512">
<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_122">
<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="1402300498179">
<div class="book" data-level="2.2" data-basepath=".." data-revision="1402305571512">
<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_144">
<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="1402300498179">
<div class="book" data-level="2.3" data-basepath=".." data-revision="1402305571512">
<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_123">
<section class="normal" id="section-gitbook_9">
<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="1402300498179">
<div class="book" data-level="2.4" data-basepath=".." data-revision="1402305571512">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
@ -587,6 +587,13 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_21">
<h1 id="-collection-types-">集合类型 (Collection Types)</h1>
</section>
</div>
</div>
</div>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.5" data-basepath=".." data-revision="1402300498179">
<div class="book" data-level="2.5" data-basepath=".." data-revision="1402305571512">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
@ -587,6 +587,14 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_22">
<h1 id="-">控制流</h1>
<p>Swift提供了类似C语言的流程控制结构包括将任务执行多次的<code>for</code><code>while</code>循环,基于特定条件选择执行不同分支的<code>if</code><code>switch</code>语句,还有控制流程跳转到其他代码的<code>break</code><code>continue</code>语句。</p>
</section>
</div>
</div>
</div>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.6" data-basepath=".." data-revision="1402300498179">
<div class="book" data-level="2.6" data-basepath=".." data-revision="1402305571512">
<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_137">
<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="1402300498179">
<div class="book" data-level="2.7" data-basepath=".." data-revision="1402305571512">
<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_138">
<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="1402300498179">
<div class="book" data-level="2.8" data-basepath=".." data-revision="1402305571512">
<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_139">
<section class="normal" id="section-gitbook_25">
<h1 id="-">枚举</h1>
<p>本页内容包含:</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.9" data-basepath=".." data-revision="1402300498179">
<div class="book" data-level="2.9" data-basepath=".." data-revision="1402305571512">
<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_140">
<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="1402300498179">
<div class="book" data-level="2.10" data-basepath=".." data-revision="1402305571512">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
@ -587,6 +587,301 @@
<div class="page-inner">
<section class="normal" id="section-gitbook_27">
<h1 id="-properties-">属性 (Properties)</h1>
<p><strong>属性</strong>将值跟特定的类、结构或枚举关联。一种是存储属性,把常量或变量的值作为实例的一部分,一种是计算属性,它计算一个值。计算属性可以用于类、结构和枚举里,存储属性只能用于类和结构。</p>
<p>存储属性和计算属性通常用于特定类型的实例,但是,属性也可以直接用于类型本身,这种属性称为类属性。</p>
<p>另外,还可以定义属性监视器来监控属性值的变化,以此来触发一个自定义的操作。属性监视器可以添加到自己写的存储属性上,也可以添加到从父类继承的属性上。</p>
<h2 id="-">存储属性</h2>
<p>简单来说,一个存储属性就是一个特定类型实例里表示常量或变量的部分,存储属性可以是<em>变量存储属性</em>(用关键字<code>var</code>定义),也可以是<em>常量存储属性</em>(用关键字<code>let</code>定义)。</p>
<p>可以在定义存储属性的时候指定默认值,详见<a href="#">默认属性值</a>一节。也可以在初始化阶段设置或修改存储属性的值,甚至修改常量存储属性的值,详见<a href="#">在初始化阶段修改常量存储属性</a>一节。</p>
<p>下面的例子定义了一个名为<code>FixedLengthRange</code>的结构体,表示一个在创建后无法修改整数范围的类型:</p>
<pre><code>struct FixedLengthRange {
var firstValue: Int
let length: Int
}
var rangeOfThreeItems = FixedLengthRange(firstValue: 0, length: 3)
// the range represents integer values 0, 1, and 2
rangeOfThreeItems.firstValue = 6
// the range now represents integer values 6, 7, and 8
</code></pre><p><code>FixedLengthRange</code>的实例包含一个名为<code>firstValue</code>的变量存储属性和一个名为<code>length</code>的常量存储属性。在上面的例子中,<code>length</code>在创建实例的时候被赋值,因为它是一个常量存储属性,所以再无法修改它的值。</p>
<h3 id="-">常量和存储属性</h3>
<p>如果创建了一个结构体的实例并赋值给一个常量,则无法修改实例的任何属性,即使定义了变量存储属性:</p>
<pre><code>let rangeOfFourItems = FixedLengthRange(firstValue: 0, length: 4)
// this range represents integer values 0, 1, 2, and 3
rangeOfFourItems.firstValue = 6
// this will report an error, even thought firstValue is a variable property
</code></pre><p>因为<code>rangeOfFourItems</code>声明成了常量(用<code>let</code>关键字),即使<code>firstValue</code>是一个变量属性,也无法再修改属性它的值。</p>
<p>这种行为是由于结构体struct属于<em>值类型</em>。当值类型的实例被声明为常量的时候,它的所有属性也就成了常量。</p>
<p>属于<em>引用类型</em>的类class则不一样把一个引用类型的实例赋给一个常量后仍然可以修改实例的变量属性。</p>
<h3 id="-">延迟存储属性</h3>
<p>延迟存储属性是指当第一次被调用的时候才有初始值的属性。在属性声明前使用<code>@lazy</code>特性来表示一个延迟存储属性。</p>
<blockquote>
<p>注意</p>
<p>必须将延迟存储属性声明成变量(使用<code>var</code>关键字),因为可能在实例构造完成之前属性的值无法得到。常量属性在构造过程完成之前必须要有初始值,因此无法声明成延迟属性。</p>
</blockquote>
<p>延迟属性很有用,当属性的值依赖于在实例的构造过程结束前无法知道具体值的外部因素时,或者当属性的值需要复杂或大量计算时,可以只在需要的时候来计算它。</p>
<p>下面复合类的例子使用了延迟存储属性来避免不必要的初始化。例子中定义了<code>DataImporter</code><code>DataManager</code>两个类,下面是部分代码:</p>
<pre><code>class DataImporter {
/*
DataImporter is a class to import data from an external file.
The class is assumed to take a non-trivial amount of time to initialize.
*/
var fileName = &quot;data.txt&quot;
// the DataImporter class would provide data importing functionality here
}
class DataManager {
@lazy var importer = DataImporter()
var data = String[]()
// the DataManager class would provide data management functionality here
}
let manager = DataManager()
manager.data += &quot;Some data&quot;
manager.data += &quot;Some more data&quot;
// the DataImporter instance for the importer property has not yet been created
</code></pre><p><code>DataManager</code>类包含一个名为<code>data</code>的存储属性,初始值是一个空的字符串(<code>String</code>)数组。虽然没有写出全部代码,<code>DataManager</code>类的目的是管理和提供对这个字符串数组的访问。</p>
<p><code>DataManager</code>的一个功能是从文件导入数据,该功能由<code>DataImporter</code>类提供,它需要一定的时间来处理。因为它需要在实例化之后打开文件、读取文件内容到内存。</p>
<p><code>DataManager</code>也可以不从文件中导入数据,所以当<code>DataManager</code>的实例被创建时,就没有必要创建一个<code>DataImporter</code>的实例。同时,更有意义的是当用到<code>DataImporter</code>的时候才去创建它。</p>
<p>由于使用了<code>@lazy</code>特性Attribute<code>importer</code>属性只有在第一次被访问的时候才被创建。比如访问它的属性<code>fileName</code>时:</p>
<pre><code>println(manager.importer.fileName)
// the DataImporter instance for the importer property has now been created
// prints &quot;data.txt”
</code></pre><h3 id="-">存储属性和实例变量</h3>
<p>如果您有过Objective-C经验应该知道有2种方式在类实例存储值和引用。对于属性来说也可以使用实例变量作为属性值的后端存储。</p>
<p>Swift编程语言中把这些理论统一用属性来实现。Swift中的属性没有对应的实例变量属性的后端存储也无法直接访问。这就避免了不同场景下访问方式的困扰同时也将属性的定义简化成一个语句。
一个类型中属性的全部信息——包括命名、类型和内存管理特征——都在唯一一个地方定义。</p>
<h2 id="-">计算属性</h2>
<p>除存储属性外,类、结构体和枚举可以定义<em>计算属性</em>计算属性不直接存储值而是提供一个getter来获取值一个可选的setter来间接设置其他属性或变量的值。</p>
<pre><code>struct Point {
var x = 0.0, y = 0.0
}
struct Size {
var width = 0.0, height = 0.0
}
struct Rect {
var origin = Point()
var size = Size()
var center: Point {
get {
let centerX = origin.x + (size.width / 2)
let centerY = origin.y + (size.height / 2)
return Point(x: centerX, y: centerY)
}
set(newCenter) {
origin.x = newCenter.x - (size.width / 2)
origin.y = newCenter.y - (size.height / 2)
}
}
}
var square = Rect(origin: Point(x: 0.0, y: 0.0),
size: Size(width: 10.0, height: 10.0))
let initialSquareCenter = square.center
square.center = Point(x: 15.0, y: 15.0)
println(&quot;square.origin is now at (\(square.origin.x), \(square.origin.y))&quot;)
// prints &quot;square.origin is now at (10.0, 10.0)”
</code></pre><p>这个例子定义了3个几何形状的结构体</p>
<ul>
<li><code>Point</code>封装了一个<code>(x, y)</code>的坐标</li>
<li><code>Size</code>封装了一个<code>width</code><code>height</code></li>
<li><code>Rect</code>表示一个有原点和尺寸的矩形</li>
</ul>
<p><code>Rect</code>也提供了一个名为<code>center</code>的计算属性。一个矩形的中心点可以从原点和尺寸来算出,所以不需要将它以显式声明的<code>Point</code>来保存。<code>Rect</code>的计算属性<code>center</code>提供了自定义的getter和setter来获取和设置矩形的中心点就像它有一个存储属性一样。</p>
<p>例子中接下来创建了一个名为<code>square</code><code>Rect</code>实例,初始值原点是<code>(0, 0)</code>,宽度高度都是<code>10</code>。如图所示蓝色正方形。</p>
<p><code>square</code><code>center</code>属性可以通过点运算符(<code>square.center</code>来访问这会调用getter来获取属性的值。跟直接返回已经存在的值不同getter实际上通过计算然后返回一个新的<code>Point</code>实例表示<code>square</code>的中心点。如代码所示,它正确返回了中心点<code>(5, 5)</code></p>
<p><code>center</code>属性之后被设置了一个新的值<code>(15, 15)</code>,表示向右上方移动正方形到如图所示橙色正方形的位置。设置属性<code>center</code>的值会调用setter来修改属性<code>origin</code><code>x</code><code>y</code>的值,从而实现移动正方形到新的位置。</p>
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/computedProperties_2x.png" alt="Computed Properties sample" width="388" height="387" /></p>
<h3 id="-setter-">便捷Setter声明</h3>
<p>如果计算属性的setter没有定义表示新值的参数名则可以使用默认名称<code>newValue</code>。下面是使用了便捷Setter声明的<code>Rect</code>结构体代码:</p>
<pre><code>struct AlternativeRect {
var origin = Point()
var size = Size()
var center: Point {
get {
let centerX = origin.x + (size.width / 2)
let centerY = origin.y + (size.height / 2)
return Point(x: centerX, y: centerY)
}
set {
origin.x = newValue.x - (size.width / 2)
origin.y = newValue.y - (size.height / 2)
}
}
}
</code></pre><h3 id="-">只读计算属性</h3>
<p>只有getter没有setter的计算属性就是<em>只读计算属性</em>。只读计算属性总是返回一个值,可以通过点运算符访问,但不能设置新的值。</p>
<blockquote>
<p>注意</p>
<p>必须使用<code>var</code>关键字定义计算属性,包括只读计算属性,因为他们的值不是固定的。<code>let</code>关键字只用来声明常量属性,表示初始化后再也无法修改的值。</p>
</blockquote>
<p>只读计算属性的声明可以去掉<code>get</code>关键字和花括号:</p>
<pre><code>struct Cuboid {
var width = 0.0, height = 0.0, depth = 0.0
var volume: Double {
return width * height * depth
}
}
let fourByFiveByTwo = Cuboid(width: 4.0, height: 5.0, depth: 2.0)
println(&quot;the volume of fourByFiveByTwo is \(fourByFiveByTwo.volume)&quot;)
// prints &quot;the volume of fourByFiveByTwo is 40.0&quot;
</code></pre><p>这个例子定义了一个名为<code>Cuboid</code>的结构体,表示三维空间的立方体,包含<code>width</code><code>height</code><code>depth</code>属性,还有一个名为<code>volume</code>的只读计算属性用来返回立方体的体积,设置<code>volume</code>的值毫无意义,因为无法确定<code>width</code><code>height</code><code>depth</code>的值。</p>
<p>尽管如此,<code>Cuboid</code>提供一个只读计算属性来让外部用户直接获取体积是很有用的。</p>
<h2 id="-">属性监视器</h2>
<p><em>属性监视器</em>监控和响应属性值的变化,每次属性被设置值的时候都会调用属性监视器,甚至新的值和现在的值相同的时候也不例外。</p>
<p>可以为除了延迟存储属性之外的其他存储属性添加属性监视器,也可以通过重载属性的方式为继承的属性(包括存储属性和计算属性)添加属性监视器。属性重载详见<a href="#">重载</a>一节。</p>
<blockquote>
<p>注意</p>
<p>不需要为无法重载的计算属性添加属性监视器因为可以通过setter直接监控和响应值的变化。</p>
</blockquote>
<p>可以为属性添加如下的一个或全部监视器:</p>
<ul>
<li><code>willSet</code>在设置新的值之前调用</li>
<li><code>didSet</code>在新的值被设置之后立即调用</li>
</ul>
<p><code>willSet</code>监视器会将新的属性值作为固定参数传入,在<code>willSet</code>的实现代码中可以为这个参数指定一个名称,如果不指定则参数仍然可用,这时使用默认名称<code>newValue</code>表示。</p>
<p>类似地,<code>didSet</code>监视器会将旧的属性值作为参数传入,可以为该参数命名或者使用默认参数名<code>oldValue</code></p>
<blockquote>
<p>注意</p>
<p><code>willSet</code><code>didSet</code>监视器在属性初始化过程中不会被调用,他们只会当属性的值在初始化之外的地方被设置时被调用。</p>
</blockquote>
<p>这里是一个<code>willSet</code><code>didSet</code>的实际例子,其中定义了一个名为<code>StepCounter</code>的类,用来统计当人步行时的总步数,可以跟计步器或其他日常锻炼的统计装置的输入数据配合使用。</p>
<pre><code>class StepCounter {
var totalSteps: Int = 0 {
willSet(newTotalSteps) {
println(&quot;About to set totalSteps to \(newTotalSteps)&quot;)
}
didSet {
if totalSteps &gt; oldValue {
println(&quot;Added \(totalSteps - oldValue) steps&quot;)
}
}
}
}
let stepCounter = StepCounter()
stepCounter.totalSteps = 200
// About to set totalSteps to 200
// Added 200 steps
stepCounter.totalSteps = 360
// About to set totalSteps to 360
// Added 160 steps
stepCounter.totalSteps = 896
// About to set totalSteps to 896
// Added 536 steps
</code></pre><p><code>StepCounter</code>类定义了一个<code>Int</code>类型的属性<code>totalSteps</code>,它是一个存储属性,包含<code>willSet</code><code>didSet</code>监视器。</p>
<p><code>totalSteps</code>设置新值的时候,它的<code>willSet</code><code>didSet</code>监视器都会被调用,甚至当新的值和现在的值完全相同也会调用。</p>
<p>例子中的<code>willSet</code>监视器将表示新值的参数自定义为<code>newTotalSteps</code>,这个监视器只是简单的将新的值输出。</p>
<p><code>didSet</code>监视器在<code>totalSteps</code>的值改变后被调用,它把新的值和旧的值进行对比,如果总的步数增加了,就输出一个消息表示增加了多少步。<code>didSet</code>没有提供自定义名称,所以默认值<code>oldValue</code>表示旧值的参数名。</p>
<blockquote>
<p>注意</p>
<p>如果在<code>didSet</code>监视器里为属性赋值,这个值会替换监视器之前设置的值。</p>
</blockquote>
<h2 id="-">全局变量和局部变量</h2>
<p>计算属性和属性监视器所描述的模式也可以用于全局变量和局部变量,全局变量是在函数、方法、闭包或任何类型之外定义的变量,局部变量是在函数、方法或闭包内部定义的变量。</p>
<p>前面章节提到的全局或局部变量都属于存储型变量,跟存储属性类似,它提供特定类型的存储空间,并允许读取和写入。</p>
<p>另外,在全局或局部范围都可以定义计算型变量和为存储型变量定义监视器,计算型变量跟计算属性一样,返回一个计算的值而不是存储值,声明格式也完全一样。</p>
<blockquote>
<p>注意</p>
<p>全局的常量或变量都是延迟计算的,跟<a href="#">延迟存储属性</a>相似,不同的地方在于,全局的常量或变量不需要标记<code>@lazy</code>特性。</p>
<p>局部范围的常量或变量不会延迟计算。</p>
</blockquote>
<h2 id="-">类属性</h2>
<p>实例的属性属于一个特定类型实例,每次类型实例化后都拥有自己的一套属性值,实例之间的属性相互独立。</p>
<p>也可以为类型本身定义属性,不管类型有多少个实例,这些属性都只有唯一一份。这种属性就是<em>类属性</em></p>
<p>类属性用于定义特定类型所有实例共享的数据,比如所有实例都能用的一个常量(就像 C 语言中的静态常量),或者所有实例都能访问的一个变量(就像 C 语言中的静态变量)。</p>
<p>对于值类型指结构体和枚举可以定义存储型和计算型类属性对于类class则只能定义计算型类属性。</p>
<p>值类型的存储型类属性可以是变量或常量,计算型类属性跟实例的计算属性一样定义成变量属性。</p>
<blockquote>
<p>注意</p>
<p>跟实例的存储属性不同,必须给存储型类属性指定默认值,因为类型本身无法在初始化过程中使用构造器给类属性赋值。</p>
</blockquote>
<h3 id="-">类属性语法</h3>
<p>在 C 或 Objective-C 中,静态常量和静态变量的定义是通过特定类型加上<code>global</code>关键字。在 Swift 编程语言中,类属性是作为类型定义的一部分写在类型最外层的花括号内,因此它的作用范围也就在类型支持的范围内。</p>
<p>使用关键字<code>static</code>来定义值类型的类属性,关键字<code>class</code>来为类class定义类属性。下面的例子演示了存储型和计算型类属性的语法</p>
<pre><code>struct SomeStructure {
static var storedTypeProperty = &quot;Some value.&quot;
static var computedTypeProperty: Int {
// return an Int value here
}
}
enum SomeEnumeration {
static var storedTypeProperty = &quot;Some value.&quot;
static var computedTypeProperty: Int {
// return an Int value here
}
}
class SomeClass {
class var computedTypeProperty: Int {
// return an Int value here
}
}
</code></pre><blockquote>
<p>注意</p>
<p>例子中的计算型类属性是只读的,但也可以定义可读可写的计算型类属性,跟实例计算属性的语法类似。</p>
</blockquote>
<h3 id="-">获取和设置类属性的值</h3>
<p>跟实例的属性一样,类属性的访问也是通过点运算符来进行,但是,类属性是通过类型本身来获取和设置,而不是通过实例。比如:</p>
<pre><code>println(SomeClass.computedTypeProperty)
// prints &quot;42&quot;
println(SomeStructure.storedTypeProperty)
// prints &quot;Some value.&quot;
SomeStructure.storedTypeProperty = &quot;Another value.&quot;
println(SomeStructure.storedTypeProperty)
// prints &quot;Another value.”
</code></pre><p>下面的例子定义了一个结构体,使用两个存储型类属性来表示多个声道的声音电平值,每个声道有一个 0 到 10 之间的整数表示声音电平值。</p>
<p>后面的图表展示了如何联合使用两个声道来表示一个立体声的声音电平值。当声道的电平值是 0没有一个灯会亮当声道的电平值是 10所有灯点亮。本图中左声道的电平是 9右声道的电平是 7。</p>
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/staticPropertiesVUMeter_2x.png" alt="Static Properties VUMeter" width="243" height="357" /></p>
<p>上面所描述的声道模型使用<code>AudioChannel</code>结构体来表示:</p>
<pre><code>struct AudioChannel {
static let thresholdLevel = 10
static var maxInputLevelForAllChannels = 0
var currentLevel: Int = 0 {
didSet {
if currentLevel &gt; AudioChannel.thresholdLevel {
// cap the new audio level to the threshold level
currentLevel = AudioChannel.thresholdLevel
}
if currentLevel &gt; AudioChannel.maxInputLevelForAllChannels {
// store this as the new overall maximum input level
AudioChannel.maxInputLevelForAllChannels = currentLevel
}
}
}
}
</code></pre><p>结构<code>AudioChannel</code>定义了 2 个存储型类属性来实现上述功能。第一个是<code>thresholdLevel</code>,表示声音电平的最大上限阈值,它是一个取值为 10 的常量,对所有实例都可见,如果声音电平高于 10则取最大上限值 10见后面描述</p>
<p>第二个类属性是变量存储型属性<code>maxInputLevelForAllChannels</code>,它用来表示所有<code>AudioChannel</code>实例的电平值的最大值,初始值是 0。</p>
<p><code>AudioChannel</code>也定义了一个名为<code>currentLevel</code>的实例存储属性,表示当前声道现在的电平值,取值为 0 到 10。</p>
<p>属性<code>currentLevel</code>包含<code>didSet</code>属性监视器来检查每次新设置后的属性值,有如下两个检查:</p>
<ul>
<li>如果<code>currentLevel</code>的新值大于允许的阈值<code>thresholdLevel</code>,属性监视器将<code>currentLevel</code>的值限定为阈值<code>thresholdLevel</code></li>
<li>如果修正后的<code>currentLevel</code>值大于任何之前任意<code>AudioChannel</code>实例中的值,属性监视器将新值保存在静态属性<code>maxInputLevelForAllChannels</code>中。</li>
</ul>
<blockquote>
<p>注意</p>
<p>在第一个检查过程中,<code>didSet</code>属性监视器将<code>currentLevel</code>设置成了不同的值,但这不会再次调用属性监视器。</p>
</blockquote>
<p>可以使用结构<code>AudioChannel</code>来创建表示立体声系统的两个声道<code>leftChannel</code><code>rightChannel</code></p>
<pre><code>var leftChannel = AudioChannel()
var rightChannel = AudioChannel()
</code></pre><p>如果将左声道的电平设置成 7类属性<code>maxInputLevelForAllChannels</code>也会更新成 7</p>
<pre><code>leftChannel.currentLevel = 7
println(leftChannel.currentLevel)
// prints &quot;7&quot;
println(AudioChannel.maxInputLevelForAllChannels)
// prints &quot;7”
</code></pre><p>如果试图将右声道的电平设置成 11则会将右声道的<code>currentLevel</code>修正到最大值 10同时<code>maxInputLevelForAllChannels</code>的值也会更新到 10</p>
<pre><code>rightChannel.currentLevel = 11
println(rightChannel.currentLevel)
// prints &quot;10&quot;
println(AudioChannel.maxInputLevelForAllChannels)
// prints &quot;10”
</code></pre><p>[本章完]</p>
</section>
</div>
</div>
</div>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.11" data-basepath=".." data-revision="1402300498179">
<div class="book" data-level="2.11" data-basepath=".." data-revision="1402305571512">
<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_142">
<section class="normal" id="section-gitbook_28">
<h1 id="-methods-">方法(Methods)</h1>
<p><strong>方法</strong>是与某些特定类型相关联的功能/函数。类、结构体、枚举都可以定义实例方法;实例方法为指定类型的实例封装了特定的任务与功能。类、结构体、枚举也可以定义类(型)方法(type itself)类型方法与类型自身相关联。类型方法与Objective-C中的类方法(class methods)相似。</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.12" data-basepath=".." data-revision="1402300498179">
<div class="book" data-level="2.12" data-basepath=".." data-revision="1402305571512">
<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_143">
<section class="normal" id="section-gitbook_29">
<h1 id="-subscripts-">下标 (Subscripts)</h1>
<p>下标可以定义在类(Class)、结构体(structures)和枚举(enumerations)这些目标中,可以认为是访问对象、集合或序列的快捷方式。举例来说,用下标访问一个数组(Array)实例中的元素可以这样写 <code>someArray[index]</code> ,访问字典(Dictionary)实例中的元素可以这样写 <code>someDictionary[key]</code>,而不需要再调用实例的某个方法来获得元素的值。</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.13" data-basepath=".." data-revision="1402300498179">
<div class="book" data-level="2.13" data-basepath=".." data-revision="1402305571512">
<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_121">
<section class="normal" id="section-gitbook_7">
<h1 id="-">继承</h1>
<p>一个类可以继承另一个类的方法属性和其它特性。当一个类继承其它类继承类叫子类被继承类叫超类或父类。在Swift中继承是区分「类」与其它类型的一个基本特征。</p>

View File

@ -46,7 +46,7 @@
<div class="book" data-level="2.14" data-basepath=".." data-revision="1402300498179">
<div class="book" data-level="2.14" data-basepath=".." data-revision="1402305571512">
<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="1402300498179">
<div class="book" data-level="2.15" data-basepath=".." data-revision="1402305571512">
<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_146">
<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="1402300498179">
<div class="book" data-level="2.16" data-basepath=".." data-revision="1402305571512">
<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_147">
<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="1402300498179">
<div class="book" data-level="2.17" data-basepath=".." data-revision="1402305571512">
<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_148">
<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="1402300498179">
<div class="book" data-level="2.18" data-basepath=".." data-revision="1402305571512">
<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_149">
<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="1402300498179">
<div class="book" data-level="2.19" data-basepath=".." data-revision="1402305571512">
<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_150">
<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="1402300498179">
<div class="book" data-level="2.20" data-basepath=".." data-revision="1402305571512">
<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_151">
<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="1402300498179">
<div class="book" data-level="2.21" data-basepath=".." data-revision="1402305571512">
<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="1402300498179">
<div class="book" data-level="2.22" data-basepath=".." data-revision="1402305571512">
<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_154">
<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="1402300498179">
<div class="book" data-level="2.23" data-basepath=".." data-revision="1402305571512">
<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="1402300498179">
<div class="book" data-level="2" data-basepath=".." data-revision="1402305571512">
<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_158">
<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="1402300498179">
<div class="book" data-level="3.1" data-basepath=".." data-revision="1402305571512">
<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_160">
<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="1402300498179">
<div class="book" data-level="3.2" data-basepath=".." data-revision="1402305571512">
<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="1402300498179">
<div class="book" data-level="3.3" data-basepath=".." data-revision="1402305571512">
<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="1402300498179">
<div class="book" data-level="3.4" data-basepath=".." data-revision="1402305571512">
<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="1402300498179">
<div class="book" data-level="3.6" data-basepath=".." data-revision="1402305571512">
<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="1402300498179">
<div class="book" data-level="3.7" data-basepath=".." data-revision="1402305571512">
<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_159">
<section class="normal" id="section-gitbook_45">
<h1 id="-">特性</h1>
<p>特性提供了关于声明和类型的更多信息。在Swift中有两类特性用于修饰声明的以及用于修饰类型的。例如<code>required</code>特性,当应用于一个类的指定或便利初始化器声明时,表明它的每个子类都必须实现那个初始化器。再比如<code>noreturn</code>特性,当应用于函数或方法类型时,表明该函数或方法不会返回到它的调用者。</p>

View File

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

View File

@ -46,7 +46,7 @@
<div class="book" data-level="3" data-basepath=".." data-revision="1402300498179">
<div class="book" data-level="3" data-basepath=".." data-revision="1402305571512">
<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="1402300498179">
<div class="book" data-level="0" data-basepath="." data-revision="1402305571512">
<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_117">
<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 1402300498180
# Revision 1402305571513
CACHE:
index.html

File diff suppressed because one or more lines are too long