update new articles
This commit is contained in:
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="1.1" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="1.1" data-basepath=".." data-revision="1402387034522">
|
||||
<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_386">
|
||||
<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>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="1.2" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="1.2" data-basepath=".." data-revision="1402387034522">
|
||||
<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_387">
|
||||
<section class="normal" id="section-gitbook_5">
|
||||
|
||||
<h1 id="swift-">Swift 初见</h1>
|
||||
<p>本页内容包括:</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="1" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="1" data-basepath=".." data-revision="1402387034522">
|
||||
<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_388">
|
||||
<section class="normal" id="section-gitbook_6">
|
||||
|
||||
<h1 id="-swift">欢迎使用 Swift</h1>
|
||||
<p>在本章中您将了解 Swift 的特性和开发历史,并对 Swift 有一个初步的了解。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.1" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="2.1" data-basepath=".." data-revision="1402387034522">
|
||||
<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_390">
|
||||
<section class="normal" id="section-gitbook_8">
|
||||
|
||||
<h1 id="-">基础部分</h1>
|
||||
<p>Swift 是 iOS 和 OS X 应用开发的一门新语言。然而,如果你有 C 或者 Objective-C 开发经验的话,你会发现 Swift 的很多内容都是你熟悉的。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.2" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="2.2" data-basepath=".." data-revision="1402387034522">
|
||||
<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,13 +587,13 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_417">
|
||||
<section class="normal" id="section-gitbook_35">
|
||||
|
||||
<h1 id="-">基础运算符</h1>
|
||||
<h1 id="-">基本运算符</h1>
|
||||
<p>运算符是检查, 改变, 合并值的特殊符号或短语. 例如, 加号 <code>+</code> 把计算两个数的和(如 <code>let i = 1 + 2</code>). 复杂些的运行算包括逻辑与<code>&&</code>(如 <code>if enteredDoorCode && passedRetinaScan</code>), 还有自增运算符 <code>++i</code> 这样让自身加一的便捷运算.</p>
|
||||
<p>Swift支持大部分标准C语言的运算符, 且改进许多特性来减少常规编码错误. 如, 赋值符 <code>=</code> 不返回值, 以防止错把等号 <code>==</code> 写成赋值号 <code>=</code> 而导致Bug. 数值运算符( <code>+</code> , <code>-</code>, <code>*</code>, <code>/</code>, <code>%</code>等)会检测并不允许值溢出, 以此来避免保存变量时由于变量大于或小于其类型所能承载的范围时导致的异常结果. 当然允许你选择使用Swift的溢出运算符来玩溢出. 具体使用请移步<a href="Overflow Operators">溢出运算符</a>.</p>
|
||||
<p>区别于C语言, 在Swift中你可以对浮点数进行取余运算( <code>%</code> ), 还提供了C语言没有的表达两数之间的值的区间运算符, ( <code>a..b</code> 和 <code>a...b</code> ), 这方便我们表达一个区间内的数值.</p>
|
||||
<p>本章节只描述了Swift中的简单运算符, <a href="http://#" target="_blank">高级运算符</a>包含了高级运算符,及如何自定义运算符, 及如何进行自定义类型的运算符重载.</p>
|
||||
<p>本章节只描述了Swift中的基本运算符, <a href="http://#" target="_blank">高级运算符</a>包含了高级运算符,及如何自定义运算符, 及如何进行自定义类型的运算符重载.</p>
|
||||
<h1 id="-">术语</h1>
|
||||
<p>运算符有一目, 双目和三目运算符.</p>
|
||||
<p>一目运算符对单一操作对象操作, 如 <code>-a</code>.</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.3" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="2.3" data-basepath=".." data-revision="1402387034522">
|
||||
<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_391">
|
||||
<section class="normal" id="section-gitbook_9">
|
||||
|
||||
<h1 id="-strings-and-characters-">字符串和字符 (Strings and Characters)</h1>
|
||||
<hr>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.4" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="2.4" data-basepath=".." data-revision="1402387034522">
|
||||
<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_403">
|
||||
<section class="normal" id="section-gitbook_21">
|
||||
|
||||
<h1 id="-collection-types-">集合类型 (Collection Types)</h1>
|
||||
<p>Swift语言提供经典的数组和字典两种集合类型来存储集合数据。数组用来按顺序存储相同类型的数据。字典虽然无序存储相同类型数据值但是需要由独有的标识符引用和寻址(就是键值对)。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.5" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="2.5" data-basepath=".." data-revision="1402387034522">
|
||||
<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_404">
|
||||
<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>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.6" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="2.6" data-basepath=".." data-revision="1402387034522">
|
||||
<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_405">
|
||||
<section class="normal" id="section-gitbook_23">
|
||||
|
||||
<h1 id="-functions-">函数(Functions)</h1>
|
||||
<p>本页包含内容:</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.7" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="2.7" data-basepath=".." data-revision="1402387034522">
|
||||
<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_406">
|
||||
<section class="normal" id="section-gitbook_24">
|
||||
|
||||
<h1 id="-">闭包</h1>
|
||||
<hr>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.8" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="2.8" data-basepath=".." data-revision="1402387034522">
|
||||
<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_412">
|
||||
<section class="normal" id="section-gitbook_30">
|
||||
|
||||
<h1 id="-">枚举</h1>
|
||||
<p>本页内容包含:</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.9" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="2.9" data-basepath=".." data-revision="1402387034522">
|
||||
<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_413">
|
||||
<section class="normal" id="section-gitbook_31">
|
||||
|
||||
<h3 id="-">类和结构体</h3>
|
||||
<p>本页包含内容:</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.10" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="2.10" data-basepath=".." data-revision="1402387034522">
|
||||
<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_414">
|
||||
<section class="normal" id="section-gitbook_32">
|
||||
|
||||
<h1 id="-properties-">属性 (Properties)</h1>
|
||||
<p><strong>属性</strong>将值跟特定的类、结构或枚举关联。一种是存储属性,把常量或变量的值作为实例的一部分,一种是计算属性,它计算一个值。计算属性可以用于类、结构和枚举里,存储属性只能用于类和结构。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.11" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="2.11" data-basepath=".." data-revision="1402387034522">
|
||||
<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,15 +587,15 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_415">
|
||||
<section class="normal" id="section-gitbook_33">
|
||||
|
||||
<h1 id="-methods-">方法(Methods)</h1>
|
||||
<p><strong>方法</strong>是与某些特定类型相关联的功能/函数。类、结构体、枚举都可以定义实例方法;实例方法为指定类型的实例封装了特定的任务与功能。类、结构体、枚举也可以定义类(型)方法(type itself);类型方法与类型自身相关联。类型方法与Objective-C中的类方法(class methods)相似。</p>
|
||||
<p>在Swift中,结构体和枚举能够定义方法;事实上这是Swift与C/Objective-C的主要区别之一。在Objective-C中,类是唯一能定义方法的类型。在Swift中,你能够选择是否定义一个类/结构体/枚举,并且你仍然享有在你创建的类型(类/结构体/枚举)上定义方法的灵活性。</p>
|
||||
<p><strong>方法</strong>是与某些特定类型相关联的函数。类、结构体、枚举都可以定义实例方法;实例方法为给定类型的实例封装了具体的任务与功能。类、结构体、枚举也可以定义类型方法;类型方法与类型自身相关联。类型方法与Objective-C中的类方法(class methods)相似。</p>
|
||||
<p>在Swift中,结构体和枚举能够定义方法;事实上这是Swift与C/Objective-C的主要区别之一。在Objective-C中,类是唯一能定义方法的类型。但在Swift中,你不仅能选择是否要定义一个类/结构体/枚举,还能灵活的在你创建的类型(类/结构体/枚举)上定义方法。</p>
|
||||
<h3 id="-instance-methods-">实例方法(Instance Methods)</h3>
|
||||
<p><strong>实例方法</strong>是某个特定类、结构体或者枚举类型的实例的方法。实例方法支撑实例的功能: 或者提供方法,以访问和修改实例属性;或者提供与实例的目的相关的功能。实例方法的语法与函数完全一致,参考<a href="functions.html" title="函数说明">函数说明</a>。</p>
|
||||
<p>实例方法要写在它所属的类型的前后括号之间。实例方法能够访问他所属类型的所有的其他实例方法和属性。实例方法只能被它所属的类的特定实例调用。实例方法不能被孤立于现存的实例而被调用。</p>
|
||||
<p>下面是定义一个很简单的类<code>Counter</code>的例子(<code>Counter</code>能被用来对一个动作发生的次数进行计数):</p>
|
||||
<p><strong>实例方法</strong>是某个特定类、结构体或者枚举类型的实例的方法。实例方法支撑实例的功能: 或者提供方法,以访问和修改实例属性;或者提供与实例的目的相关的功能。实例方法的语法与函数完全一致,你可以参考<a href="../charpter2/06_Functions.html">函数</a>。</p>
|
||||
<p>实例方法要写在它所属的类型的前后大括号之间。实例方法能够隐式访问它所属类型的所有的其他实例方法和属性。实例方法只能被它所属的类的某个特定实例调用。实例方法不能脱离于现存的实例而被调用。</p>
|
||||
<p>下面的例子,定义一个很简单的类<code>Counter</code>,<code>Counter</code>能被用来对一个动作发生的次数进行计数:</p>
|
||||
<pre><code>class Counter {
|
||||
var count = 0
|
||||
func increment() {
|
||||
@ -608,7 +608,7 @@
|
||||
count = 0
|
||||
}
|
||||
}
|
||||
</code></pre><p><code>Counter</code>类定理了三个实例方法:</p>
|
||||
</code></pre><p><code>Counter</code>类定义了三个实例方法:</p>
|
||||
<ul>
|
||||
<li><code>increment</code>让计数器按一递增;</li>
|
||||
<li><code>incrementBy(amount: Int)</code>让计数器按一个指定的整数值递增;</li>
|
||||
@ -625,10 +625,9 @@
|
||||
counter.reset()
|
||||
// the counter's value is now 0
|
||||
</code></pre><h3 id="-local-and-external-parameter-names-for-methods-">方法的局部参数名称和外部参数名称(Local and External Parameter Names for Methods)</h3>
|
||||
<p>函数参数有一个局部名称(在函数体内部使用)和一个外部名称(在调用函数时使用),参考<a href="external_parameter_names.html">External Parameter Names</a>。对于方法参数也是这样,因为方法就是函数(只是这个函数与某个类型相关联了)。但是,方法和函数的局部名称和外部名称的默认行为是不一样的。</p>
|
||||
<p>Swift中的方法和Objective-C中的方法极其相似。像在Objective-C中一样,Swift中方法的名称通常用一个介词指向方法的第一个参数,比如:<code>with</code>,<code>for</code>,<code>by</code>等等。前面的<code>Counter</code>类的例子中<code>incrementBy</code>方法就是这样的。介词的使用让方法在被调用时能像一个句子一样被解读。Swift这种方法命名约定很容易落实,因为它是用不同的默认处理方法参数的方式,而不是用函数参数(来实现的)。</p>
|
||||
<p>具体来说,Swift默认仅给方法的第一个参数名称一个局部参数名称;但是默认同时给第二个和后续的参数名称局部参数名称和外部参数名称。
|
||||
这个约定与典型的命名和调用约定相匹配,这与你在写Objective-C的方法时很相似。这个约定还让expressive method调用不需要再检查/限定参数名。</p>
|
||||
<p>函数参数可以同时有一个局部名称(在函数体内部使用)和一个外部名称(在调用函数时使用),参考<a href="../06_Functions.html">函数的外部参数名</a>。方法参数也一样(因为方法就是函数,只是这个函数与某个类型相关联了)。但是,方法和函数的局部名称和外部名称的默认行为是不一样的。</p>
|
||||
<p>Swift中的方法和Objective-C中的方法极其相似。像在Objective-C中一样,Swift中方法的名称通常用一个介词指向方法的第一个参数,比如:<code>with</code>,<code>for</code>,<code>by</code>等等。前面的<code>Counter</code>类的例子中<code>incrementBy</code>方法就是这样的。介词的使用让方法在被调用时能像一个句子一样被解读。Swift这种方法命名约定让书写代码更容易了,因为Swift依靠对方法参数不同的默认处理方式,而不是靠函数参数,来实现方法命名。</p>
|
||||
<p>具体来说,Swift默认仅给方法的第一个参数名称一个局部参数名称;但是默认同时给第二个和后续的参数名称局部参数名称和外部参数名称。这个约定与典型的命名和调用约定相适应,这与你在写Objective-C的方法时很相似。这个约定还让表达式方法在调用时不需要再限定参数名称。</p>
|
||||
<p>看看下面这个<code>Counter</code>的替换版本(它定义了一个更复杂的<code>incrementBy</code>方法):</p>
|
||||
<pre><code>class Counter {
|
||||
var count: Int = 0
|
||||
@ -641,25 +640,23 @@
|
||||
counter.incrementBy(5, numberOfTimes: 3)
|
||||
// counter value is now 15
|
||||
</code></pre><p>你不必为第一个参数值再定义一个外部变量名:因为从函数名<code>incrementBy</code>已经能很清楚地看出它的目的/作用。但是第二个参数,就要被一个外部参数名称所限定,以便在方法被调用时让他目的/作用明确。</p>
|
||||
<p>这种默认的行为能够有效的检查方法,比如你在参数numberOfTimes前写了个井号( <code>#</code> )时:</p>
|
||||
<p>这种默认的行为能够有效的处理方法,比如你在参数numberOfTimes前写了个井号( <code>#</code> )时:</p>
|
||||
<pre><code>func incrementBy(amount: Int, #numberOfTimes: Int) {
|
||||
count += amount * numberOfTimes
|
||||
}
|
||||
</code></pre><p>这种默认行为使上面代码意味着:在Swift中定义方法使用了与Objective-C同样的语法风格,并且方法将以自然表达式的方式被调用。</p>
|
||||
<h3 id="-modifying-external-parameter-name-behavior-for-methods-">修改外部参数名称(Modifying External Parameter Name Behavior for Methods)</h3>
|
||||
<p>有时为方法的第一个参数提供一个外部参数名称是非常有用的,尽管这不是默认的行为。你可以自己添加一个明确的外部名称;你也可以用一个hash符号作为第一个参数的前缀,然后用这个局部名字作为外部名字。</p>
|
||||
<h3 id="-modifying-external-parameter-name-behavior-for-methods-">修改方法的外部参数名称(Modifying External Parameter Name Behavior for Methods)</h3>
|
||||
<p>有时为方法的第一个参数提供一个外部参数名称是非常有用的,尽管这不是默认的行为。你可以自己添加一个显式的外部名称;你也可以用一个hash符号作为第一个参数的前缀,把这个局部名称呢更作为外部名称。</p>
|
||||
<p>相反,如果你不想为方法的第二个及后续的参数提供一个外部名称,你可以通过使用下划线(<code>_</code>)作为该参数的显式外部名称来覆盖默认行为。</p>
|
||||
<h3 id="-self-the-self-property-"><code>self</code>属性(The self Property)</h3>
|
||||
<p>类型的每一个实例都有一个隐含属性叫做<code>self</code>,它完全等同于这个实力变量本身。你可以在一个实例的实例方法中使用这个隐含的<code>self</code>属性来引用当前实例。</p>
|
||||
<p>上面例子中的<code>increment</code>方法可以被写成这样:</p>
|
||||
<p>类型的每一个实例都有一个隐含属性叫做<code>self</code>,<code>self</code>完全等同于该实例本身。你可以在一个实例的实例方法中使用这个隐含的<code>self</code>属性来引用当前实例。</p>
|
||||
<p>上面例子中的<code>increment</code>方法还可以这样写:</p>
|
||||
<pre><code>func increment() {
|
||||
self.count++
|
||||
}
|
||||
</code></pre><p>实际上,你不必在你的代码里面经常写<code>self</code>。不论何时,在一个方法中使用一个已知的属性或者方法名称,如果你没有明确的写<code>self</code>,Swift假定你是指当前实例的属性或者方法。这种假定在上面的<code>Counter</code>中已经示范了:<code>Counter</code>中的三个实例方法中都使用的是<code>count</code>(而不是<code>self.count</code>)</p>
|
||||
<p>这条规则的主要例外发生在当实例方法的某个参数名称与实例的某个属性名称相同时。
|
||||
在这种情况下,参数名称享有优先权,并且在引用属性时必须使用一种更恰当(被限定更严格)的方式。
|
||||
你可以使用隐藏的<code>self</code>属性来区分参数名称和属性名称。</p>
|
||||
<p>下面的例子演示了<code>self</code>消除方法参数<code>x</code>和实例属性<code>x</code>之间的歧义:</p>
|
||||
<p>这条规则的主要例外发生在当实例方法的某个参数名称与实例的某个属性名称相同时。在这种情况下,参数名称享有优先权,并且在引用属性时必须使用一种更恰当(被限定更严格)的方式。你可以使用<code>self</code>属性来区分参数名称和属性名称。</p>
|
||||
<p>下面的例子中,<code>self</code>消除方法参数<code>x</code>和实例属性<code>x</code>之间的歧义:</p>
|
||||
<pre><code>struct Point {
|
||||
var x = 0.0, y = 0.0
|
||||
func isToTheRightOfX(x: Double) -> Bool {
|
||||
@ -673,8 +670,8 @@ if somePoint.isToTheRightOfX(1.0) {
|
||||
// prints "This point is to the right of the line where x == 1.0"
|
||||
</code></pre><p>如果不使用<code>self</code>前缀,Swift就认为两次使用的<code>x</code>都指的是名称为<code>x</code>的函数参数。</p>
|
||||
<h3 id="-modifying-value-types-from-within-instance-methods-">在实例方法中修改值类型(Modifying Value Types from Within Instance Methods)</h3>
|
||||
<p>结构体和枚举是<strong>值类型</strong><a href=""#"">Structures and Enumerations Are Value Types</a>。一般情况下,值类型的属性不能在他的实例方法中被修改。</p>
|
||||
<p>但是,如果你确实需要在某个具体的方法中修改结构体或者枚举的属性,你可以选择<code>变异(mutating)</code>这个方法。方法可以从内部变异它的属性;并且它做的任何改变在方法结束时都会回写到原始结构。方法会给它隐含的<code>self</code>属性赋值一个全新的实例,这个新实例在方法结束后将替换原来的实例。</p>
|
||||
<p>结构体和枚举是<strong>值类型</strong>。一般情况下,值类型的属性不能在它的实例方法中被修改。</p>
|
||||
<p>但是,如果你确实需要在某个具体的方法中修改结构体或者枚举的属性,你可以选择<code>变异(mutating)</code>这个方法,然后方法就可以从方法内部改变它的属性;并且它做的任何改变在方法结束时都会回写到原始结构。方法还可以给它隐含的<code>self</code>属性赋值一个全新的实例,这个新实例在方法结束后将替换原来的实例。</p>
|
||||
<p>要<code>变异</code>方法, 将关键字<code>mutating</code> 放到方法的<code>func</code>关键字之前就可以了:</p>
|
||||
<pre><code>struct Point {
|
||||
var x = 0.0, y = 0.0
|
||||
@ -687,8 +684,8 @@ var somePoint = Point(x: 1.0, y: 1.0)
|
||||
somePoint.moveByX(2.0, y: 3.0)
|
||||
println("The point is now at (\(somePoint.x), \(somePoint.y))")
|
||||
// prints "The point is now at (3.0, 4.0)"
|
||||
</code></pre><p>上面的Point结构体定义了一个变异方法(mutating method)<code>moveByX</code>,<code>moveByX</code>用来移动一个point。<code>moveByX</code>方法在被调用时修改了这个point,而不是返回一个新的point。方法定义是加上那个了<code>mutating</code>关键字,所以方法可以修改值类型的属性了。</p>
|
||||
<p>注意:不能在结构体类型的常量上调用变异方法,因为常量的属性不能被改变,就算你想改变的是常量的可变属性也不行,参考<a href=""#"">Stored Properties of Constant Structure Instances</a></p>
|
||||
</code></pre><p>上面的Point结构体定义了一个变异方法(mutating method)<code>moveByX</code>,<code>moveByX</code>用来移动一个point。<code>moveByX</code>方法在被调用时修改了这个point,而不是返回一个新的point。方法定义时加上<code>mutating</code>关键字,这才让方法可以修改值类型的属性了。</p>
|
||||
<p>注意:不能在结构体类型的常量上调用变异方法,因为常量的属性不能被改变,就算你想改变的是常量的可变属性也不行,参考<a href=""../10_Properties"">存储属性和实例变量</a></p>
|
||||
<pre><code>let fixedPoint = Point(x: 3.0, y: 3.0)
|
||||
fixedPoint.moveByX(2.0, y: 3.0)
|
||||
// this will report an error
|
||||
@ -700,8 +697,8 @@ fixedPoint.moveByX(2.0, y: 3.0)
|
||||
self = Point(x: x + deltaX, y: y + deltaY)
|
||||
}
|
||||
}
|
||||
</code></pre><p>新版的变异方法<code>moveByX</code>创建了一个新的分支结构(他的x和y的值都被设定为目标值了)。调用这个版本的方法和调用上个版本的最终结果是一样的。</p>
|
||||
<p>枚举的变异方法可以让<code>self</code>从相同的枚举设置为不同的成员。</p>
|
||||
</code></pre><p>新版的变异方法<code>moveByX</code>创建了一个新的结构(它的x和y的值都被设定为目标值了)。调用这个版本的方法和调用上个版本的最终结果是一样的。</p>
|
||||
<p>枚举的变异方法可以让<code>self</code>从相同的枚举设置为不同的成员:</p>
|
||||
<pre><code>enum TriStateSwitch {
|
||||
case Off, Low, High
|
||||
mutating func next() {
|
||||
@ -778,7 +775,7 @@ SomeClass.someTypeMethod()
|
||||
player.completedLevel(1)
|
||||
println("highest unlocked level is now \(LevelTracker.highestUnlockedLevel)")
|
||||
// prints "highest unlocked level is now 2"
|
||||
</code></pre><p>如果你创建了第二个玩家,并尝试让他开始一个没有被任何玩家解锁的等级,你试图去设置玩家当前等级时会失败的:</p>
|
||||
</code></pre><p>如果你创建了第二个玩家,并尝试让他开始一个没有被任何玩家解锁的等级,当你试图去设置玩家当前等级时会失败的:</p>
|
||||
<pre><code>player = Player(name: "Beto")
|
||||
if player.tracker.advanceToLevel(6) {
|
||||
println("player is now on level 6")
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.12" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="2.12" data-basepath=".." data-revision="1402387034522">
|
||||
<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_416">
|
||||
<section class="normal" id="section-gitbook_34">
|
||||
|
||||
<h1 id="-subscripts-">下标 (Subscripts)</h1>
|
||||
<p>下标可以定义在类(Class)、结构体(structures)和枚举(enumerations)这些目标中,可以认为是访问对象、集合或序列的快捷方式。举例来说,用下标访问一个数组(Array)实例中的元素可以这样写 <code>someArray[index]</code> ,访问字典(Dictionary)实例中的元素可以这样写 <code>someDictionary[key]</code>,而不需要再调用实例的某个方法来获得元素的值。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.13" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="2.13" data-basepath=".." data-revision="1402387034522">
|
||||
<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_389">
|
||||
<section class="normal" id="section-gitbook_7">
|
||||
|
||||
<h1 id="-">继承</h1>
|
||||
<p>一个类可以继承另一个类的方法,属性和其它特性。当一个类继承其它类,继承类叫子类,被继承类叫超类(或父类)。在Swift中,继承是区分「类」与其它类型的一个基本特征。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.14" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="2.14" data-basepath=".." data-revision="1402387034522">
|
||||
<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>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.15" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="2.15" data-basepath=".." data-revision="1402387034522">
|
||||
<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_419">
|
||||
<section class="normal" id="section-gitbook_37">
|
||||
|
||||
<h1 id="-">析构过程</h1>
|
||||
<p>在一个类的实例被释放之前,析构函数被立即调用。用关键字deinit来标示析构函数,类似于初始化函数用init来标示。析构函数只适用于类类型。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.16" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="2.16" data-basepath=".." data-revision="1402387034522">
|
||||
<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_420">
|
||||
<section class="normal" id="section-gitbook_38">
|
||||
|
||||
<h1 id="-">自动引用计数</h1>
|
||||
<p>本页包含内容:</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.17" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="2.17" data-basepath=".." data-revision="1402387034522">
|
||||
<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_421">
|
||||
<section class="normal" id="section-gitbook_39">
|
||||
|
||||
<h1 id="optional-chaining">Optional Chaining</h1>
|
||||
<p>可选链(Optional Chaining)是一种可以请求和调用属性、方法及子脚本的过程,它的自判断性体现于请求或调用的目标当前可能为空(<code>nil</code>)。如果自判断的目标有值,那么调用就会成功;相反,如果选择的目标为空(<code>nil</code>),则这种调用将返回空(<code>nil</code>)。多次请求或调用可以被链接在一起形成一个链,如果任何一个节点为空(<code>nil</code>)将导致整个链失效。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.18" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="2.18" data-basepath=".." data-revision="1402387034522">
|
||||
<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_422">
|
||||
<section class="normal" id="section-gitbook_40">
|
||||
|
||||
<h1 id="-type-casting-">类型检查(Type Casting)</h1>
|
||||
<p>(ps:为了方便各位检验所以保留了英文,可删。)
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.19" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="2.19" data-basepath=".." data-revision="1402387034522">
|
||||
<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_423">
|
||||
<section class="normal" id="section-gitbook_41">
|
||||
|
||||
<h1 id="-">类型嵌套</h1>
|
||||
<p>本页包含内容:</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.20" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="2.20" data-basepath=".." data-revision="1402387034522">
|
||||
<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_424">
|
||||
<section class="normal" id="section-gitbook_42">
|
||||
|
||||
<h1 id="-extensions-">扩展(Extensions)</h1>
|
||||
<hr>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.21" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="2.21" data-basepath=".." data-revision="1402387034522">
|
||||
<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_429">
|
||||
<section class="normal" id="section-gitbook_44">
|
||||
|
||||
<h1 id="-">协议</h1>
|
||||
<p><code>Protocol(协议)</code>用于<strong>统一</strong>方法和属性的名称,而不实现任何功能,(<em>译者注: 协议在其他语言中也称作<code>接口(Interface)</code></em>).<code>协议</code>能够被<code>类</code>,<code>枚举</code>,<code>结构体</code>实现,满足协议要求的<code>类</code>,<code>枚举</code>,<code>结构体</code>被称为协议的<code>遵循者</code>.</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.22" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="2.22" data-basepath=".." data-revision="1402387034522">
|
||||
<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_426">
|
||||
<section class="normal" id="section-gitbook_45">
|
||||
|
||||
<h1 id="-">泛型</h1>
|
||||
<hr>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.23" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="2.23" data-basepath=".." data-revision="1402387034522">
|
||||
<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_48">
|
||||
|
||||
<p>In addition to the operators described in <a href="Basic Operators">Basic Operators</a>, Swift provides several advanced operators that perform more complex value manipulation. These include all of the bitwise and bit shifting operators you will be familiar with from C and Objective-C.</p>
|
||||
<p>除于<a href="Basic Operators">基本操作符</a>中所讲的运算符, Swift还有许多复杂的高级运算符, 包括了C语和Objective-C中的位运算符和位移运算.</p>
|
||||
<p>Unlike arithmetic operators in C, arithmetic operators in Swift do not overflow by default. Overflow behavior is trapped and reported as an error. To opt in to overflow behavior, use Swift’s second set of arithmetic operators that overflow by default, such as the overflow addition operator (&+). All of these overflow operators begin with an ampersand (&).</p>
|
||||
<p>不同于C语言中的数值计算, Swift的数值计算默认是不可溢出的. 溢出行为会被捕获并报告为错误. 你是故意的? 好吧, 你可以使用Swift为你准备的另一套默认允许溢出的数值运算符, 如可溢出加 <code>&+</code>. 所有允许溢出的运算符都是以 <code>&</code> 开始的.</p>
|
||||
<p>When you define your own structures, classes, and enumerations, it can be useful to provide your own implementations of the standard Swift operators for these custom types. Swift makes it easy to provide tailored implementations of these operators and to determine exactly what their behavior should be for each type you create.</p>
|
||||
<p>自定义的结构, 类和枚举, 是否可以使用标准的运算符来定义操作? 当然可以, 在Swift中, 你可以对你创建的类型定制运算符的行为.</p>
|
||||
<p>You’re not just limited to the predefined operators. Swift gives you the freedom to define your own custom infix, prefix, postfix, and assignment operators, with custom precedence and associativity values. These operators can be used and adopted in your code just like any of the predefined operators, and you can even extend existing types to support the custom operators you define.</p>
|
||||
<p>可定制的运算符并不限于那些预设的运算符, 也创建个性的中置, 前置,后置,还有赋值运算符, 当然优先级和结合性也是可以自定义的. 这些运算符的实现可以使用预设的运算符, 也可以使用已定制好的运算符.</p>
|
||||
<h1 id="bitwise-operators">Bitwise Operators</h1>
|
||||
<h1 id="-">位运算符</h1>
|
||||
<p>Bitwise operators enable you to manipulate the individual raw data bits within a data structure. They are often used in low-level programming, such as graphics programming and device driver creation. Bitwise operators can also be useful when you work with raw data from external sources, such as encoding and decoding data for communication over a custom protocol.</p>
|
||||
<p>位操作符通常在诸如图像处理和创建设备驱动等底层开发中使用, 使用它可以操作数据结构中原始数据中的个别比特. 当然, 使用一个自定的协议进行通信的时候, 使用位运算符来对原始数据进行编码和解码也是非常有效的.</p>
|
||||
<p>Swift supports all of the bitwise operators found in C, as described below.</p>
|
||||
<p>Swift支持所有C语言的位运算符, 如下描述:</p>
|
||||
<h2 id="bitwise-not-operator">Bitwise NOT Operator</h2>
|
||||
<h2 id="-">按位取反运算符</h2>
|
||||
<p>The bitwise NOT operator (~) inverts all bits in a number:</p>
|
||||
<p>按位取反运算符 <code>~</code> 对一个操作数的每一位都取反.</p>
|
||||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/bitwiseNOT_2x.png" alt="Art/bitwiseNOT_2x.png" title="Art/bitwiseNOT_2x.png"></p>
|
||||
<p>The bitwise NOT operator is a prefix operator, and appears immediately before the value it operates on, without any white space:</p>
|
||||
<p>这个运算符是前置, 所以请不加任何空格地写着操作数之前.</p>
|
||||
<pre><code>let initialBits: UInt8 = 0b00001111
|
||||
let invertedBits = ~initialBits // 等于 0b11110000
|
||||
</code></pre><p>UInt8 integers have eight bits and can store any value between 0 and 255. This example initializes a UInt8 integer with the binary value 00001111, which has its first four bits set to 0, and its second four bits set to 1. This is equivalent to a decimal value of 15.</p>
|
||||
<p><code>UInt8</code> 整型是8位的, 可以储存0~255之间的任意数. 这个例子初始化一个整型为二进制值 <code>00001111</code>(前4位为 <code>0</code>, 后4位为 <code>1</code>), 它的十进制值为 <code>15</code>.</p>
|
||||
<p>The bitwise NOT operator is then used to create a new constant called invertedBits, which is equal to initialBits, but with all of the bits inverted. Zeroes become ones, and ones become zeroes. The value of invertedBits is 11110000, which is equal to an unsigned decimal value of 240.</p>
|
||||
<p>使用按位取反运算 <code>~</code> 对 <code>initialBits</code> 操作, 然后赋值给 <code>invertedBits</code> 这个新常量. 这个新常量的值等于所有位都取反的<code>initialBits</code>, 即 <code>1</code> 变成 <code>0</code>, <code>0</code> 变成 <code>1</code>, 变成了 <code>11110000</code>, 十进制值为 <code>240</code>.</p>
|
||||
<h2 id="bitwise-and-operator">Bitwise AND Operator</h2>
|
||||
<h2 id="-">按位与运算符</h2>
|
||||
<p>The bitwise AND operator (&) combines the bits of two numbers. It returns a new number whose bits are set to 1 only if the bits were equal to 1 in both input numbers:</p>
|
||||
<p>按位与运算符对两个数进行操作, 然后返回一个新的数, 这个数的每个位都需两个输入数的同一位都为1的时候才为1.</p>
|
||||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/bitwiseAND_2x.png" alt="Art/bitwiseAND_2x.png" title="Art/bitwiseAND_2x.png"></p>
|
||||
<p>In the example below, the values of firstSixBits and lastSixBits both have four middle bits equal to 1. The bitwise AND operator combines them to make the number 00111100, which is equal to an unsigned decimal value of 60:</p>
|
||||
<p>如下代码, <code>firstSixBits</code> 和 <code>lastSixBits</code> 中间4个位都为1. 对它俩进行按位与运算后, 就得到了 <code>00111100</code>, 即十进制的 <code>60</code>.</p>
|
||||
<pre><code>let firstSixBits: UInt8 = 0b11111100
|
||||
let lastSixBits: UInt8 = 0b00111111
|
||||
let middleFourBits = firstSixBits & lastSixBits // 等于 00111100
|
||||
</code></pre><h2 id="bitwise-or-operator">Bitwise OR Operator</h2>
|
||||
<h2 id="-">按位或运算</h2>
|
||||
<p>The bitwise OR operator (|) compares the bits of two numbers. The operator returns a new number whose bits are set to 1 if the bits are equal to 1 in either input number:</p>
|
||||
<p>按位或运算符 <code>|</code> 比较两个数, 然后返回一个新的数, 这个数的每一位设置1的条件是两个输入数的同一位都不为0(即任意一个为1, 或都为1).</p>
|
||||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/bitwiseOR_2x.png" alt="Art/bitwiseOR_2x.png" title="Art/bitwiseOR_2x.png"></p>
|
||||
<p>In the example below, the values of someBits and moreBits have different bits set to 1. The bitwise OR operator combines them to make the number 11111110, which equals an unsigned decimal of 254:</p>
|
||||
<p>如下代码, <code>someBits</code> 和 <code>moreBits</code> 在不同位上有 <code>1</code>. 按位或运行的结果是 <code>11111110</code>, 即十进制的 <code>254</code>.</p>
|
||||
<pre><code>let someBits: UInt8 = 0b10110010
|
||||
let moreBits: UInt8 = 0b01011110
|
||||
let combinedbits = someBits | moreBits // 等于 11111110
|
||||
</code></pre><h2 id="bitwise-xor-operator">Bitwise XOR Operator</h2>
|
||||
<h2 id="-">按位异或运算符</h2>
|
||||
<p>The bitwise XOR operator, or “exclusive OR operator” (^), compares the bits of two numbers. The operator returns a new number whose bits are set to 1 where the input bits are different and are set to 0 where the input bits are the same:</p>
|
||||
<p>按位异或运算符 <code>^</code> 比较两个数, 然后返回一个数, 这个数的每个位设为 <code>1</code>的条件是两个输入数的同一位不同, 如果相同就设为 <code>0</code>.</p>
|
||||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/bitwiseXOR_2x.png" alt="Art/bitwiseXOR_2x.png" title="Art/bitwiseXOR_2x.png"></p>
|
||||
<p>In the example below, the values of firstBits and otherBits each have a bit set to 1 in a location that the other does not. The bitwise XOR operator sets both of these bits to 1 in its output value. All of the other bits in firstBits and otherBits match and are set to 0 in the output value:</p>
|
||||
<p>以下代码, <code>firstBits</code> 和 <code>otherBits</code> 都有一个 <code>1</code> 跟另一个数不同的. 所以按位异或的结果是把它这些位置为 <code>1</code>, 其他都置为 <code>0</code>.</p>
|
||||
<pre><code>let firstBits: UInt8 = 0b00010100
|
||||
let otherBits: UInt8 = 0b00000101
|
||||
let outputBits = firstBits ^ otherBits // 等于 00010001
|
||||
</code></pre><h2 id="bitwise-left-and-right-shift-operators">Bitwise Left and Right Shift Operators</h2>
|
||||
<h2 id="-">按位左移/右移运算符</h2>
|
||||
<p>The bitwise left shift operator (<<) and bitwise right shift operator (>>) move all bits in a number to the left or the right by a certain number of places, according to the rules defined below.</p>
|
||||
<p>左移运算符 <code><<</code> 和右移运算符 <code>>></code> 会把一个数的所有比特位按以下定义的规则向左或向右移动指定位数.</p>
|
||||
<p>Bitwise left and right shifts have the effect of multiplying or dividing an integer number by a factor of two. Shifting an integer’s bits to the left by one position doubles its value, whereas shifting it to the right by one position halves its value.</p>
|
||||
<p>按位左移和按位右移的效果相当把一个整数乘于或除于一个因子为 <code>2</code> 的整数. 向左移动一个整型的比特位相当于把这个数乘于 <code>2</code>, 向右移一位就是除于 <code>2</code>.</p>
|
||||
<h3 id="shifting-behavior-for-unsigned-integers">Shifting Behavior for Unsigned Integers</h3>
|
||||
<h3 id="-">对无符整型进行移位</h3>
|
||||
<p>The bit-shifting behavior for unsigned integers is as follows:
|
||||
对无符整型的移位的效果如下:</p>
|
||||
<p>Existing bits are moved to the left or right by the requested number of places.
|
||||
Any bits that are moved beyond the bounds of the integer’s storage are discarded.
|
||||
Zeroes are inserted in the spaces left behind after the original bits are moved to the left or right.
|
||||
This approach is known as a logical shift.</p>
|
||||
<p>已经存在的比特位向左或向右移动指定的位数. 被移出整型存储边界的的位数直接抛弃, 移动留下的空白位用零 <code>0</code> 来填充. 这种方法称为逻辑移位.</p>
|
||||
<p>The illustration below shows the results of 11111111 << 1 (which is 11111111 shifted to the left by 1 place), and 11111111 >> 1 (which is 11111111 shifted to the right by 1 place). Blue numbers are shifted, gray numbers are discarded, and orange zeroes are inserted:</p>
|
||||
<p>以下这张把展示了 <code>11111111 << 1</code> (<code>11111111</code> 向左移1位), 和 <code>11111111 << 1</code> (<code>11111111</code> 向右移1位). 蓝色的是被移位的, 灰色是被抛弃的, 橙色的 <code>0</code> 是被填充进来的.</p>
|
||||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/bitshiftUnsigned_2x.png" alt="Art/bitshiftUnsigned_2x.png" title="Art/bitshiftUnsigned_2x.png">
|
||||
Here’s how bit shifting looks in Swift code:</p>
|
||||
<pre><code>let shiftBits: UInt8 = 4 // 即二进制的00000100
|
||||
shiftBits << 1 // 00001000
|
||||
shiftBits << 2 // 00010000
|
||||
shiftBits << 5 // 10000000
|
||||
shiftBits << 6 // 00000000
|
||||
shiftBits >> 2 // 00000001
|
||||
</code></pre><p>You can use bit shifting to encode and decode values within other data types:
|
||||
你可以使用移位操作进行其他数据类型的编码和解码.</p>
|
||||
<pre><code>let pink: UInt32 = 0xCC6699
|
||||
let redComponent = (pink & 0xFF0000) >> 16 // redComponent 是 0xCC, 即 204
|
||||
let greenComponent = (pink & 0x00FF00) >> 8 // greenComponent 是 0x66, 即 102
|
||||
let blueComponent = pink & 0x0000FF // blueComponent 是 0x99, 即 153
|
||||
</code></pre><p>This example uses a UInt32 constant called pink to store a Cascading Style Sheets color value for the color pink. The CSS color value #CC6699 is written as 0xCC6699 in Swift’s hexadecimal number representation. This color is then decomposed into its red (CC), green (66), and blue (99) components by the bitwise AND operator (&) and the bitwise right shift operator (>>).</p>
|
||||
<p>这个例子使用了一个 <code>UInt32</code> 的命名为 <code>pink</code>的常量来储存层叠样式表 <code>CSS</code> 中粉色的颜色值, <code>CSS</code>颜色<code>#CC6699</code>在Swift用十六进制<code>0xCC6699</code>来表示. 然后使用按位与(&)和按位右移就可以从这个颜色值中解析出红(CC), 绿(66), 蓝(99)三个部分.</p>
|
||||
<p>The red component is obtained by performing a bitwise AND between the numbers 0xCC6699 and 0xFF0000. The zeroes in 0xFF0000 effectively “mask” the second and third bytes of 0xCC6699, causing the 6699 to be ignored and leaving 0xCC0000 as the result.</p>
|
||||
<p>对 <code>0xCC6699</code> 和 <code>0xFF0000</code> 进行按位与 <code>&</code> 操作就可以得到红色部分. <code>0xFF0000</code> 中的 <code>0</code> 了遮盖了 <code>OxCC6699</code> 的第二和第三个字节, 这样 <code>6699</code> 被忽略了, 只留下 <code>0xCC0000</code>.</p>
|
||||
<p>This number is then shifted 16 places to the right (>> 16). Each pair of characters in a hexadecimal number uses 8 bits, so a move 16 places to the right will convert 0xCC0000 into 0x0000CC. This is the same as 0xCC, which has a decimal value of 204.</p>
|
||||
<p>然后, 按向右移动16位, 即 <code>>> 16</code>. 十六进制中每两个字符是8比特位, 所以移动16位的结果是把 <code>0xCC0000</code> 变成 <code>0x0000CC</code>. 这和 <code>0xCC</code> 是相等的, 都是十进制的 <code>204</code>.</p>
|
||||
<p>Similarly, the green component is obtained by performing a bitwise AND between the numbers 0xCC6699 and 0x00FF00, which gives an output value of 0x006600. This output value is then shifted eight places to the right, giving a a value of 0x66, which has a decimal value of 102.</p>
|
||||
<p>同样的, 绿色部分来自于 <code>0xCC6699</code> 和 <code>0x00FF00</code> 的按位操作得到 <code>0x006600</code>. 然后向右移动8們, 得到 <code>0x66</code>, 即十进制的 <code>102</code>.</p>
|
||||
<p>Finally, the blue component is obtained by performing a bitwise AND between the numbers 0xCC6699 and 0x0000FF, which gives an output value of 0x000099. There’s no need to shift this to the right, as 0x000099 already equals 0x99, which has a decimal value of 153.</p>
|
||||
<p>最后, 蓝色部分对 <code>0xCC6699</code> 和 <code>0x0000FF</code> 进行按位与运算, 得到 <code>0x000099</code>, 无需向右移位了, 所以结果就是 <code>0x99</code>, 即十进制的 <code>153</code>.</p>
|
||||
<h3 id="shifting-behavior-for-signed-integers">Shifting Behavior for Signed Integers</h3>
|
||||
<p>The shifting behavior is more complex for signed integers than for unsigned integers, because of the way signed integers are represented in binary. (The examples below are based on 8-bit signed integers for simplicity, but the same principles apply for signed integers of any size.)</p>
|
||||
<p>Signed integers use their first bit (known as the sign bit) to indicate whether the integer is positive or negative. A sign bit of 0 means positive, and a sign bit of 1 means negative.</p>
|
||||
<p>The remaining bits (known as the value bits) store the actual value. Positive numbers are stored in exactly the same way as for unsigned integers, counting upwards from 0. Here’s how the bits inside an Int8 look for the number 4:</p>
|
||||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/bitshiftSignedFour_2x.png" alt="Art/bitshiftSignedFour_2x.png" title="Art/bitshiftSignedFour_2x.png">
|
||||
The sign bit is 0 (meaning “positive”), and the seven value bits are just the number 4, written in binary notation.</p>
|
||||
<p>Negative numbers, however, are stored differently. They are stored by subtracting their absolute value from 2 to the power of n, where n is the number of value bits. An eight-bit number has seven value bits, so this means 2 to the power of 7, or 128.</p>
|
||||
<p>Here’s how the bits inside an Int8 look for the number -4:</p>
|
||||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/bitshiftSignedMinusFour_2x.png" alt="Art/bitshiftSignedMinusFour_2x.png" title="Art/bitshiftSignedMinusFour_2x.png">
|
||||
This time, the sign bit is 1 (meaning “negative”), and the seven value bits have a binary value of 124 (which is 128 - 4):</p>
|
||||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/bitshiftSignedMinusFourValue_2x.png" alt="Art/bitshiftSignedMinusFourValue_2x.png" title="Art/bitshiftSignedMinusFourValue_2x.png">
|
||||
The encoding for negative numbers is known as a two’s complement representation. It may seem an unusual way to represent negative numbers, but it has several advantages.</p>
|
||||
<p>First, you can add -1 to -4, simply by performing a standard binary addition of all eight bits (including the sign bit), and discarding anything that doesn’t fit in the eight bits once you’re done:</p>
|
||||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/bitshiftSignedAddition_2x.png" alt="Art/bitshiftSignedAddition_2x.png" title="Art/bitshiftSignedAddition_2x.png">
|
||||
Second, the two’s complement representation also lets you shift the bits of negative numbers to the left and right like positive numbers, and still end up doubling them for every shift you make to the left, or halving them for every shift you make to the right. To achieve this, an extra rule is used when signed integers are shifted to the right:</p>
|
||||
<p>When you shift signed integers to the right, apply the same rules as for unsigned integers, but fill any empty bits on the left with the sign bit, rather than with a zero.
|
||||
<img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/bitshiftSigned_2x.png" alt="Art/bitshiftSigned_2x.png" title="Art/bitshiftSigned_2x.png">
|
||||
This action ensures that signed integers have the same sign after they are shifted to the right, and is known as an arithmetic shift.</p>
|
||||
<p>Because of the special way that positive and negative numbers are stored, shifting either of them to the right moves them closer to zero. Keeping the sign bit the same during this shift means that negative integers remain negative as their value moves closer to zero.</p>
|
||||
<h1 id="overflow-operators">Overflow Operators</h1>
|
||||
<p>If you try to insert a number into an integer constant or variable that cannot hold that value, by default Swift reports an error rather than allowing an invalid value to be created. This behavior gives extra safety when you work with numbers that are too large or too small.</p>
|
||||
<p>For example, the Int16 integer type can hold any signed integer number between -32768 and 32767. Trying to set a UInt16 constant or variable to a number outside of this range causes an error:</p>
|
||||
<pre><code>var potentialOverflow = Int16.max
|
||||
// potentialOverflow equals 32767, which is the largest value an Int16 can hold
|
||||
potentialOverflow += 1
|
||||
// this causes an error
|
||||
</code></pre><p>Providing error handling when values get too large or too small gives you much more flexibility when coding for boundary value conditions.</p>
|
||||
<p>However, when you specifically want an overflow condition to truncate the number of available bits, you can opt in to this behavior rather than triggering an error. Swift provides five arithmetic overflow operators that opt in to the overflow behavior for integer calculations. These operators all begin with an ampersand (&):</p>
|
||||
<ul>
|
||||
<li>Overflow addition (&+)</li>
|
||||
<li>Overflow subtraction (&-)</li>
|
||||
<li>Overflow multiplication (&*)</li>
|
||||
<li>Overflow division (&/)</li>
|
||||
<li>Overflow remainder (&%)</li>
|
||||
</ul>
|
||||
<h2 id="value-overflow">Value Overflow</h2>
|
||||
<p>Here’s an example of what happens when an unsigned value is allowed to overflow, using the overflow addition operator (&+):</p>
|
||||
<pre><code>var willOverflow = UInt8.max
|
||||
// willOverflow equals 255, which is the largest value a UInt8 can hold
|
||||
willOverflow = willOverflow &+ 1
|
||||
// willOverflow is now equal to 0
|
||||
</code></pre><p>The variable willOverflow is initialized with the largest value a UInt8 can hold (255, or 11111111 in binary). It is then incremented by 1 using the overflow addition operator (&+). This pushes its binary representation just over the size that a UInt8 can hold, causing it to overflow beyond its bounds, as shown in the diagram below. The value that remains within the bounds of the UInt8 after the overflow addition is 00000000, or zero:</p>
|
||||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/overflowAddition_2x.png" alt="Art/overflowAddition_2x.png" title="Art/overflowAddition_2x.png">
|
||||
Value Underflow</p>
|
||||
<p>Numbers can also become too small to fit in their type’s maximum bounds. Here’s an example.</p>
|
||||
<p>The smallest value that a UInt8 can hold is 0 (which is 00000000 in eight-bit binary form). If you subtract 1 from 00000000 using the overflow subtraction operator, the number will overflow back round to 11111111, or 255 in decimal:</p>
|
||||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/overflowUnsignedSubtraction_2x.png" alt="Art/overflowUnsignedSubtraction_2x.png" title="Art/overflowUnsignedSubtraction_2x.png">
|
||||
Here’s how that looks in Swift code:</p>
|
||||
<pre><code>var willUnderflow = UInt8.min
|
||||
// willUnderflow equals 0, which is the smallest value a UInt8 can hold
|
||||
willUnderflow = willUnderflow &- 1
|
||||
// willUnderflow is now equal to 255
|
||||
</code></pre><p>A similar underflow occurs for signed integers. All subtraction for signed integers is performed as straight binary subtraction, with the sign bit included as part of the numbers being subtracted, as described in Bitwise Left and Right Shift Operators. The smallest number that an Int8 can hold is -128, which is 10000000 in binary. Subtracting 1 from this binary number with the overflow operator gives a binary value of 01111111, which toggles the sign bit and gives positive 127, the largest positive value that an Int8 can hold:</p>
|
||||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/overflowSignedSubtraction_2x.png" alt="Art/overflowSignedSubtraction_2x.png" title="Art/overflowSignedSubtraction_2x.png"></p>
|
||||
<p>Here’s the same thing in Swift code:</p>
|
||||
<pre><code>var signedUnderflow = Int8.min
|
||||
// signedUnderflow equals -128, which is the smallest value an Int8 can hold
|
||||
signedUnderflow = signedUnderflow &- 1
|
||||
// signedUnderflow is now equal to 127
|
||||
</code></pre><p>The end result of the overflow and underflow behavior described above is that for both signed and unsigned integers, overflow always wraps around from the largest valid integer value back to the smallest, and underflow always wraps around from the smallest value to the largest.</p>
|
||||
<h2 id="division-by-zero">Division by Zero</h2>
|
||||
<p>Dividing a number by zero (i / 0), or trying to calculate remainder by zero (i % 0), causes an error:</p>
|
||||
<pre><code>let x = 1
|
||||
let y = x / 0
|
||||
</code></pre><p>However, the overflow versions of these operators (&/ and &%) return a value of zero if you divide by zero:</p>
|
||||
<pre><code>let x = 1
|
||||
let y = x &/ 0
|
||||
// y is equal to 0
|
||||
</code></pre><h1 id="precedence-and-associativity">Precedence and Associativity</h1>
|
||||
<p>Operator precedence gives some operators higher priority than others; these operators are calculated first.</p>
|
||||
<p>Operator associativity defines how operators of the same precedence are grouped together (or associated)—either grouped from the left, or grouped from the right. Think of it as meaning “they associate with the expression to their left,” or “they associate with the expression to their right.”</p>
|
||||
<p>It is important to consider each operator’s precedence and associativity when working out the order in which a compound expression will be calculated. Here’s an example. Why does the following expression equal 4?</p>
|
||||
<pre><code>2 + 3 * 4 % 5
|
||||
// this equals 4
|
||||
</code></pre><p>Taken strictly from left to right, you might expect this to read as follows:</p>
|
||||
<p>2 plus 3 equals 5;
|
||||
5 times 4 equals 20;
|
||||
20 remainder 5 equals 0
|
||||
However, the actual answer is 4, not 0. Higher-precedence operators are evaluated before lower-precedence ones. In Swift, as in C, the multiplication operator (*) and the remainder operator (%) have a higher precedence than the addition operator (+). As a result, they are both evaluated before the addition is considered.</p>
|
||||
<p>However, multiplication and remainder have the same precedence as each other. To work out the exact evaluation order to use, you also need to consider their associativity. Multiplication and remainder both associate with the expression to their left. Think of this as adding implicit parentheses around these parts of the expression, starting from their left:</p>
|
||||
<pre><code>2 + ((3 * 4) % 5)
|
||||
</code></pre><p>(3 * 4) is 12, so this is equivalent to:</p>
|
||||
<pre><code>2 + (12 % 5)
|
||||
</code></pre><p>(12 % 5) is 2, so this is equivalent to:</p>
|
||||
<pre><code>2 + 2
|
||||
</code></pre><p>This calculation yields the final answer of 4.</p>
|
||||
<p>For a complete list of Swift operator precedences and associativity rules, see Expressions.</p>
|
||||
<blockquote>
|
||||
<p>NOTE</p>
|
||||
<p>Swift’s operator precedences and associativity rules are simpler and more predictable than those found in C and Objective-C. However, this means that they are not the same as in C-based languages. Be careful to ensure that operator interactions still behave in the way you intend when porting existing code to Swift.</p>
|
||||
</blockquote>
|
||||
<p>Operator Functions</p>
|
||||
<p>Classes and structures can provide their own implementations of existing operators. This is known as overloading the existing operators.</p>
|
||||
<p>The example below shows how to implement the arithmetic addition operator (+) for a custom structure. The arithmetic addition operator is a binary operator because it operates on two targets and is said to be infix because it appears in between those two targets.</p>
|
||||
<p>The example defines a Vector2D structure for a two-dimensional position vector (x, y), followed by a definition of an operator function to add together instances of the Vector2D structure:</p>
|
||||
<p>struct Vector2D {
|
||||
var x = 0.0, y = 0.0
|
||||
}
|
||||
@infix func + (left: Vector2D, right: Vector2D) -> Vector2D {
|
||||
return Vector2D(x: left.x + right.x, y: left.y + right.y)
|
||||
}
|
||||
The operator function is defined as a global function called +, which takes two input parameters of type Vector2D and returns a single output value, also of type Vector2D. You implement an infix operator by writing the @infix attribute before the func keyword when declaring the operator function.</p>
|
||||
<p>In this implementation, the input parameters are named left and right to represent the Vector2D instances that will be on the left side and right side of the + operator. The function returns a new Vector2D instance, whose x and y properties are initialized with the sum of the x and y properties from the two Vector2D instances that are added together.</p>
|
||||
<p>The function is defined globally, rather than as a method on the Vector2D structure, so that it can be used as an infix operator between existing Vector2D instances:</p>
|
||||
<p>let vector = Vector2D(x: 3.0, y: 1.0)
|
||||
let anotherVector = Vector2D(x: 2.0, y: 4.0)
|
||||
let combinedVector = vector + anotherVector
|
||||
// combinedVector is a Vector2D instance with values of (5.0, 5.0)
|
||||
This example adds together the vectors (3.0, 1.0) and (2.0, 4.0) to make the vector (5.0, 5.0), as illustrated below.</p>
|
||||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/vectorAddition_2x.png" alt="Art/vectorAddition_2x.png" title="Art/vectorAddition_2x.png">
|
||||
Prefix and Postfix Operators</p>
|
||||
<p>The example shown above demonstrates a custom implementation of a binary infix operator. Classes and structures can also provide implementations of the standard unary operators. Unary operators operate on a single target. They are prefix if they precede their target (such as -a) and postfix operators if they follow their target (such as i++).</p>
|
||||
<p>You implement a prefix or postfix unary operator by writing the @prefix or @postfix attribute before the func keyword when declaring the operator function:</p>
|
||||
<p>@prefix func - (vector: Vector2D) -> Vector2D {
|
||||
return Vector2D(x: -vector.x, y: -vector.y)
|
||||
}
|
||||
The example above implements the unary minus operator (-a) for Vector2D instances. The unary minus operator is a prefix operator, and so this function has to be qualified with the @prefix attribute.</p>
|
||||
<p>For simple numeric values, the unary minus operator converts positive numbers into their negative equivalent and vice versa. The corresponding implementation for Vector2D instances performs this operation on both the x and y properties:</p>
|
||||
<p>let positive = Vector2D(x: 3.0, y: 4.0)
|
||||
let negative = -positive
|
||||
// negative is a Vector2D instance with values of (-3.0, -4.0)
|
||||
let alsoPositive = -negative
|
||||
// alsoPositive is a Vector2D instance with values of (3.0, 4.0)
|
||||
Compound Assignment Operators</p>
|
||||
<p>Compound assignment operators combine assignment (=) with another operation. For example, the addition assignment operator (+=) combines addition and assignment into a single operation. Operator functions that implement compound assignment must be qualified with the @assignment attribute. You must also mark a compound assignment operator’s left input parameter as inout, because the parameter’s value will be modified directly from within the operator function.</p>
|
||||
<p>The example below implements an addition assignment operator function for Vector2D instances:</p>
|
||||
<p>@assignment func += (inout left: Vector2D, right: Vector2D) {
|
||||
left = left + right
|
||||
}
|
||||
Because an addition operator was defined earlier, you don’t need to reimplement the addition process here. Instead, the addition assignment operator function takes advantage of the existing addition operator function, and uses it to set the left value to be the left value plus the right value:</p>
|
||||
<p>var original = Vector2D(x: 1.0, y: 2.0)
|
||||
let vectorToAdd = Vector2D(x: 3.0, y: 4.0)
|
||||
original += vectorToAdd
|
||||
// original now has values of (4.0, 6.0)
|
||||
You can combine the @assignment attribute with either the @prefix or @postfix attribute, as in this implementation of the prefix increment operator (++a) for Vector2D instances:</p>
|
||||
<p>@prefix @assignment func ++ (inout vector: Vector2D) -> Vector2D {
|
||||
vector += Vector2D(x: 1.0, y: 1.0)
|
||||
return vector
|
||||
}
|
||||
The prefix increment operator function above takes advantage of the addition assignment operator defined earlier. It adds a Vector2D with x and y values of 1.0 to the Vector2D on which it is called, and returns the result:</p>
|
||||
<p>var toIncrement = Vector2D(x: 3.0, y: 4.0)
|
||||
let afterIncrement = ++toIncrement
|
||||
// toIncrement now has values of (4.0, 5.0)
|
||||
// afterIncrement also has values of (4.0, 5.0)
|
||||
NOTE</p>
|
||||
<p>It is not possible to overload the default assignment operator (=). Only the compound assignment operators can be overloaded. Similarly, the ternary conditional operator (a ? b : c) cannot be overloaded.</p>
|
||||
<p>Equivalence Operators</p>
|
||||
<p>Custom classes and structures do not receive a default implementation of the equivalence operators, known as the “equal to” operator (==) and “not equal to” operator (!=). It is not possible for Swift to guess what would qualify as “equal” for your own custom types, because the meaning of “equal” depends on the roles that those types play in your code.</p>
|
||||
<p>To use the equivalence operators to check for equivalence of your own custom type, provide an implementation of the operators in the same way as for other infix operators:</p>
|
||||
<p>@infix func == (left: Vector2D, right: Vector2D) -> Bool {
|
||||
return (left.x == right.x) && (left.y == right.y)
|
||||
}
|
||||
@infix func != (left: Vector2D, right: Vector2D) -> Bool {
|
||||
return !(left == right)
|
||||
}
|
||||
The above example implements an “equal to” operator (==) to check if two Vector2D instances have equivalent values. In the context of Vector2D, it makes sense to consider “equal” as meaning “both instances have the same x values and y values”, and so this is the logic used by the operator implementation. The example also implements the “not equal to” operator (!=), which simply returns the inverse of the result of the “equal to” operator.</p>
|
||||
<p>You can now use these operators to check whether two Vector2D instances are equivalent:</p>
|
||||
<p>let twoThree = Vector2D(x: 2.0, y: 3.0)
|
||||
let anotherTwoThree = Vector2D(x: 2.0, y: 3.0)
|
||||
if twoThree == anotherTwoThree {
|
||||
println("These two vectors are equivalent.")
|
||||
}
|
||||
// prints "These two vectors are equivalent."
|
||||
Custom Operators</p>
|
||||
<p>You can declare and implement your own custom operators in addition to the standard operators provided by Swift. Custom operators can be defined only with the characters / = - + * % < > ! & | ^ . ~.</p>
|
||||
<p>New operators are declared at a global level using the operator keyword, and can be declared as prefix, infix or postfix:</p>
|
||||
<p>operator prefix +++ {}
|
||||
The example above defines a new prefix operator called +++. This operator does not have an existing meaning in Swift, and so it is given its own custom meaning below in the specific context of working with Vector2D instances. For the purposes of this example, +++ is treated as a new “prefix doubling incrementer” operator. It doubles the x and y values of a Vector2D instance, by adding the vector to itself with the addition assignment operator defined earlier:</p>
|
||||
<p>@prefix @assignment func +++ (inout vector: Vector2D) -> Vector2D {
|
||||
vector += vector
|
||||
return vector
|
||||
}
|
||||
This implementation of +++ is very similar to the implementation of ++ for Vector2D, except that this operator function adds the vector to itself, rather than adding Vector2D(1.0, 1.0):</p>
|
||||
<p>var toBeDoubled = Vector2D(x: 1.0, y: 4.0)
|
||||
let afterDoubling = +++toBeDoubled
|
||||
// toBeDoubled now has values of (2.0, 8.0)
|
||||
// afterDoubling also has values of (2.0, 8.0)
|
||||
Precedence and Associativity for Custom Infix Operators</p>
|
||||
<p>Custom infix operators can also specify a precedence and an associativity. See Precedence and Associativity for an explanation of how these two characteristics affect an infix operator’s interaction with other infix operators.</p>
|
||||
<p>The possible values for associativity are left, right, and none. Left-associative operators associate to the left if written next to other left-associative operators of the same precedence. Similarly, right-associative operators associate to the right if written next to other right-associative operators of the same precedence. Non-associative operators cannot be written next to other operators with the same precedence.</p>
|
||||
<p>The associativity value defaults to none if it is not specified. The precedence value defaults to 100 if it is not specified.</p>
|
||||
<p>The following example defines a new custom infix operator called +-, with left associativity and a precedence of 140:</p>
|
||||
<p>operator infix +- { associativity left precedence 140 }
|
||||
func +- (left: Vector2D, right: Vector2D) -> Vector2D {
|
||||
return Vector2D(x: left.x + right.x, y: left.y - right.y)
|
||||
}
|
||||
let firstVector = Vector2D(x: 1.0, y: 2.0)
|
||||
let secondVector = Vector2D(x: 3.0, y: 4.0)
|
||||
let plusMinusVector = firstVector +- secondVector
|
||||
// plusMinusVector is a Vector2D instance with values of (4.0, -2.0)
|
||||
This operator adds together the x values of two vectors, and subtracts the y value of the second vector from the first. Because it is in essence an “additive” operator, it has been given the same associativity and precedence values (left and 140) as default additive infix operators such as + and -. For a complete list of the default Swift operator precedence and associativity settings, see Expressions.</p>
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="2" data-basepath=".." data-revision="1402387034522">
|
||||
<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_431">
|
||||
<section class="normal" id="section-gitbook_49">
|
||||
|
||||
<h1 id="swift-">Swift 教程</h1>
|
||||
<p>本章介绍了 Swift 的各种特性及其使用方法,是全书的核心部分。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.1" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="3.1" data-basepath=".." data-revision="1402387034522">
|
||||
<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_433">
|
||||
<section class="normal" id="section-gitbook_51">
|
||||
|
||||
<h1 id="-">关于语言附注</h1>
|
||||
<p>本书的这一节描述了Swift编程语言的形式语法。这里描述的语法是为了帮助您更详细的了解该语言,而不是让您直接实现一个解析器或编译器。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.2" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="3.2" data-basepath=".." data-revision="1402387034522">
|
||||
<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_439">
|
||||
<section class="normal" id="section-gitbook_57">
|
||||
|
||||
<h1 id="-">语法结构</h1>
|
||||
<p>本页包含内容:</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.3" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="3.3" data-basepath=".." data-revision="1402387034522">
|
||||
<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_434">
|
||||
<section class="normal" id="section-gitbook_52">
|
||||
|
||||
<h1 id="-types-">类型(Types)</h1>
|
||||
<hr>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.4" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="3.4" data-basepath=".." data-revision="1402387034522">
|
||||
<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_436">
|
||||
<section class="normal" id="section-gitbook_54">
|
||||
|
||||
<h1 id="-expressions-">表达式(Expressions)</h1>
|
||||
<hr>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.6" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="3.6" data-basepath=".." data-revision="1402387034522">
|
||||
<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>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.7" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="3.7" data-basepath=".." data-revision="1402387034522">
|
||||
<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_432">
|
||||
<section class="normal" id="section-gitbook_50">
|
||||
|
||||
<h1 id="-">特性</h1>
|
||||
<p>特性提供了关于声明和类型的更多信息。在Swift中有两类特性,用于修饰声明的以及用于修饰类型的。例如,<code>required</code>特性,当应用于一个类的指定或便利初始化器声明时,表明它的每个子类都必须实现那个初始化器。再比如<code>noreturn</code>特性,当应用于函数或方法类型时,表明该函数或方法不会返回到它的调用者。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.8" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="3.8" data-basepath=".." data-revision="1402387034522">
|
||||
<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_440">
|
||||
<section class="normal" id="section-gitbook_58">
|
||||
|
||||
<h1 id="-patterns-">模式(Patterns)</h1>
|
||||
<p>模式(pattern)代表了单个值或者复合值的结构。例如,元组<code>(1, 2)</code>的结构是逗号分隔的,包含两个元素的列表。因为模式代表一种值的结构,而不是特定的某个值,你可以把模式和各种同类型的值匹配起来。比如,<code>(x, y)</code>可以匹配元组<code>(1, 2)</code>,以及任何含两个元素的元组。除了将模式与一个值匹配外,你可以从合成值中提取出部分或全部,然后分别把各个部分和一个常量或变量绑定起来。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.9" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="3.9" data-basepath=".." data-revision="1402387034522">
|
||||
<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,19 +587,19 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_441">
|
||||
<section class="normal" id="section-gitbook_59">
|
||||
|
||||
<h1 id="-">泛型参数</h1>
|
||||
<hr>
|
||||
<p>本页包含内容:</p>
|
||||
<ul>
|
||||
<li><a href="#label1">泛型形参语句</a></li>
|
||||
<li><a href="#label2">泛型实参语句</a></li>
|
||||
<li><a href="#generic_parameter">泛型形参语句</a></li>
|
||||
<li><a href="#generic_argument">泛型实参语句</a></li>
|
||||
</ul>
|
||||
<p>本节涉及泛型类型、泛型函数以及泛型构造器的参数,包括形参和实参。声明泛型类型、函数或构造器时,须指定相应的类型参数。类型参数相当于一个占位符,当实例化泛型类型、调用泛型函数或泛型构造器时,就用具体的类型实参替代之。</p>
|
||||
<p>关于 Swift 语言的泛型概述,见<a href="../charpter2/22_Generics.html">泛型</a>(第二部分第22章)。</p>
|
||||
<h2 id="-a-name-label1-a-"><a name="label1">泛型实参语句</a></h2>
|
||||
<p>泛型形参语句指定泛型类型或函数的类型形参,以及这些参数的关联约束和要求。泛型形参语句用尖括号(<>)包住,并且有以下两种声明形式:</p>
|
||||
<h2 id="-a-name-generic_parameter-a-"><a name="generic_parameter">泛型形参语句</a></h2>
|
||||
<p>泛型形参语句指定泛型类型或函数的类型形参,以及这些参数的关联约束和要求。泛型形参语句用尖括号(<>)包住,并且有以下两种形式:</p>
|
||||
<pre><code><generic parameter list>
|
||||
<generic parameter list where requirements >
|
||||
</code></pre><p>泛型形参列表中泛型形参用逗号分开,每一个采用以下形式:</p>
|
||||
@ -637,8 +637,8 @@ simpleMin(3.14159, 2.71828) // T is inferred to be Double
|
||||
<p>conformance-requirement → type-identifier:protocol-composition-type</p>
|
||||
<p>same-type-requirement → type-identifier==type-identifier</p>
|
||||
</blockquote>
|
||||
<h2 id="-a-name-label2-a-"><a name="label2">泛型实参语句</a></h2>
|
||||
<p>泛型实参语句指定泛型类型的类型实参。泛型实参语句用尖括号(<>)包住,并且按如下形式声明:</p>
|
||||
<h2 id="-a-name-generic_argument-a-"><a name="generic_argument">泛型实参语句</a></h2>
|
||||
<p>泛型实参语句指定<em>泛型类型</em>的类型实参。泛型实参语句用尖括号(<>)包住,形式如下:</p>
|
||||
<pre><code>< generic argument list >
|
||||
</code></pre><p>泛型实参列表中类型实参有逗号分开。类型实参是实际具体类型的名字,用来替代泛型类型的泛型形参语句中的相应的类型形参。从而得到泛型类型的一个特化版本。如,Swift标准库的泛型字典类型定义如下:</p>
|
||||
<pre><code> struct Dictionary<KeyTypel: Hashable, ValueType>: Collection,
|
||||
@ -648,13 +648,16 @@ DictionaryLiteralConvertible {
|
||||
</code></pre><p>泛型<code>Dictionary</code>类型的特化版本,<code>Dictionary<String, Int></code>就是用具体的<code>String</code>和<code>Int</code>类型替代泛型类型<code>KeyType: Hashable</code>和<code>ValueType</code>产生的。每一个类型实参必须满足它所替代的泛型形参的所有约束,包括任何<code>where</code>语句所指定的额外的要求。上面的例子中,类型形参<code>KeyType</code>要求满足<code>Hashable</code>协议,因此<code>String</code>也必须满足<code>Hashable</code>协议。</p>
|
||||
<p>可以用本身就是泛型类型的特化版本的类型实参替代类型形参(假设已满足合适的约束和要求)。例如,为了生成一个元素类型是整型数组的数组,可以用数组的特化版本<code>Array<Int></code>替代泛型类型<code>Array<T></code>的类型形参<code>T</code>来实现。</p>
|
||||
<pre><code>let arrayOfArrays: Array<Array<Int>> = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
|
||||
</code></pre><p>同<a href="#label1">泛型形参语句</a>一样,不能用泛型实参语句来指定泛型函数或构造器的类型实参。</p>
|
||||
</code></pre><p>如<a href="#generic_parameter">泛型形参语句</a>所述,不能用泛型实参语句来指定泛型函数或构造器的类型实参。</p>
|
||||
<blockquote>
|
||||
<p>Grammar of a generic argument clause</p>
|
||||
<p>generic-argument-clause → <generic-argument-list></p>
|
||||
<p>generic-argument-list → generic-argument generic-argument,generic-argument-list</p>
|
||||
<p>generic-argument → type</p>
|
||||
</blockquote>
|
||||
<p>==============================================
|
||||
上篇:<a href="07_Patterns.html">模式</a></p>
|
||||
<p>下篇:<a href="09_Summary_of_the_Grammar.html">语法总结</a></p>
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
@ -44,7 +44,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.10" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="3.10" data-basepath=".." data-revision="1402387034522">
|
||||
<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_443">
|
||||
<section class="normal" id="section-gitbook_63">
|
||||
|
||||
<h1 id="-">语法总结</h1>
|
||||
<p>本页包含内容:</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3.5" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="3.5" data-basepath=".." data-revision="1402387034522">
|
||||
<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_444">
|
||||
<section class="normal" id="section-gitbook_61">
|
||||
|
||||
<h1 id="-">语句</h1>
|
||||
<p>在 Swift 中,有两种类型的语句:简单语句和控制流语句。简单语句是最常见的,用于构造表达式和声明。控制流语句则用于控制程序执行的流程,Swift 中有三种类型的控制流语句:循环语句、分支语句和控制传递语句。</p>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="3" data-basepath=".." data-revision="1402386668059">
|
||||
<div class="book" data-level="3" data-basepath=".." data-revision="1402387034522">
|
||||
<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>
|
||||
|
||||
@ -44,7 +44,7 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="0" data-basepath="." data-revision="1402386668059">
|
||||
<div class="book" data-level="0" data-basepath="." data-revision="1402387034522">
|
||||
<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_385">
|
||||
<section class="normal" id="section-gitbook_3">
|
||||
|
||||
<h1 id="swift-">Swift 编程语言</h1>
|
||||
<p>Swift 是苹果在 WWDC 2014 上发布的一款全新的编程语言,本书译自苹果官方的 Swift 教程《The Swift Programming Language》。</p>
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
CACHE MANIFEST
|
||||
# Revision 1402386668060
|
||||
# Revision 1402387034523
|
||||
|
||||
CACHE:
|
||||
index.html
|
||||
@ -26,8 +26,8 @@ chapter2/17_Optional_Chaining.html
|
||||
chapter2/18_Type_Casting.html
|
||||
chapter2/19_Nested_Types.html
|
||||
chapter2/20_Extensions.html
|
||||
chapter2/22_Generics.html
|
||||
chapter2/21_Protocols.html
|
||||
chapter2/22_Generics.html
|
||||
chapter2/23_Advanced_Operators.html
|
||||
chapter2/chapter2.html
|
||||
chapter3/06_Attributes.html
|
||||
@ -38,9 +38,9 @@ chapter3/05_Declarations.html
|
||||
chapter3/02_Lexical_Structure.html
|
||||
chapter3/07_Patterns.html
|
||||
chapter3/08_Generic_Parameters_and_Arguments.html
|
||||
chapter3/09_Summary_of_the_Grammar.html
|
||||
chapter3/10_Statements.html
|
||||
chapter3/chapter3.html
|
||||
chapter3/09_Summary_of_the_Grammar.html
|
||||
gitbook/app.js
|
||||
gitbook/fonts/anonymouspro/400.woff
|
||||
gitbook/fonts/anonymouspro/400i.woff
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user