按校对规则大致修改
This commit is contained in:
@ -1,28 +1,38 @@
|
||||
# 基本运算符
|
||||
-----------------
|
||||
|
||||
本页包含内容:
|
||||
|
||||
- [术语](#terminology)
|
||||
- [赋值运算符](#assignment_operator)
|
||||
- [数值运算符](#arithmetic_operators)
|
||||
- [组合赋值运算符(Compound Assignment Operators)](#compound_assignment_operators)
|
||||
- [比较运算符](#comparison_operators)
|
||||
- [三目条件运算符(Ternary Conditional Operator)](#ternary_conditional_operator)
|
||||
- [区间运算符](#range_operators)
|
||||
- [逻辑运算符](#logical_operators)
|
||||
|
||||
运算符是检查,改变,合并值的特殊符号或短语。例如,加号`+`把计算两个数的和(如 `let i = 1 + 2`)。复杂些的运行算包括逻辑与`&&`(如 `if enteredDoorCode && passedRetinaScan`),还有自增运算符`++i`这样让自身加一的便捷运算。
|
||||
|
||||
Swift支持大部分标准C语言的运算符,且改进许多特性来减少常规编码错误。如,赋值符`=`不返回值,以防止错把等号`==`写成赋值号`=`而导致Bug。数值运算符(`+`,`-`,`*`,`/`,`%`等)会检测并不允许值溢出,以此来避免保存变量时由于变量大于或小于其类型所能承载的范围时导致的异常结果。当然允许你选择使用Swift的溢出运算符来玩溢出。具体使用请移步[溢出运算符](Overflow Operators)。
|
||||
Swift支持大部分标准C语言的运算符,且改进许多特性来减少常规编码错误。如,赋值符`=`不返回值,以防止错把等号`==`写成赋值号`=`而导致Bug。数值运算符(`+`,`-`,`*`,`/`,`%`等)会检测并不允许值溢出,以此来避免保存变量时由于变量大于或小于其类型所能承载的范围时导致的异常结果。当然允许你选择使用Swift的溢出运算符来玩溢出。具体使用请移步[溢出运算符](23_Advanced_Operators.html#overflow_operators)。
|
||||
|
||||
区别于C语言,在Swift中你可以对浮点数进行取余运算(`%`),还提供了C语言没有的表达两数之间的值的区间运算符,(`a..b`和`a...b`),这方便我们表达一个区间内的数值。
|
||||
|
||||
本章节只描述了Swift中的基本运算符,[高级运算符](http://#)包含了高级运算符,及如何自定义运算符,及如何进行自定义类型的运算符重载。
|
||||
本章节只描述了Swift中的基本运算符,[高级运算符](23_Advanced_Operators.html)包含了高级运算符,及如何自定义运算符,及如何进行自定义类型的运算符重载。
|
||||
|
||||
# 术语
|
||||
<a name="terminology"></a>
|
||||
## 术语
|
||||
|
||||
运算符有一目,双目和三目运算符。
|
||||
|
||||
一目运算符对单一操作对象操作,如`-a`。
|
||||
- 一目运算符对单一操作对象操作,如`-a`。一目运算符分前置符和后置运算符,前置运算符需紧排操作对象之前,如`!b`,后置运算符需紧跟操作对象之后,如`i++`,
|
||||
- 双目运算符操作两个操作对象,如 `2 + 3`。是中置的,因为它们出现在两个操作对象之间。
|
||||
- 三目运算符操作三个操作对象,和C语言一样,Swift只有一个三目运算符,就是三目条件运算符 `a?b:c`。
|
||||
|
||||
一目运算符分前置符和后置运算符,前置运算符需紧排操作对象之前,如`!b`,后置运算符需紧跟操作对象之后,如`i++`,
|
||||
受运算符影响的值叫操作数,在表达式`1 + 2`中,加号`+`是双目运算符,它的两个操作数是值`1`和`2`。
|
||||
|
||||
双目运算符操作两个操作对象,如 `2 + 3`。是中置的,因为它们出现在两个操作对象之间。
|
||||
|
||||
三目运算符操作三个操作对象,和C语言一样,Swift只有一个三目运算符,就是三目条件运算符 `a?b:c`。
|
||||
|
||||
受运算符影响的值叫操作数,在表达式 `1 + 2` 中,加号`+`是双目运算符,它的两个操作数是值`1`和`2`。
|
||||
|
||||
# 赋值运算符
|
||||
<a name="assignment_operator"></a>
|
||||
## 赋值运算符
|
||||
|
||||
赋值运算 `a = b`,表示用`b`的值来初始化或更新`a`的值。
|
||||
|
||||
@ -48,7 +58,8 @@ if x = y {
|
||||
|
||||
这个特性使得你不无法把`==`错写成`=`了,由于`if x = y`是错误代码,Swift从底层帮你避免了这些代码错误。
|
||||
|
||||
# 数值运算
|
||||
<a name="arithmetic_operators"></a>
|
||||
## 数值运算
|
||||
|
||||
Swift让所有数值类型都支持了基本的四则运算:
|
||||
|
||||
@ -64,7 +75,7 @@ Swift让所有数值类型都支持了基本的四则运算:
|
||||
10.0 / 2.5 // 等于 4.0
|
||||
```
|
||||
|
||||
与C语言和Objective-C不同的是,Swift默认不允许在数值运算中出现溢出情况。但你可以使用Swift的溢出运算符来达到你有目的的溢出,(如 `a &+ b`)。详情请移步:[溢出运算符](Overflow Operators)。
|
||||
与C语言和Objective-C不同的是,Swift默认不允许在数值运算中出现溢出情况。但你可以使用Swift的溢出运算符来达到你有目的的溢出,(如 `a &+ b`)。详情请移步:[溢出运算符](23_Advanced_Operators.html#overflow_operators)。
|
||||
|
||||
加法操作`+`也用于字符串的拼接:
|
||||
|
||||
@ -81,38 +92,21 @@ let dogCow = dog + cow
|
||||
// 译者注: 原谅的引号内是很可爱的小狗和小牛, 但win os下不支持表情字符, 所以改成了普通字符
|
||||
// dogCow 现在是 "dc"
|
||||
```
|
||||
详细请点击[字符,字符串的拼接](http://#)。
|
||||
详细请点击[字符,字符串的拼接](03_Strings_and_Characters.html#concatenating_strings_and_characters)。
|
||||
|
||||
# 求余运算
|
||||
### 求余运算
|
||||
|
||||
求余运算 `a % b` 是计算`b`的多少倍刚刚好可以容入`a`,多出来的那部分叫余数。
|
||||
|
||||
> 注意
|
||||
>注意:
|
||||
求余运算(%)在其他语言也叫取模运算。然而严格说来,我们看该运算符对负数的操作结果,"求余"比"取模"更合适些。
|
||||
|
||||
> 求余运算(%)在其他语言也叫取模运算。然而严格说来,我们看该运算符对负数的操作结果,`求余` 比 `取模` 更合适些。
|
||||
我们来谈谈取余是怎么回事,计算`9 % 4`,你先计算出4的多少倍会刚好可以容入`9`中。
|
||||
|
||||
我们来谈谈取余是怎么回事,计算 `9 % 4`,你先计算出4的多少倍会刚好可以容入`9`中。
|
||||
2倍,非常好,那余数是1(用橙色标出)
|
||||
|
||||
2倍,非常好,那余数是1(用'*'标出)
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<td>1 </td>
|
||||
<td>2 </td>
|
||||
<td>3 </td>
|
||||
<td>4 </td>
|
||||
<td>5 </td>
|
||||
<td>6 </td>
|
||||
<td>7 </td>
|
||||
<td>8 </td>
|
||||
<td>9 </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="4">4</td>
|
||||
<td colspan="4">4</td>
|
||||
<td>1*</td>
|
||||
</tr>
|
||||
</table>
|
||||

|
||||
|
||||
在Swift中这么来表达
|
||||
|
||||
@ -121,9 +115,9 @@ let dogCow = dog + cow
|
||||
```
|
||||
|
||||
为了得到 `a % b` 的结果,`%`计算了以下等式,并输出`余数`作为结果:
|
||||
```
|
||||
a = (b × 倍数) + 余数
|
||||
```
|
||||
|
||||
*a = (b × 倍数) + 余数*
|
||||
|
||||
当`倍数`取最大值的时候,就会刚好可以容入`a`中。
|
||||
|
||||
把`9`和`4`代入等式中,我们得`1`:
|
||||
@ -147,7 +141,7 @@ a = (b × 倍数) + 余数
|
||||
|
||||
在对负数`-b`求余时,`-b`的符号会被忽略。这意味着 `a % b` 和 `a % -b`的结果是相同的。
|
||||
|
||||
## 浮点数求余计算
|
||||
### 浮点数求余计算
|
||||
|
||||
不同于C和Objective-C,Swift中是可以对浮点数进行求余的。
|
||||
|
||||
@ -155,9 +149,11 @@ a = (b × 倍数) + 余数
|
||||
8 % 2.5 // 等于 0.5
|
||||
```
|
||||
|
||||
这个例子中,8除于2。5等于3余0。5,所以结果是0。5。
|
||||
这个例子中,8除于2.5等于3余0.5,所以结果是0.5。
|
||||
|
||||
# 自增和自增运算
|
||||

|
||||
|
||||
### 自增和自增运算
|
||||
|
||||
和C一样,Swift也提供了方便对变量本身加1或减1的自增`++`和自减`--`的运算符。其操作对象可以是整形和浮点型。
|
||||
|
||||
@ -192,7 +188,7 @@ let c = a++ // a 现在 2, 但 c 是 a 自增前的值 1
|
||||
除非你需要使用`i++`的特性,不然推荐你使用`++i`和`--i`,因为先修改后返回这样的行为更符合我们的逻辑。
|
||||
|
||||
|
||||
# 单目负号
|
||||
### 单目负号
|
||||
|
||||
数值的正负号可以使用前缀`-`(即单目负号)来切换:
|
||||
|
||||
@ -204,7 +200,7 @@ let plusThree = -minusThree // plusThree 等于 3, 或 "负负3"
|
||||
|
||||
单目负号写在操作数之前,中间没有空格。
|
||||
|
||||
# 单目正号
|
||||
### 单目正号
|
||||
|
||||
单目正号`+`不做任何改变地返回操作数的值。
|
||||
|
||||
@ -215,7 +211,8 @@ let alsoMinusSix = +minusSix // alsoMinusSix 等于 -6
|
||||
虽然单目`+`做无用功,但当你在使用单目负号来表达负数时,你可以使用单目正号来表达正数,如此你的代码会具有对称美。
|
||||
|
||||
|
||||
# 复合赋值
|
||||
<a name="compound_assignment_operators"></a>
|
||||
## 复合赋值
|
||||
|
||||
如同强大的C语言,Swift也提供把其他运算符和赋值运算`=`组合的复合赋值运算符,加赋运算`+=`是其中一个例子:
|
||||
|
||||
@ -226,13 +223,13 @@ a += 2 // a 现在是 3
|
||||
|
||||
表达式 `a += 2` 是 `a = a + 2` 的简写,一个加赋运算就把加法和赋值两件事完成了。
|
||||
|
||||
> 注意:
|
||||
>注意:
|
||||
复合赋值运算没有返回值,`let b = a += 2` 这类代码是错误。这不同于上面提到的自增和自减运算符。
|
||||
|
||||
> 复合赋值运算没有返回值,`let b = a += 2` 这类代码是错误。这不同于上面提到的自增和自减运算符。
|
||||
|
||||
[表达式](http://#)里有复合运算符的完整列表。
|
||||
在[表达式](../chapter3/04_Expressions.html)章节里有复合运算符的完整列表。
|
||||
|
||||
# 比较运算
|
||||
<a name="comparison_operators"></a>
|
||||
## 比较运算
|
||||
|
||||
所有标准C中的比较运算都可以在Swift中使用。
|
||||
|
||||
@ -244,8 +241,7 @@ a += 2 // a 现在是 3
|
||||
- 小于等于 `a <= b`
|
||||
|
||||
> 注意:
|
||||
|
||||
> Swift也提供恒等`===`和不恒等`!==`这两个比较符来判断两个对象是否引用同一个对象实例。更多细节在[类与结构](Classes and Structures)。
|
||||
Swift也提供恒等`===`和不恒等`!==`这两个比较符来判断两个对象是否引用同一个对象实例。更多细节在[类与结构](09_Classes_and_Structures.html)。
|
||||
|
||||
每个比较运算都返回了一个标识表达式是否成立的布尔值:
|
||||
|
||||
@ -270,9 +266,10 @@ if name == "world" {
|
||||
// 输出 "hello, world", 因为 `name` 就是等于 "world"
|
||||
```
|
||||
|
||||
关于`if`语句,请看[控制流](Control Flow)。
|
||||
关于`if`语句,请看[控制流](05_Control_Flow.html)。
|
||||
|
||||
# 三目条件运算
|
||||
<a name="ternary_conditional_operator"></a>
|
||||
## 三目条件运算
|
||||
|
||||
三目条件运算的特殊在于它是有三个操作数的运算符,它的原型是 `问题?答案1:答案2`。它简洁地表达根据 `问题` 成立与否作出二选一的操作。如果 `问题` 成立,返回 `答案1` 的结果; 如果不成立,返回 `答案2` 的结果。
|
||||
|
||||
@ -314,11 +311,12 @@ if hasHeader {
|
||||
|
||||
三目条件运算提供有效率且便捷的方式来表达二选一的选择。需要注意的事,过度使用三目条件运算就会由简洁的代码变成难懂的代码。我们应避免在一个组合语句使用多个三目条件运算符。
|
||||
|
||||
# 区间运算符
|
||||
<a name="range_operators"></a>
|
||||
## 区间运算符
|
||||
|
||||
Swift提供了两个方便表达一个区间的值的运算符。
|
||||
|
||||
## 闭区间运算符
|
||||
### 闭区间运算符
|
||||
闭区间运算符`a...b`定义一个包含从`a`到`b`(包括`a`和`b`)的所有值的区间。
|
||||
|
||||
闭区间运算符在迭代一个区间的所有值时是非常有用的,如在`for-in`循环中:
|
||||
@ -334,9 +332,9 @@ for index in 1...5 {
|
||||
// 5 * 5 = 25
|
||||
```
|
||||
|
||||
关于`for-in`,请看[控制流](Control Flow)。
|
||||
关于`for-in`,请看[控制流](05_Control_Flow.html)。
|
||||
|
||||
## 半闭区间
|
||||
### 半闭区间
|
||||
|
||||
半闭区间`a..b`定义一个从`a`到`b`但不包括`b`的区间。
|
||||
之所以称为半闭区间,是因为该区间包含第一个值而不包括最后的值。
|
||||
@ -355,9 +353,11 @@ for i in 0..count {
|
||||
// 第 4 个人叫 Jack
|
||||
```
|
||||
|
||||
> 注意:数组有4个元素,但`0..count`只数到 3(最后一个元素的下标),因为它是半闭区间。关于数组,请查阅[数组](Arrays)。
|
||||
> 注意:
|
||||
数组有4个元素,但`0..count`只数到 3(最后一个元素的下标),因为它是半闭区间。关于数组,请查阅[数组](04_Collection_Types.html#arrays)。
|
||||
|
||||
# 逻辑运算
|
||||
<a name="logical_operators"></a>
|
||||
## 逻辑运算
|
||||
|
||||
逻辑运算的操作对象是逻辑布尔值。Swift支持基于C语言的三个标准逻辑运算。
|
||||
|
||||
@ -365,7 +365,7 @@ for i in 0..count {
|
||||
- 逻辑与 `a && b`
|
||||
- 逻辑或 `a || b`
|
||||
|
||||
## 逻辑非
|
||||
### 逻辑非
|
||||
|
||||
逻辑非运算`!a`对一个布尔值取反,使得`true`变`false`,`false`变`true`。
|
||||
|
||||
@ -383,7 +383,7 @@ if !allowedEntry {
|
||||
|
||||
在示例代码中,小心地选择布尔常量或变量有助于代码的可读性,并且避免使用双重逻辑非运算,或混乱的逻辑语句。
|
||||
|
||||
## 逻辑与
|
||||
### 逻辑与
|
||||
逻辑与 `a && b` 表达了只有`a`和`b`的值都为`true`时,整个表达式的值才会是`true`。
|
||||
|
||||
只要任意一个值为`false`,整个表达式的值就为`false`。事实上,如果第一个值为`false`,那么是不去计算第二个值的,因为它已经不可能影响整个表达式的结果了。这被称做 "短路计算"。
|
||||
@ -401,7 +401,7 @@ if enteredDoorCode && passedRetinaScan {
|
||||
// 输出 "ACCESS DENIED
|
||||
```
|
||||
|
||||
## 逻辑或
|
||||
### 逻辑或
|
||||
逻辑或 `a || b` 是一个由两个连续的`|`组成的中置运算符。它表示了两个逻辑表达式的其中一个为`true`,整个表达式就为`true`。
|
||||
|
||||
同逻辑与运算类似,逻辑或也是"短路计算"的,当左端的表达式为`true`时,将不计算右边的表达式了,因为它不可能改变整个表达式的值了。
|
||||
@ -419,7 +419,7 @@ if hasDoorKey || knowsOverridePassword {
|
||||
// 输出 "Welcome!"
|
||||
```
|
||||
|
||||
## 组合逻辑
|
||||
### 组合逻辑
|
||||
|
||||
我们可以组合多个逻辑运算来表达一个复合逻辑:
|
||||
|
||||
@ -438,7 +438,7 @@ if enteredDoorCode && passedRetinaScan || hasDoorKey || knowsOverridePassword {
|
||||
|
||||
前两种情况,我们都不满足,所以前两个简单逻辑的结果是`false`,但是我们是知道紧急情况下重置的密码的,所以整个复杂表达式的值还是`true`。
|
||||
|
||||
## 使用括号来明确优先级
|
||||
### 使用括号来明确优先级
|
||||
|
||||
为了一个复杂表达式更容易读懂,在合适的地方使用括号来明确优先级是很有效的,虽然它并非必要的。在上个关于门的权限的例子中,我们给第一个部分加个括号,使用它看起来逻辑更明确。
|
||||
|
||||
|
||||
@ -1,6 +1,17 @@
|
||||
# 高级运算符
|
||||
-----------------
|
||||
|
||||
本页内容包括:
|
||||
|
||||
- [位运算符](#bitwise_operators)
|
||||
- [溢出运算符](#overflow_operators)
|
||||
- [优先级和结合性(Precedence and Associativity)](#precedence_and_associativity)
|
||||
- [运算符函数(Operator Functions)](#operator_functions)
|
||||
- [自定义运算符](#custom_operators)
|
||||
|
||||
In addition to the operators described in [Basic Operators](Basic Operators), 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.
|
||||
|
||||
除了[基本操作符](Basic Operators)中所讲的运算符,Swift还有许多复杂的高级运算符,包括了C语和Objective-C中的位运算符和移位运算。
|
||||
除了[基本操作符](02_Basic_Operators.html)中所讲的运算符,Swift还有许多复杂的高级运算符,包括了C语和Objective-C中的位运算符和移位运算。
|
||||
|
||||
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 (&).
|
||||
|
||||
@ -14,9 +25,10 @@ You’re not just limited to the predefined operators. Swift gives you the freed
|
||||
|
||||
可定制的运算符并不限于那些预设的运算符,自定义有个性的中置,前置,后置及赋值运算符,当然还有优先级和结合性。这些运算符的实现可以运用预设的运算符,也可以运用之前定制的运算符。
|
||||
|
||||
# Bitwise Operators
|
||||
## Bitwise Operators
|
||||
|
||||
# 位运算符
|
||||
<a name="bitwise_operators"></a>
|
||||
## 位运算符
|
||||
|
||||
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.
|
||||
|
||||
@ -26,9 +38,9 @@ Swift supports all of the bitwise operators found in C, as described below.
|
||||
|
||||
Swift支持如下所有C语言的位运算符:
|
||||
|
||||
## Bitwise NOT Operator
|
||||
### Bitwise NOT Operator
|
||||
|
||||
## 按位取反运算符
|
||||
### 按位取反运算符
|
||||
|
||||
The bitwise NOT operator (~) inverts all bits in a number:
|
||||
|
||||
@ -53,9 +65,9 @@ The bitwise NOT operator is then used to create a new constant called invertedBi
|
||||
|
||||
使用按位取反运算`~`对`initialBits`操作,然后赋值给`invertedBits`这个新常量。这个新常量的值等于所有位都取反的`initialBits`,即`1`变成`0`,`0`变成`1`,变成了`11110000`,十进制值为`240`。
|
||||
|
||||
## Bitwise AND Operator
|
||||
### Bitwise AND Operator
|
||||
|
||||
## 按位与运算符
|
||||
### 按位与运算符
|
||||
|
||||
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:
|
||||
|
||||
@ -73,9 +85,9 @@ let lastSixBits: UInt8 = 0b00111111
|
||||
let middleFourBits = firstSixBits & lastSixBits // 等于 00111100
|
||||
```
|
||||
|
||||
## Bitwise OR Operator
|
||||
### Bitwise OR Operator
|
||||
|
||||
## 按位或运算
|
||||
### 按位或运算
|
||||
|
||||
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:
|
||||
|
||||
@ -93,9 +105,9 @@ let moreBits: UInt8 = 0b01011110
|
||||
let combinedbits = someBits | moreBits // 等于 11111110
|
||||
```
|
||||
|
||||
## Bitwise XOR Operator
|
||||
### Bitwise XOR Operator
|
||||
|
||||
## 按位异或运算符
|
||||
### 按位异或运算符
|
||||
|
||||
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:
|
||||
|
||||
@ -113,8 +125,8 @@ let otherBits: UInt8 = 0b00000101
|
||||
let outputBits = firstBits ^ otherBits // 等于 00010001
|
||||
```
|
||||
|
||||
## Bitwise Left and Right Shift Operators
|
||||
## 按位左移/右移运算符
|
||||
### Bitwise Left and Right Shift Operators
|
||||
### 按位左移/右移运算符
|
||||
|
||||
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.
|
||||
|
||||
@ -124,8 +136,8 @@ Bitwise left and right shifts have the effect of multiplying or dividing an inte
|
||||
|
||||
按位左移和按位右移的效果相当把一个整数乘于或除于一个因子为`2`的整数。向左移动一个整型的比特位相当于把这个数乘于`2`,向右移一位就是除于`2`。
|
||||
|
||||
### Shifting Behavior for Unsigned Integers
|
||||
### 无符整型的移位操作
|
||||
#### Shifting Behavior for Unsigned Integers
|
||||
#### 无符整型的移位操作
|
||||
|
||||
The bit-shifting behavior for unsigned integers is as follows:
|
||||
对无符整型的移位的效果如下:
|
||||
@ -183,8 +195,8 @@ Finally, the blue component is obtained by performing a bitwise AND between the
|
||||
|
||||
最后,蓝色部分对`0xCC6699`和`0x0000FF`进行按位与运算,得到`0x000099`,无需向右移位了,所以结果就是`0x99`,即十进制的`153`。
|
||||
|
||||
### Shifting Behavior for Signed Integers
|
||||
### 有符整型的移位操作
|
||||
#### Shifting Behavior for Signed Integers
|
||||
#### 有符整型的移位操作
|
||||
|
||||
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.)
|
||||
|
||||
@ -249,8 +261,10 @@ Because of the special way that positive and negative numbers are stored, shifti
|
||||
|
||||
正因为正数和负数特殊的存储方式,向右移位使它接近于`0`。移位过程中保持符号会不变,负数在接近`0`的过程中一直是负数。
|
||||
|
||||
# Overflow Operators
|
||||
# 溢出运算符
|
||||
## Overflow Operators
|
||||
|
||||
<a name="overflow_operators"></a>
|
||||
## 溢出运算符
|
||||
|
||||
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.
|
||||
|
||||
@ -281,8 +295,8 @@ However, when you specifically want an overflow condition to truncate the number
|
||||
- 溢出除法 `&/`
|
||||
- 溢出求余 `&%`
|
||||
|
||||
## Value Overflow
|
||||
## 值的上溢出
|
||||
### Value Overflow
|
||||
### 值的上溢出
|
||||
|
||||
Here’s an example of what happens when an unsigned value is allowed to overflow, using the overflow addition operator (&+):
|
||||
|
||||
@ -301,8 +315,8 @@ The variable willOverflow is initialized with the largest value a UInt8 can hold
|
||||
|
||||

|
||||
|
||||
## Value Underflow
|
||||
## 值的下溢出
|
||||
### Value Underflow
|
||||
### 值的下溢出
|
||||
|
||||
Numbers can also become too small to fit in their type’s maximum bounds. Here’s an example.
|
||||
数值也有可能因为太小而越界。举个例子:
|
||||
@ -343,8 +357,8 @@ signedUnderflow = signedUnderflow &- 1
|
||||
|
||||
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.
|
||||
|
||||
## Division by Zero
|
||||
## 除零溢出
|
||||
### Division by Zero
|
||||
### 除零溢出
|
||||
|
||||
Dividing a number by zero(i / 0), or trying to calculate remainder by zero(i % 0), causes an error:
|
||||
|
||||
@ -365,9 +379,10 @@ let y = x &/ 0
|
||||
// y 等于 0
|
||||
```
|
||||
|
||||
<a name="PrecedenceandAssociativity"></a>
|
||||
# Precedence and Associativity
|
||||
# 优先级和结合性
|
||||
## Precedence and Associativity
|
||||
|
||||
<a name="precedence_and_associativity"></a>
|
||||
## 优先级和结合性
|
||||
|
||||
Operator precedence gives some operators higher priority than others; these operators are calculated first.
|
||||
|
||||
@ -430,18 +445,19 @@ This calculation yields the final answer of 4.
|
||||
|
||||
For a complete list of Swift operator precedences and associativity rules, see [Expressions](Expressions).
|
||||
|
||||
查阅Swift运算符的优先级和结合性的完整列表,请看[表达式](Expressions)。
|
||||
查阅Swift运算符的优先级和结合性的完整列表,请看[表达式](../chapter3/04_Expressions.html)。
|
||||
|
||||
> NOTE
|
||||
|
||||
> 注意
|
||||
|
||||
> 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.
|
||||
|
||||
> Swift的运算符较C语言和Objective-C来得更简单和保守,这意味着跟基于C的语言可能不一样。所以,在移植已有代码到Swift时,注意去确保代码按你想的那样去执行。
|
||||
> 注意:
|
||||
Swift的运算符较C语言和Objective-C来得更简单和保守,这意味着跟基于C的语言可能不一样。所以,在移植已有代码到Swift时,注意去确保代码按你想的那样去执行。
|
||||
|
||||
# Operator Functions
|
||||
# 运算符函数
|
||||
## Operator Functions
|
||||
|
||||
<a name="operator_functions"></a>
|
||||
## 运算符函数
|
||||
|
||||
Classes and structures can provide their own implementations of existing operators. This is known as overloading the existing operators.
|
||||
|
||||
@ -485,12 +501,12 @@ let combinedVector = vector + anotherVector
|
||||
|
||||
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.
|
||||
|
||||
这个例子实现两个向量 `(3。0,1。0)` 和 `(2。0,4。0)` 相加,得到向量 `(5。0,5。0)` 的过程。如下图示:
|
||||
这个例子实现两个向量 `(3.0,1.0)` 和 `(2.0,4.0)` 相加,得到向量 `(5.0,5.0)` 的过程。如下图示:
|
||||
|
||||

|
||||
|
||||
## Prefix and Postfix Operators
|
||||
## 前置和后置运算符
|
||||
### Prefix and Postfix Operators
|
||||
### 前置和后置运算符
|
||||
|
||||
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++).
|
||||
|
||||
@ -508,7 +524,7 @@ You implement a prefix or postfix unary operator by writing the @prefix or @post
|
||||
|
||||
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.
|
||||
|
||||
这段代码为`Vector2D`类型提供了单目减运算`-a`,`@prefix` 表明这是个前置运算符。这个运算符,
|
||||
这段代码为`Vector2D`类型提供了单目减运算`-a`,`@prefix`属性表明这是个前置运算符。
|
||||
|
||||
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:
|
||||
|
||||
@ -522,14 +538,14 @@ let alsoPositive = -negative
|
||||
// alsoPositive 为 (3.0, 4.0)
|
||||
```
|
||||
|
||||
## Compound Assignment Operators
|
||||
## 组合赋值运算符
|
||||
### Compound Assignment Operators
|
||||
### 组合赋值运算符
|
||||
|
||||
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.
|
||||
|
||||
The example below implements an addition assignment operator function for Vector2D instances:
|
||||
|
||||
组合赋值是其他运算符和赋值运算符一起执行的运算。如`+=`把加运算和赋值运算组合成一个操作。实现一个组合赋值符号需要使用 `@assignment` 属性,
|
||||
组合赋值是其他运算符和赋值运算符一起执行的运算。如`+=`把加运算和赋值运算组合成一个操作。实现一个组合赋值符号需要使用`@assignment`属性,还需要把运算符的左参数设置成`inout`,因为这个参数会在运算符函数内直接修改它的值。
|
||||
|
||||
```
|
||||
@assignment func += (inout left: Vector2D, right: Vector2D) {
|
||||
@ -561,7 +577,7 @@ You can combine the @assignment attribute with either the @prefix or @postfix at
|
||||
|
||||
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:
|
||||
|
||||
这个前置使用了已经定义好的高级加赋运算,将自己加上一个值为 `(1。0,1。0)` 的对象然后赋给自己,然后再将自己返回。
|
||||
这个前置使用了已经定义好的高级加赋运算,将自己加上一个值为 `(1.0,1.0)` 的对象然后赋给自己,然后再将自己返回。
|
||||
|
||||
```
|
||||
var toIncrement = Vector2D(x: 3.0, y: 4.0)
|
||||
@ -572,14 +588,13 @@ let afterIncrement = ++toIncrement
|
||||
|
||||
> NOTE
|
||||
|
||||
> 注意
|
||||
|
||||
> 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.
|
||||
|
||||
> 默认的赋值符是不可重载的。只有组合赋值符可以重载。三目条件运算符 `a?b:c` 也是不可重载。
|
||||
>注意:
|
||||
默认的赋值符是不可重载的。只有组合赋值符可以重载。三目条件运算符 `a?b:c` 也是不可重载。
|
||||
|
||||
## Equivalence Operators
|
||||
## 比较运算符
|
||||
### Equivalence Operators
|
||||
### 比较运算符
|
||||
|
||||
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.
|
||||
|
||||
@ -616,8 +631,8 @@ if twoThree == anotherTwoThree {
|
||||
// prints "这两个向量是相等的."
|
||||
```
|
||||
|
||||
## Custom Operators
|
||||
## 自定义运算符
|
||||
### Custom Operators
|
||||
### 自定义运算符
|
||||
|
||||
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 / = - + * % < > ! & | ^ . ~.
|
||||
|
||||
@ -653,8 +668,8 @@ let afterDoubling = +++toBeDoubled
|
||||
// afterDoubling 现在也是 (2.0, 8.0)
|
||||
```
|
||||
|
||||
## Precedence and Associativity for Custom Infix Operators
|
||||
## 自定义中置运算符的优先级和结合性
|
||||
### Precedence and Associativity for Custom Infix Operators
|
||||
### 自定义中置运算符的优先级和结合性
|
||||
|
||||
Custom infix operators can also specify a precedence and an associativity. See [Precedence and Associativity](#PrecedenceandAssociativity) for an explanation of how these two characteristics affect an infix operator’s interaction with other infix operators.
|
||||
|
||||
@ -685,4 +700,4 @@ let plusMinusVector = firstVector +- secondVector
|
||||
|
||||
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](Expressions).
|
||||
|
||||
这个运算符把两个向量的`x`相加,把向量的`y`相减。因为他实际是属于加减运算,所以让它保持了和加法一样的结合性和优先级(`left`和`140`)。查阅完整的Swift默认结合性和优先级的设置,请移步[表达式](Expressions);
|
||||
这个运算符把两个向量的`x`相加,把向量的`y`相减。因为他实际是属于加减运算,所以让它保持了和加法一样的结合性和优先级(`left`和`140`)。查阅完整的Swift默认结合性和优先级的设置,请移步[表达式](../chapter3/04_Expressions.html);
|
||||
Reference in New Issue
Block a user