From ee2bb24fc8a4b09a32c1ebee4b7238470ef1b289 Mon Sep 17 00:00:00 2001 From: Amin by Date: Thu, 5 Jun 2014 10:48:14 +0800 Subject: [PATCH 1/8] first para --- source/chapter2/02_Basic_Operators.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/source/chapter2/02_Basic_Operators.md b/source/chapter2/02_Basic_Operators.md index e69de29b..0649c662 100644 --- a/source/chapter2/02_Basic_Operators.md +++ b/source/chapter2/02_Basic_Operators.md @@ -0,0 +1,11 @@ +# 基础运算符 + +运算符是检查, 改变, 合并值的特殊符号或短语. 例如, 加号`+`把计算两个数的和(如 `let i = 1 + 2`). 复杂些的运行算包括逻辑与`&&`(如 `if enteredDoorCode && passedRetinaScan`), 还有自增运算符 `++i`, 这一个自身增加一的快捷操作. + +Swift supports most standard C operators and improves several capabilities to eliminate common coding errors. The assignment operator (=) does not return a value, to prevent it from being mistakenly used when the equal to operator (==) is intended. Arithmetic operators (+, -, *, /, % and so forth) detect and disallow value overflow, to avoid unexpected results when working with numbers that become larger or smaller than the allowed value range of the type that stores them. You can opt in to value overflow behavior by using Swift’s overflow operators, as described in Overflow Operators. + +Unlike C, Swift lets you perform remainder (%) calculations on floating-point numbers. Swift also provides two range operators (a..b and a...b) not found in C, as a shortcut for expressing a range of values. + +This chapter describes the common operators in Swift. Advanced Operators[…]” + +Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/cn/jEUH0.l \ No newline at end of file From 866246913a06c01fee4ac86027cb5ed96cab7430 Mon Sep 17 00:00:00 2001 From: Amin by Date: Thu, 5 Jun 2014 11:01:37 +0800 Subject: [PATCH 2/8] add some origin --- source/chapter2/02_Basic_Operators.md | 82 ++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/source/chapter2/02_Basic_Operators.md b/source/chapter2/02_Basic_Operators.md index 0649c662..3368d586 100644 --- a/source/chapter2/02_Basic_Operators.md +++ b/source/chapter2/02_Basic_Operators.md @@ -8,4 +8,84 @@ Unlike C, Swift lets you perform remainder (%) calculations on floating-point nu This chapter describes the common operators in Swift. Advanced Operators[…]” -Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/cn/jEUH0.l \ No newline at end of file +Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/cn/jEUH0.l + +“Terminology +Operators are unary, binary, or ternary: + +Unary operators operate on a single target (such as -a). Unary prefix operators appear immediately before their target (such as !b), and unary postfix operators appear immediately after their target (such as i++). +Binary operators operate on two targets (such as 2 + 3) and are infix because they appear in between their two targets. +Ternary operators operate on three targets. Like C, Swift has only one ternary operator, the ternary conditional operator (a ? b : c).” + +Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/cn/jEUH0.l + +“The values that operators affect are operands. In the expression 1 + 2, the + symbol is a binary operator and its two operands are the values 1 and 2. + +‌ +Assignment Operator +The assignment operator (a = b) initializes or updates the value of a with the value of b: + +let b = 10 +var a = 5 +a = b +// a is now equal to 10 +If the right side of the assignment is a tuple with multiple values, its elements can be decomposed into multiple constants or variables at once: + +let (x, y) = (1, 2) +// x is equal to 1, and y is equal to 2 +Unlike the assignment operator in C and Objective-C, the assignment operator in Swift does not itself return a value. The following statement is not valid: + +if x = y { + // this is not valid, because x = y does not return a value +} +This feature prevents the assignment operator (=) from being used by accident when the equal to operator (==) is actually intended. By making if x = y invalid, Swift helps you to avoid these kinds of errors in your code. + +‌ +Arithmetic Operators +Swift supports the four standard arithmetic operators for all number types: + +Addition (+) +Subtraction (-) +Multiplication (*) +Division (/) +1 + 2[…]” + +Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/cn/jEUH0.l + +“Unlike the arithmetic operators in C and Objective-C, the Swift arithmetic operators do not allow values to overflow by default. You can opt in to value overflow behavior by using Swift’s overflow operators (such as a &+ b). See Overflow Operators. + +The addition operator is also supported for String concatenation: + +"hello, " + "world" // equals "hello, world" +Two Character values, or one Character value and one String value, can be added together to make a new String value: + +let dog: Character = "🐶" +let cow: Character = "🐮" +let dogCow = dog + cow +// dogCow is equal to "🐶🐮" +See also Concatenating Strings and Characters. + +‌ +Remainder Operator +The remainder operator (a % b) works out how many multiples of b will fit inside a and returns the value that is left over (known as the remainder). + +NOTE + +The remainder operator (%) is also known as a modulo operator in other languages. However, its behavior in Swift for negative numbers means that it is, strictly speaking, a remainder rather than a modulo operation. + +Here’s how the remainder operator works. To calculate 9 % 4, you first work out how many 4s will fit inside 9: + + +You can fit two 4s inside 9, and the remainder is 1 (shown in orange). + +In Swift, this be written as: + +9 % 4 // equals 1 +To determine the answer for a % b, the % operator calculates the following equation and returns remainder as its output: + +a = (b × some multiplier) + remainder + +where some multiplier is the largest number of multiples of” + +Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/cn/jEUH0.l + From b3bbfea7be7e5c03cd0a95d0844eb1f75ae78d1e Mon Sep 17 00:00:00 2001 From: Amin by Date: Thu, 5 Jun 2014 10:48:14 +0800 Subject: [PATCH 3/8] first para --- source/chapter2/02_Basic_Operators.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/source/chapter2/02_Basic_Operators.md b/source/chapter2/02_Basic_Operators.md index e69de29b..0649c662 100644 --- a/source/chapter2/02_Basic_Operators.md +++ b/source/chapter2/02_Basic_Operators.md @@ -0,0 +1,11 @@ +# 基础运算符 + +运算符是检查, 改变, 合并值的特殊符号或短语. 例如, 加号`+`把计算两个数的和(如 `let i = 1 + 2`). 复杂些的运行算包括逻辑与`&&`(如 `if enteredDoorCode && passedRetinaScan`), 还有自增运算符 `++i`, 这一个自身增加一的快捷操作. + +Swift supports most standard C operators and improves several capabilities to eliminate common coding errors. The assignment operator (=) does not return a value, to prevent it from being mistakenly used when the equal to operator (==) is intended. Arithmetic operators (+, -, *, /, % and so forth) detect and disallow value overflow, to avoid unexpected results when working with numbers that become larger or smaller than the allowed value range of the type that stores them. You can opt in to value overflow behavior by using Swift’s overflow operators, as described in Overflow Operators. + +Unlike C, Swift lets you perform remainder (%) calculations on floating-point numbers. Swift also provides two range operators (a..b and a...b) not found in C, as a shortcut for expressing a range of values. + +This chapter describes the common operators in Swift. Advanced Operators[…]” + +Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/cn/jEUH0.l \ No newline at end of file From e9236905c1015af9155ece55b056074e9d7129bb Mon Sep 17 00:00:00 2001 From: Amin by Date: Thu, 5 Jun 2014 11:01:37 +0800 Subject: [PATCH 4/8] add some origin --- source/chapter2/02_Basic_Operators.md | 82 ++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/source/chapter2/02_Basic_Operators.md b/source/chapter2/02_Basic_Operators.md index 0649c662..3368d586 100644 --- a/source/chapter2/02_Basic_Operators.md +++ b/source/chapter2/02_Basic_Operators.md @@ -8,4 +8,84 @@ Unlike C, Swift lets you perform remainder (%) calculations on floating-point nu This chapter describes the common operators in Swift. Advanced Operators[…]” -Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/cn/jEUH0.l \ No newline at end of file +Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/cn/jEUH0.l + +“Terminology +Operators are unary, binary, or ternary: + +Unary operators operate on a single target (such as -a). Unary prefix operators appear immediately before their target (such as !b), and unary postfix operators appear immediately after their target (such as i++). +Binary operators operate on two targets (such as 2 + 3) and are infix because they appear in between their two targets. +Ternary operators operate on three targets. Like C, Swift has only one ternary operator, the ternary conditional operator (a ? b : c).” + +Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/cn/jEUH0.l + +“The values that operators affect are operands. In the expression 1 + 2, the + symbol is a binary operator and its two operands are the values 1 and 2. + +‌ +Assignment Operator +The assignment operator (a = b) initializes or updates the value of a with the value of b: + +let b = 10 +var a = 5 +a = b +// a is now equal to 10 +If the right side of the assignment is a tuple with multiple values, its elements can be decomposed into multiple constants or variables at once: + +let (x, y) = (1, 2) +// x is equal to 1, and y is equal to 2 +Unlike the assignment operator in C and Objective-C, the assignment operator in Swift does not itself return a value. The following statement is not valid: + +if x = y { + // this is not valid, because x = y does not return a value +} +This feature prevents the assignment operator (=) from being used by accident when the equal to operator (==) is actually intended. By making if x = y invalid, Swift helps you to avoid these kinds of errors in your code. + +‌ +Arithmetic Operators +Swift supports the four standard arithmetic operators for all number types: + +Addition (+) +Subtraction (-) +Multiplication (*) +Division (/) +1 + 2[…]” + +Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/cn/jEUH0.l + +“Unlike the arithmetic operators in C and Objective-C, the Swift arithmetic operators do not allow values to overflow by default. You can opt in to value overflow behavior by using Swift’s overflow operators (such as a &+ b). See Overflow Operators. + +The addition operator is also supported for String concatenation: + +"hello, " + "world" // equals "hello, world" +Two Character values, or one Character value and one String value, can be added together to make a new String value: + +let dog: Character = "🐶" +let cow: Character = "🐮" +let dogCow = dog + cow +// dogCow is equal to "🐶🐮" +See also Concatenating Strings and Characters. + +‌ +Remainder Operator +The remainder operator (a % b) works out how many multiples of b will fit inside a and returns the value that is left over (known as the remainder). + +NOTE + +The remainder operator (%) is also known as a modulo operator in other languages. However, its behavior in Swift for negative numbers means that it is, strictly speaking, a remainder rather than a modulo operation. + +Here’s how the remainder operator works. To calculate 9 % 4, you first work out how many 4s will fit inside 9: + + +You can fit two 4s inside 9, and the remainder is 1 (shown in orange). + +In Swift, this be written as: + +9 % 4 // equals 1 +To determine the answer for a % b, the % operator calculates the following equation and returns remainder as its output: + +a = (b × some multiplier) + remainder + +where some multiplier is the largest number of multiples of” + +Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/cn/jEUH0.l + From 08b4145c388bf516b597a048178b430e84bd57c3 Mon Sep 17 00:00:00 2001 From: Amin by Date: Fri, 6 Jun 2014 14:54:42 +0800 Subject: [PATCH 5/8] finish section 1 of part 2 of charpter 2 --- source/chapter2/02_Basic_Operators.md | 310 ++++++++++++++++++++++++-- 1 file changed, 286 insertions(+), 24 deletions(-) diff --git a/source/chapter2/02_Basic_Operators.md b/source/chapter2/02_Basic_Operators.md index 3368d586..52f917f0 100644 --- a/source/chapter2/02_Basic_Operators.md +++ b/source/chapter2/02_Basic_Operators.md @@ -1,25 +1,26 @@ # 基础运算符 -运算符是检查, 改变, 合并值的特殊符号或短语. 例如, 加号`+`把计算两个数的和(如 `let i = 1 + 2`). 复杂些的运行算包括逻辑与`&&`(如 `if enteredDoorCode && passedRetinaScan`), 还有自增运算符 `++i`, 这一个自身增加一的快捷操作. +运算符是检查, 改变, 合并值的特殊符号或短语. 例如, 加号`+`把计算两个数的和(如 `let i = 1 + 2`). 复杂些的运行算包括逻辑与`&&`(如 `if enteredDoorCode && passedRetinaScan`), 还有自增运算符 `++i`, 这一个自身加一的快捷操作. -Swift supports most standard C operators and improves several capabilities to eliminate common coding errors. The assignment operator (=) does not return a value, to prevent it from being mistakenly used when the equal to operator (==) is intended. Arithmetic operators (+, -, *, /, % and so forth) detect and disallow value overflow, to avoid unexpected results when working with numbers that become larger or smaller than the allowed value range of the type that stores them. You can opt in to value overflow behavior by using Swift’s overflow operators, as described in Overflow Operators. +Swift支持大部分标准C的运算符, 且改进许多项来获得减少常规编码错误. 赋值符`=`不返回值, 以防止出现错把等号 `==` 写成赋值号 `=` 导致的Bug. 数值运算符(`+`, `-`, `*`, `/`, `%`等)会检测并不允许值溢出, 以此来避免保存变量时由于变量大于或小于其类型所能承载的范围时导致的异常结果. 你可以选择使用Swift的溢出运算符来玩溢出. 具体使用请移步[溢出运算符](http://#). -Unlike C, Swift lets you perform remainder (%) calculations on floating-point numbers. Swift also provides two range operators (a..b and a...b) not found in C, as a shortcut for expressing a range of values. +与C不同, Swift中你可以对浮点数进行取余运算(`%`). Swift也提供在C语言没有的表达两数之间的范围操作符, (`a..b`和`a...b`),这方便我们表达一个范围的数值. -This chapter describes the common operators in Swift. Advanced Operators[…]” +本章节只描述了Swift中的常规运算符, [高级运算符](http://#)包含了高级运算符,及如何自定义运算符,及自定义类型的运算符重载. -Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/cn/jEUH0.l -“Terminology +‌# 术语 +运算符有一目,两目和三目运算符. + +一目运算符对单一目标操作, 如`-a`. + +Terminology Operators are unary, binary, or ternary: Unary operators operate on a single target (such as -a). Unary prefix operators appear immediately before their target (such as !b), and unary postfix operators appear immediately after their target (such as i++). -Binary operators operate on two targets (such as 2 + 3) and are infix because they appear in between their two targets. -Ternary operators operate on three targets. Like C, Swift has only one ternary operator, the ternary conditional operator (a ? b : c).” - -Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/cn/jEUH0.l - -“The values that operators affect are operands. In the expression 1 + 2, the + symbol is a binary operator and its two operands are the values 1 and 2. +Binary operators operate on two targets (such as 2 + 3) and are infix“because they appear in between their two targets. +Ternary operators operate on three targets. Like C, Swift has only one ternary operator, the ternary conditional operator (a ? b : c). +The values that operators affect are operands. In the expression 1 + 2, the + symbol is a binary operator and its two operands are the values 1 and 2. ‌ Assignment Operator @@ -40,7 +41,6 @@ if x = y { } This feature prevents the assignment operator (=) from being used by accident when the equal to operator (==) is actually intended. By making if x = y invalid, Swift helps you to avoid these kinds of errors in your code. -‌ Arithmetic Operators Swift supports the four standard arithmetic operators for all number types: @@ -48,11 +48,11 @@ Addition (+) Subtraction (-) Multiplication (*) Division (/) -1 + 2[…]” - -Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/cn/jEUH0.l - -“Unlike the arithmetic operators in C and Objective-C, the Swift arithmetic operators do not allow values to overflow by default. You can opt in to value overflow behavior by using Swift’s overflow operators (such as a &+ b). See Overflow Operators. +1 + 2 // equals 3 +5 - 3 // equals 2 +2 * 3 // equals 6 +10.0 / 2.5 // equals 4.0 +Unlike the arithmetic operators in C and Objective-C, the Swift arithmetic operators do not allow values to overflow by default. You can opt in to value overflow behavior by using Swift’s overflow operators (such as a &+ b). See [Overflow Operators](http://#). The addition operator is also supported for String concatenation: @@ -63,29 +63,291 @@ let dog: Character = "🐶" let cow: Character = "🐮" let dogCow = dog + cow // dogCow is equal to "🐶🐮" -See also Concatenating Strings and Characters. +See also [Concatenating Strings and Characters](http://#). ‌ Remainder Operator The remainder operator (a % b) works out how many multiples of b will fit inside a and returns the value that is left over (known as the remainder). -NOTE -The remainder operator (%) is also known as a modulo operator in other languages. However, its behavior in Swift for negative numbers means that it is, strictly speaking, a remainder rather than a modulo operation. +> NOTE + +> The remainder operator (%) is also known as a modulo operator in other languages. However, its behavior in Swift for negative numbers means that it is, strictly speaking, a remainder rather than a modulo operation. Here’s how the remainder operator works. To calculate 9 % 4, you first work out how many 4s will fit inside 9: You can fit two 4s inside 9, and the remainder is 1 (shown in orange). -In Swift, this be written as: +In Swift, this would be written as: 9 % 4 // equals 1 To determine the answer for a % b, the % operator calculates the following equation and returns remainder as its output: a = (b × some multiplier) + remainder -where some multiplier is the largest number of multiples of” +where some multiplier is the largest number of multiples of b that will fit inside a. -Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/cn/jEUH0.l +Inserting 9 and 4 into this equation yields: +9 = (4 × 2) + 1 + +The same method is applied when calculating the remainder for a negative value of a: + +-9 % 4 // equals -1 +Inserting -9 and 4 into the equation yields: + +-9 = (4 × -2) + -1 + +giving a remainder value of -1. + +The sign of b is ignored for negative values of b. This means that a % b and a % -b always give the same answer. + +‌ +Floating-Point Remainder Calculations +Unlike the remainder operator in C and Objective-C, Swift’s remainder operator can also operate on floating-point numbers: + +“8 % 2.5 // equals 0.5 +In this example, 8 divided by 2.5 equals 3, with a remainder of 0.5, so the remainder operator returns a Double value of 0.5. + + +‌ +Increment and Decrement Operators +Like C, Swift provides an increment operator (++) and a decrement operator (--) as a shortcut to increase or decrease the value of a numeric variable by 1. You can use these operators with variables of any integer or floating-point type. + +var i = 0 +++i // i now equals 1 +Each time you call ++i, the value of i is increased by 1. Essentially, ++i is shorthand for saying i = i + 1. Likewise, --i can be used as shorthand for i = i - 1. + +The ++ and -- symbols can be used as prefix operators or as postfix operators. ++i and i++ are both valid ways to increase the value of i by 1. Similarly, --i and i-- are both valid ways to decrease the value of i by 1. + +Note that these operators modify i and also return a value. If you only want to increment or decrement the value stored in i, you can ignore the returned value. However, if you do use the returned value, it will be different “based on whether you used the prefix or postfix version of the operator, according to the following rules: + +If the operator is written before the variable, it increments the variable before returning its value. +If the operator is written after the variable, it increments the variable after returning its value. +For example: + +var a = 0 +let b = ++a +// a and b are now both equal to 1 +let c = a++ +// a is now equal to 2, but c has been set to the pre-increment value of 1 +In the example above, let b = ++a increments a before returning its value. This is why both a and b are equal to to the new value of 1. + +However, let c = a++ increments a after returning its value. This means that c gets the old value of 1, and a is then updated to equal 2. + +Unless you need the specific behavior of i++, it is recommended that you use ++i and --i in all cases, because they have the typical expected behavior of modifying i and returning the result. + +“Unary Minus Operator +The sign of a numeric value can be toggled using a prefixed -, known as the unary minus operator: + +let three = 3 +let minusThree = -three // minusThree equals -3 +let plusThree = -minusThree // plusThree equals 3, or "minus minus three" +The unary minus operator (-) is prepended directly before the value it operates on, without any white space. + +‌ +Unary Plus Operator +The unary plus operator (+) simply returns the value it operates on, without any change: + +let minusSix = -6 +let alsoMinusSix = +minusSix // alsoMinusSix equals -6 +Although the unary plus operator doesn’t actually do anything, you can use it to provide symmetry in your code for positive numbers when also using the unary minus operator for negative numbers. + +“Compound Assignment Operators +Like C, Swift provides compound assignment operators that combine assignment (=) with another operation. One example is the addition assignment operator (+=): + +var a = 1 +a += 2 +// a is now equal to 3 +The expression a += 2 is shorthand for a = a + 2. Effectively, the addition and the assignment are combined into one operator that performs both tasks at the same time. + +NOTE + +The compound assignment operators do not return a value. You cannot write let b = a += 2, for example. This behavior is different from the increment and decrement operators mentioned above. + +A complete list of compound assignment operators can be found in [Expressions](http://#). + +‌ +Comparison Operators +Swift supports all standard C comparison operators: + +Equal to (a == b) +Not equal to (a != b) + +“Greater than (a > b) +Less than (a < b) +Greater than or equal to (a >= b) +Less than or equal to (a <= b) +NOTE + +Swift also provides two identity operators (=== and !==), which you use to test whether two object references both refer to the same object instance. For more information, see [Classes and Structures](http://#). + +Each of the comparison operators returns a Bool value to indicate whether or not the statement is true: + +1 == 1 // true, because 1 is equal to 1 +2 != 1 // true, because 2 is not equal to 1 +2 > 1 // true, because 2 is greater than 1 +1 < 2 // true, because 1 is less than 2 +1 >= 1 // true, because 1 is greater than or equal to 1 +2 <= 1 // false, because 2 is not less than or equal to 1 +Comparison operators are often used in conditional statements, such as the if statement: + + +“let name = "world" +if name == "world" { + println("hello, world") +} else { + println("I'm sorry \(name), but I don't recognize you") +} +// prints "hello, world", because name is indeed equal to "world" +For more on the if statement, see Control Flow. + +‌ +Ternary Conditional Operator +The ternary conditional operator is a special operator with three parts, which takes the form question ? answer1 : answer2. It is a shortcut for evaluating one of two expressions based on whether question is true or false. If question is true, it evaluates answer1 and returns its value; otherwise, it evaluates answer2 and returns its value. + +The ternary conditional operator is shorthand for the code below: + +if question { + answer1 +} else { + answer2 +} +Here’s an example, which calculates the pixel height for a table row. The row height should be 50 pixels taller “than the content height if the row has a header, and 20 pixels taller if the row doesn’t have a header: + +let contentHeight = 40 +let hasHeader = true +let rowHeight = contentHeight + (hasHeader ? 50 : 20) +// rowHeight is equal to 90 +The preceding example is shorthand for the code below: + +let contentHeight = 40 +let hasHeader = true +var rowHeight = contentHeight +if hasHeader { + rowHeight = rowHeight + 50 +} else { + rowHeight = rowHeight + 20 +} +// rowHeight is equal to 90 +The first example’s use of the ternary conditional operator means that rowHeight can be set to the correct value on a single line of code. This is more concise than the second example, and removes the need for rowHeight to be a variable, because its value does not need to be modified within an if statement. + +“The ternary conditional operator provides an efficient shorthand for deciding which of two expressions to consider. Use the ternary conditional operator with care, however. Its conciseness can lead to hard-to-read code if overused. Avoid combining multiple instances of the ternary conditional operator into one compound statement. + +‌ +Range Operators +Swift includes two range operators, which are shortcuts for expressing a range of values. + +‌ +Closed Range Operator +The closed range operator (a...b) defines a range that runs from a to b, and includes the values a and b. + +The closed range operator is useful when iterating over a range in which you want all of the values to be used, such as with a for-in loop: + +for index in 1...5 { + println("\(index) times 5 is \(index * 5)") +} +// 1 times 5 is 5 +// 2 times 5 is 10 +// 3 times 5 is 15 +// 4 times 5 is 20 +// 5 times 5 is 25 +For more on for-in loops, see [Control Flow](http://#). + +“Half-Closed Range Operator +The half-closed range operator (a..b) defines a range that runs from a to b, but does not include b. It is said to be half-closed because it contains its first value, but not its final value. + +Half-closed ranges are particularly useful when you work with zero-based lists such as arrays, where it is useful to count up to (but not including) the length of the list: + +let names = ["Anna", "Alex", "Brian", "Jack"] +let count = names.count +for i in 0..count { + println("Person \(i + 1) is called \(names[i])") +} +// Person 1 is called Anna +// Person 2 is called Alex +// Person 3 is called Brian +// Person 4 is called Jack +Note that the array contains four items, but 0..count only counts as far as 3 (the index of the last item in the array), because it is a half-closed range. For more on arrays, see [Arrays](http://#). + + +“Logical Operators +Logical operators modify or combine the Boolean logic values true and false. Swift supports the three standard logical operators found in C-based languages: + +Logical NOT (!a) +Logical AND (a && b) +Logical OR (a || b) +‌ +Logical NOT Operator +The logical NOT operator (!a) inverts a Boolean value so that true becomes false, and false becomes true. + +The logical NOT operator is a prefix operator, and appears immediately before the value it operates on, without any white space. It can be read as “not a”, as seen in the following example: + +let allowedEntry = false +if !allowedEntry { + println("ACCESS DENIED") +} +// prints "ACCESS DENIED" +The phrase if !allowedEntry can be read as “if not allowed entry.” The subsequent line is only executed if “not allowed entry” is true; that is, if allowedEntry is false. + +As in this example, careful choice of Boolean constant and variable names can help to keep code readable and concise, while avoiding double negatives or confusing logic statements. + +“Logical AND Operator +The logical AND operator (a && b) creates logical expressions where both values must be true for the overall expression to also be true. + +If either value is false, the overall expression will also be false. In fact, if the first value is false, the second value won’t even be evaluated, because it can’t possibly make the overall expression equate to true. This is known as short-circuit evaluation. + +This example considers two Bool values and only allows access if both values are true: + +let enteredDoorCode = true +let passedRetinaScan = false +if enteredDoorCode && passedRetinaScan { + println("Welcome!") +} else { + println("ACCESS DENIED") +} +// prints "ACCESS DENIED + +“Logical OR Operator +The logical OR operator (a || b) is an infix operator made from two adjacent pipe characters. You use it to create logical expressions in which only one of the two values has to be true for the overall expression to be true. + +Like the Logical AND operator above, the Logical OR operator uses short-circuit evaluation to consider its expressions. If the left side of a Logical OR expression is true, the right side is not evaluated, because it cannot change the outcome of the overall expression. + +In the example below, the first Bool value (hasDoorKey) is false, but the second value (knowsOverridePassword) is true. Because one value is true, the overall expression also evaluates to true, and access is allowed: + +let hasDoorKey = false +let knowsOverridePassword = true +if hasDoorKey || knowsOverridePassword { + println("Welcome!") +} else { + println("ACCESS DENIED") +} +// prints "Welcome!" +‌ +Combining Logical Operators +You can combine multiple logical operators to create longer compound expressions: + +“if enteredDoorCode && passedRetinaScan || hasDoorKey || knowsOverridePassword { + println("Welcome!") +} else { + println("ACCESS DENIED") +} +// prints "Welcome!" +This example uses multiple && and || operators to create a longer compound expression. However, the && and || operators still operate on only two values, so this is actually three smaller expressions chained together. It can be read as: + +If we’ve entered the correct door code and passed the retina scan; or if we have a valid door key; or if we know the emergency override password, then allow access. + +Based on the values of enteredDoorCode, passedRetinaScan, and hasDoorKey, the first two mini-expressions are false. However, the emergency override password is known, so the overall compound expression still evaluates to true. + +‌ +Explicit Parentheses +It is sometimes useful to include parentheses when they are not strictly needed, to make the intention of a complex expression easier to read. In the door access example above, it is useful to add parentheses around the first part of the compound expression to make its intent explicit: + +if (enteredDoorCode && passedRetinaScan) || hasDoorKey || knowsOverridePassword { + println("Welcome!") +} else { + println("ACCESS DENIED") +} +// prints "Welcome!" +The parentheses make it clear that the first two values are considered as part of a separate possible state in the overall logic. “The output of the compound expression doesn’t change, but the overall intention is clearer to the reader. Readability is always preferred over brevity; use parentheses where they help to make your intentions clear. \ No newline at end of file From 96b01fc952e089dcd00672684266b1b07064e95c Mon Sep 17 00:00:00 2001 From: Amin by Date: Fri, 6 Jun 2014 15:58:30 +0800 Subject: [PATCH 6/8] finish to floating-point remainder calculations --- source/chapter2/02_Basic_Operators.md | 135 ++++++++++++++++---------- 1 file changed, 82 insertions(+), 53 deletions(-) diff --git a/source/chapter2/02_Basic_Operators.md b/source/chapter2/02_Basic_Operators.md index 52f917f0..e4ed3f00 100644 --- a/source/chapter2/02_Basic_Operators.md +++ b/source/chapter2/02_Basic_Operators.md @@ -10,100 +10,129 @@ Swift支持大部分标准C的运算符, 且改进许多项来获得减少常规 ‌# 术语 -运算符有一目,两目和三目运算符. +运算符有一目,双目和三目运算符. -一目运算符对单一目标操作, 如`-a`. +一目运算符对单一操作对象操作, 如`-a`. -Terminology -Operators are unary, binary, or ternary: +一目运算符分前置符和后置运算符, 前置运算符需紧排操作对象之前, 如`!b`, 后置运算符需紧跟操作对象之后,如`i++`, + +双目运算符操作两个操作对象, 如`2 + 3`. 是中置的, 因为它们出现在两个操作对象之间. + +三目运算符操作三个操作对象, 跟C一样, Swift只有一个三目运算符, 就是三目条件运算符 `a ? b : c`. + +受运算符影响的值叫操作数, 在表达式`1 + 2`中, 加号`+`是双目运算符, 它的两个操作数是值`1`和`2`. -Unary operators operate on a single target (such as -a). Unary prefix operators appear immediately before their target (such as !b), and unary postfix operators appear immediately after their target (such as i++). -Binary operators operate on two targets (such as 2 + 3) and are infix“because they appear in between their two targets. -Ternary operators operate on three targets. Like C, Swift has only one ternary operator, the ternary conditional operator (a ? b : c). -The values that operators affect are operands. In the expression 1 + 2, the + symbol is a binary operator and its two operands are the values 1 and 2. ‌ -Assignment Operator -The assignment operator (a = b) initializes or updates the value of a with the value of b: +# 赋值运算符 +赋值运算 `a = b`, 表示用`b`的值来初始化或更新`a`的值. + +``` let b = 10 var a = 5 a = b -// a is now equal to 10 -If the right side of the assignment is a tuple with multiple values, its elements can be decomposed into multiple constants or variables at once: +// a 现在等于 10 +``` +如果赋值的右边是一个多元组, 它的元素可以马上被分解多个变量或变量 +``` let (x, y) = (1, 2) -// x is equal to 1, and y is equal to 2 -Unlike the assignment operator in C and Objective-C, the assignment operator in Swift does not itself return a value. The following statement is not valid: +// 现在 x 等于 1, y 等于 2 +``` +与C和Objective-C又不同了, Swift的赋值操作并不返回任何值. 所以以下代码是错误的: +``` if x = y { - // this is not valid, because x = y does not return a value + // 此句错误, 因为 x = y 并不返回任何值 } -This feature prevents the assignment operator (=) from being used by accident when the equal to operator (==) is actually intended. By making if x = y invalid, Swift helps you to avoid these kinds of errors in your code. +``` -Arithmetic Operators -Swift supports the four standard arithmetic operators for all number types: +这个特性使得你不能够再把`==`错写成`=`了, 由于`if x = y`是错误代码, Swift从底层帮你避免了这些代码错误. -Addition (+) -Subtraction (-) -Multiplication (*) -Division (/) -1 + 2 // equals 3 -5 - 3 // equals 2 -2 * 3 // equals 6 -10.0 / 2.5 // equals 4.0 -Unlike the arithmetic operators in C and Objective-C, the Swift arithmetic operators do not allow values to overflow by default. You can opt in to value overflow behavior by using Swift’s overflow operators (such as a &+ b). See [Overflow Operators](http://#). +# 数值运算 +Swift让所有数值类型都支持了基本的四则运算: +- 加法 (+) +- 减法 (-) +- 乘法 (*) +- 除法 (/) -The addition operator is also supported for String concatenation: +``` +1 + 2 // 等于 3 +5 - 3 // 等于 2 +2 * 3 // 等于 6 +10.0 / 2.5 // 等于 4.0 +``` -"hello, " + "world" // equals "hello, world" -Two Character values, or one Character value and one String value, can be added together to make a new String value: +又与C和Objective-C不一样了, Swift默认不允许数值运算出现溢出. 但你可以使用Swift的溢出运算符来达到你的目的, (如 `a &+ b` ). 详情请移步: [溢出运算符](http://#). +加法操作也可以用于字符串的拼接: + +``` +"hello, " + "world" // 等于 "hello, world" +``` + +两个字符类型或一个字符类型和一个字符串类型, 相加会生成一个新的字符串类型: + +``` let dog: Character = "🐶" let cow: Character = "🐮" let dogCow = dog + cow -// dogCow is equal to "🐶🐮" -See also [Concatenating Strings and Characters](http://#). +// dogCow 现在是 "🐶🐮" +``` +详细请点击 [字符,字符串的拼接](http://#). -‌ -Remainder Operator -The remainder operator (a % b) works out how many multiples of b will fit inside a and returns the value that is left over (known as the remainder). +‌# 求余运算 +求余运算`a % b`是计算`b`的多少倍刚好可以装进`a`, 多出来的那部分叫余数. -> NOTE +> 注意 -> The remainder operator (%) is also known as a modulo operator in other languages. However, its behavior in Swift for negative numbers means that it is, strictly speaking, a remainder rather than a modulo operation. +> 求余运算(%)在其他语言也叫取模运算. 然而, 鉴于在Swift中该运算符对负数的操作结果, 严格来说, 求余比取模更合适些. -Here’s how the remainder operator works. To calculate 9 % 4, you first work out how many 4s will fit inside 9: +我们来谈谈取余是怎么回事, 计算 `9 % 4`, 你先计算出4的多少倍会刚好可以装进`9`中. +2, 好的, 余数是1 (用橙色标出) - -You can fit two 4s inside 9, and the remainder is 1 (shown in orange). +``` +传说这里有张求余数的图... +传说这里有张求余数的图... +``` -In Swift, this would be written as: +在Swift中这么来表达 -9 % 4 // equals 1 -To determine the answer for a % b, the % operator calculates the following equation and returns remainder as its output: +``` +9 % 4 // 等于 1 +``` -a = (b × some multiplier) + remainder +为了得到`a % b`的结果, `%`计算了以下等式, 并输出`余数`作为结果: +``` +a = (b × `倍数`) + `余数` +``` +当`倍数`取最大值的时候, 就会刚好可以装进`a`中. -where some multiplier is the largest number of multiples of b that will fit inside a. - -Inserting 9 and 4 into this equation yields: +把 `9` 和 `4` 代入等式中: +``` 9 = (4 × 2) + 1 +``` -The same method is applied when calculating the remainder for a negative value of a: - +同样的方法, 我来们计算`-9 % 4`: +``` -9 % 4 // equals -1 -Inserting -9 and 4 into the equation yields: +``` +把 `-9` 和 4 代入等式: + +``` -9 = (4 × -2) + -1 +``` -giving a remainder value of -1. +等于余数是-1. -The sign of b is ignored for negative values of b. This means that a % b and a % -b always give the same answer. +在对负数的`b`求余时, `b`的符号会被忽略. 这意味着 `a % b` 和 `a % -b`的结果是相同的. + +# 浮点数的求余计算 -‌ Floating-Point Remainder Calculations Unlike the remainder operator in C and Objective-C, Swift’s remainder operator can also operate on floating-point numbers: From f278459244ae753c75c0cd134addbab282f18cb5 Mon Sep 17 00:00:00 2001 From: Amin hpc Date: Sat, 7 Jun 2014 14:46:53 +0800 Subject: [PATCH 7/8] finish all --- source/chapter2/02_Basic_Operators.md | 354 +++++++++++++++----------- 1 file changed, 206 insertions(+), 148 deletions(-) diff --git a/source/chapter2/02_Basic_Operators.md b/source/chapter2/02_Basic_Operators.md index 5d5901af..63964fa5 100644 --- a/source/chapter2/02_Basic_Operators.md +++ b/source/chapter2/02_Basic_Operators.md @@ -4,12 +4,12 @@ Swift支持大部分标准C的运算符, 且改进许多项来获得减少常规编码错误. 赋值符`=`不返回值, 以防止出现错把等号 `==` 写成赋值号 `=` 导致的Bug. 数值运算符(`+`, `-`, `*`, `/`, `%`等)会检测并不允许值溢出, 以此来避免保存变量时由于变量大于或小于其类型所能承载的范围时导致的异常结果. 你可以选择使用Swift的溢出运算符来玩溢出. 具体使用请移步[溢出运算符](http://#). -与C不同, Swift中你可以对浮点数进行取余运算(`%`). Swift也提供在C语言没有的表达两数之间的范围操作符, (`a..b`和`a...b`),这方便我们表达一个范围的数值. +与C不同, Swift中你可以对浮点数进行取余运算(`%`). Swift也提供在C语言没有的表达两数之间的区间运算符, (`a..b`和`a...b`),这方便我们表达一个区间的数值. 本章节只描述了Swift中的常规运算符, [高级运算符](http://#)包含了高级运算符,及如何自定义运算符,及自定义类型的运算符重载. +# 术语 -‌# 术语 运算符有一目,双目和三目运算符. 一目运算符对单一操作对象操作, 如`-a`. @@ -22,13 +22,11 @@ Swift支持大部分标准C的运算符, 且改进许多项来获得减少常规 受运算符影响的值叫操作数, 在表达式`1 + 2`中, 加号`+`是双目运算符, 它的两个操作数是值`1`和`2`. - -‌ # 赋值运算符 -赋值运算 `a = b`, 表示用`b`的值来初始化或更新`a`的值. +赋值运算 `a = b`, 表示用 `b` 的值来初始化或更新 `a` 的值. -``` +```swift let b = 10 var a = 5 a = b @@ -42,7 +40,7 @@ let (x, y) = (1, 2) ``` 与C和Objective-C又不同了, Swift的赋值操作并不返回任何值. 所以以下代码是错误的: -``` +```swift if x = y { // 此句错误, 因为 x = y 并不返回任何值 } @@ -57,7 +55,7 @@ Swift让所有数值类型都支持了基本的四则运算: - 乘法 (*) - 除法 (/) -``` +```swift 1 + 2 // 等于 3 5 - 3 // 等于 2 2 * 3 // 等于 6 @@ -68,13 +66,13 @@ Swift让所有数值类型都支持了基本的四则运算: 加法操作也可以用于字符串的拼接: -``` +```swift "hello, " + "world" // 等于 "hello, world" ``` 两个字符类型或一个字符类型和一个字符串类型, 相加会生成一个新的字符串类型: -``` +```swift let dog: Character = "🐶" let cow: Character = "🐮" let dogCow = dog + cow @@ -84,7 +82,7 @@ let dogCow = dog + cow ‌# 求余运算 -求余运算`a % b`是计算`b`的多少倍刚好可以装进`a`, 多出来的那部分叫余数. +求余运算`a % b`是计算 `b` 的多少倍刚好可以装进 `a` , 多出来的那部分叫余数. > 注意 @@ -93,14 +91,14 @@ let dogCow = dog + cow 我们来谈谈取余是怎么回事, 计算 `9 % 4`, 你先计算出4的多少倍会刚好可以装进`9`中. 2, 好的, 余数是1 (用橙色标出) -``` +```swift 传说这里有张求余数的图... 传说这里有张求余数的图... ``` 在Swift中这么来表达 -``` +```swift 9 % 4 // 等于 1 ``` @@ -108,11 +106,11 @@ let dogCow = dog + cow ``` a = (b × `倍数`) + `余数` ``` -当`倍数`取最大值的时候, 就会刚好可以装进`a`中. +当`倍数`取最大值的时候, 就会刚好可以装进 `a` 中. 把 `9` 和 `4` 代入等式中: -``` +```swift 9 = (4 × 2) + 1 ``` @@ -123,134 +121,170 @@ a = (b × `倍数`) + `余数` 把 `-9` 和 4 代入等式: -``` +```swift -9 = (4 × -2) + -1 ``` 等于余数是-1. -在对负数的`b`求余时, `b`的符号会被忽略. 这意味着 `a % b` 和 `a % -b`的结果是相同的. +在对负数的 `b` 求余时, `b`的符号会被忽略. 这意味着 `a % b` 和 `a % -b`的结果是相同的. -# 浮点数的求余计算 +## 浮点数求余计算 -Floating-Point Remainder Calculations -Unlike the remainder operator in C and Objective-C, Swift’s remainder operator can also operate on floating-point numbers: +不同于C和Objective-C, Swift中是可以对浮点数进行求余的. -“8 % 2.5 // equals 0.5 -In this example, 8 divided by 2.5 equals 3, with a remainder of 0.5, so the remainder operator returns a Double value of 0.5. +```swift +8 % 2.5 // 等于 0.5 +``` - +这个例子中, 8除于2.5等于3余0.5, 所以结果是0.5. + +# 自增和自增运算 + +和C一样, Swift也提供了方便对变量本身加1或减1的自增 `++` 和自减 `--` 的运算符. 其操作对象可以是整形和浮点型。 ‌ -Increment and Decrement Operators -Like C, Swift provides an increment operator (++) and a decrement operator (--) as a shortcut to increase or decrease the value of a numeric variable by 1. You can use these operators with variables of any integer or floating-point type. - +``` var i = 0 -++i // i now equals 1 -Each time you call ++i, the value of i is increased by 1. Essentially, ++i is shorthand for saying i = i + 1. Likewise, --i can be used as shorthand for i = i - 1. +++i // 现在 i = 1 +``` -The ++ and -- symbols can be used as prefix operators or as postfix operators. ++i and i++ are both valid ways to increase the value of i by 1. Similarly, --i and i-- are both valid ways to decrease the value of i by 1. +调用一次 `++i`, `i` 的值就会加1. +实际上, `++i` 是 `i = i + 1` 的简写, 而 `--i` 是 `i = i - 1`的简写. -Note that these operators modify i and also return a value. If you only want to increment or decrement the value stored in i, you can ignore the returned value. However, if you do use the returned value, it will be different “based on whether you used the prefix or postfix version of the operator, according to the following rules: +`++` 和 `--`既是前置又是后置运算. `++i`, `i++`, `--i` 和 `i--` 都是有效的写法. -If the operator is written before the variable, it increments the variable before returning its value. -If the operator is written after the variable, it increments the variable after returning its value. -For example: +我们需要注意的是这些运算符修改了 `i` 后有一个返回值. 如果你只想修改 `i` 的值, 那你就忽略这个返回值. 但如果你想使用返回值, 你就要留意前置和后置操作的返回值是不同的. +当 `++` 前置的时候, 先自増再返回. +当 `++` 后置的时候, 先返回再自增. + +不懂? 我们看例子: + +```swift var a = 0 -let b = ++a -// a and b are now both equal to 1 -let c = a++ -// a is now equal to 2, but c has been set to the pre-increment value of 1 -In the example above, let b = ++a increments a before returning its value. This is why both a and b are equal to to the new value of 1. +let b = ++a // a 和 b 现在都是 1 +let c = a++ // a 现在 2, 但 c 是 a 自增前的值 1 +``` -However, let c = a++ increments a after returning its value. This means that c gets the old value of 1, and a is then updated to equal 2. +上述例子, `let b = ++a`, 先把 `a` 加1了再返回 `a` 的值. 所以 `a` 和 `b` 都是新值 `1`. -Unless you need the specific behavior of i++, it is recommended that you use ++i and --i in all cases, because they have the typical expected behavior of modifying i and returning the result. +而 `let c = a++`, 是先返回了 `a` 的值, 然后 `a` 才加1. 所以 `c` 得到了 `a` 的旧值1, 而 `a` 加1后变成2. -“Unary Minus Operator -The sign of a numeric value can be toggled using a prefixed -, known as the unary minus operator: +除非你需要使用 `i++` 的特性, 不然推荐你使用 `++i` 和 `--i`, 因为先修改后返回这样的行为更符合我们的逻辑. + +# 单目负号 + + + +Unary Minus Operator +数值的正负号可以使用前缀 `-` (即单目负号) 来切换: + +```swift let three = 3 -let minusThree = -three // minusThree equals -3 -let plusThree = -minusThree // plusThree equals 3, or "minus minus three" -The unary minus operator (-) is prepended directly before the value it operates on, without any white space. +let minusThree = -three // minusThree 等于 -3 +let plusThree = -minusThree // plusThree 等于 3, o或 "负负3" +``` -‌ -Unary Plus Operator -The unary plus operator (+) simply returns the value it operates on, without any change: +单目负号写在需要操作的值之前, 之间不要空格. +# 单目正号 + +单目正号 `+` 不做任何改变地返回被操作的值. + +```swift let minusSix = -6 -let alsoMinusSix = +minusSix // alsoMinusSix equals -6 -Although the unary plus operator doesn’t actually do anything, you can use it to provide symmetry in your code for positive numbers when also using the unary minus operator for negative numbers. +let alsoMinusSix = +minusSix // alsoMinusSix 等于 -6 +``` +虽然单目`+`做无用功, 但当你在使用单目负号来表达负数时, 你可以使用单目正号来表达正数, 如此你的代码会具有对称美. -“Compound Assignment Operators -Like C, Swift provides compound assignment operators that combine assignment (=) with another operation. One example is the addition assignment operator (+=): +# 复合赋值 + +C的影子又来了, Swift也提供把其他运算符和赋值运算 `=` 组合的复合赋值运算符, 加赋运算 `+=` 是其中一个例子: + +```swift var a = 1 -a += 2 -// a is now equal to 3 -The expression a += 2 is shorthand for a = a + 2. Effectively, the addition and the assignment are combined into one operator that performs both tasks at the same time. +a += 2 // a 现在是 3 +``` -NOTE +表达式 `a += 2` 是 `a = a + 2` 的简写, 一个加赋运算就把加法和赋值两件事完成了. -The compound assignment operators do not return a value. You cannot write let b = a += 2, for example. This behavior is different from the increment and decrement operators mentioned above. +> 注意: -A complete list of compound assignment operators can be found in [Expressions](http://#). +> 复合赋值运算没有返回值, `let b = a += 2` 这类代码是错误. 这不同于上面提到的自增和自减运算符. + +[表达式](http://#)里有复合运算符的完整列表. ‌ +# 比较运算 + +所有标准C中的比较运算都可以在Swift中使用. Comparison Operators Swift supports all standard C comparison operators: -Equal to (a == b) -Not equal to (a != b) +- 等于 `a == b` +- 不等于 `a != b` +- 大于 `a > b` +- 小于 `a < b` +- 大于等于 `a >= b` +- 小于等于 `a <= b` -“Greater than (a > b) -Less than (a < b) -Greater than or equal to (a >= b) -Less than or equal to (a <= b) -NOTE +> 注意: -Swift also provides two identity operators (=== and !==), which you use to test whether two object references both refer to the same object instance. For more information, see [Classes and Structures](http://#). +> Swift也提供恒等 `===` 和不恒等 `!==` 这两个比较符来判断两个对象是否引用同一个对象实例. 更多细节在 [类与结构](Classes and Structures). -Each of the comparison operators returns a Bool value to indicate whether or not the statement is true: +每个比较运算都返回了一个指示表达式是否成立的布尔值: -1 == 1 // true, because 1 is equal to 1 -2 != 1 // true, because 2 is not equal to 1 -2 > 1 // true, because 2 is greater than 1 -1 < 2 // true, because 1 is less than 2 -1 >= 1 // true, because 1 is greater than or equal to 1 -2 <= 1 // false, because 2 is not less than or equal to 1 -Comparison operators are often used in conditional statements, such as the if statement: +```swift +1 == 1 // true, 因为 1 等于 1 +2 != 1 // true, 因为 2 不等于 1 +2 > 1 // true, 因为 2 大于 1 +1 < 2 // true, 因为 1 小于2 +1 >= 1 // true, 因为 1 大于等于 1 +2 <= 1 // false, 因为 2 并不小于等于 1 +``` +比较运算多用于条件语句, 如 `if` 条件: -“let name = "world" +```swift +let name = "world" if name == "world" { println("hello, world") } else { - println("I'm sorry \(name), but I don't recognize you") + println("对不起, \(name), 我不认识你!") } -// prints "hello, world", because name is indeed equal to "world" -For more on the if statement, see Control Flow. +// 输出 "hello, world", 因为 `name` 就是等于 "world" +``` +关于 `if` 语句, 请看 [控制流](Control Flow). -‌ -Ternary Conditional Operator -The ternary conditional operator is a special operator with three parts, which takes the form question ? answer1 : answer2. It is a shortcut for evaluating one of two expressions based on whether question is true or false. If question is true, it evaluates answer1 and returns its value; otherwise, it evaluates answer2 and returns its value. +‌# 三目条件运算 -The ternary conditional operator is shorthand for the code below: +这是一个特殊的有三个部分的运算符, 它的形式是 `问题 ? 答案1 : 答案2`. 它是根据 `问题` 成立与否作出二选一操作的简化表达. 如果 `问题` 成立, 返回 `答案1` 的结果; 如果不成立, 返回 `答案2` 的结果. -if question { - answer1 -} else { - answer2 +三目条件运算是以下代码的精简表达: + +```swift +if question: { + answer1 } -Here’s an example, which calculates the pixel height for a table row. The row height should be 50 pixels taller “than the content height if the row has a header, and 20 pixels taller if the row doesn’t have a header: +else { + answer2 +} +``` +这里有个计算表格行高的例子. 如果有表头, 那行高应比内容高度高出50像素; 如果没有表头, 只需高出20像素. + +```swift let contentHeight = 40 let hasHeader = true let rowHeight = contentHeight + (hasHeader ? 50 : 20) -// rowHeight is equal to 90 -The preceding example is shorthand for the code below: +// rowHeight 现在是 90 +``` +这代码会比下面的代码简单: + +```swift let contentHeight = 40 let hasHeader = true var rowHeight = contentHeight @@ -259,76 +293,89 @@ if hasHeader { } else { rowHeight = rowHeight + 20 } -// rowHeight is equal to 90 -The first example’s use of the ternary conditional operator means that rowHeight can be set to the correct value on a single line of code. This is more concise than the second example, and removes the need for rowHeight to be a variable, because its value does not need to be modified within an if statement. +// rowHeight 现在是 90 +``` -“The ternary conditional operator provides an efficient shorthand for deciding which of two expressions to consider. Use the ternary conditional operator with care, however. Its conciseness can lead to hard-to-read code if overused. Avoid combining multiple instances of the ternary conditional operator into one compound statement. +第一段代码例子使用了三目条件运算, 所以一行代码就能得到正确答案. 这比第二段代码简洁得多, 无需将 `rowHeight` 定义成变量, 因它的值不用在 `if` 语句中改变. +三目条件运算提供有效率且便捷的方式来表达二选一的选择. 需要注意的事, 过度使用三目条件运算就会由简洁的代码变成难懂的代码. 我们应避免在一个组合语句使用多个三目条件运算符. + +# 区间运算符 + +Swift提供了两个方便表达一个区间的值的运算符. + +## 闭区间运算符 +闭区间运算符 `a...b` 定义一个包含从 `a` 到 `b` (包括 `a` 和 `b`)的所有值的区间. ‌ -Range Operators -Swift includes two range operators, which are shortcuts for expressing a range of values. - -‌ -Closed Range Operator -The closed range operator (a...b) defines a range that runs from a to b, and includes the values a and b. - -The closed range operator is useful when iterating over a range in which you want all of the values to be used, such as with a for-in loop: +闭区间运算符在迭代一个区间的所有值时是非常有用的, 如在 `for-in` 循环中: +```swift for index in 1...5 { - println("\(index) times 5 is \(index * 5)") + println("\(index) * 5 = \(index * 5)") } -// 1 times 5 is 5 -// 2 times 5 is 10 -// 3 times 5 is 15 -// 4 times 5 is 20 -// 5 times 5 is 25 -For more on for-in loops, see [Control Flow](http://#). +// 1 * 5 = 5 +// 2 * 5 = 10 +// 3 * 5 = 15 +// 4 * 5 = 20 +// 5 * 5 = 25 +``` -“Half-Closed Range Operator -The half-closed range operator (a..b) defines a range that runs from a to b, but does not include b. It is said to be half-closed because it contains its first value, but not its final value. +关于 `for-in`, 请看 [控制流](Control Flow). -Half-closed ranges are particularly useful when you work with zero-based lists such as arrays, where it is useful to count up to (but not including) the length of the list: +## 半闭区间 +半闭区间 `a..b` 定义一个从 `a` 到 `b` 但不包括 `b` 的区间. +之所以称为半闭区间, 是因为该区间包含第一个值而不包括最后的值. + +半闭区间的实用性在于当你使用一个0始的列表(如数组)时, 非常方便地从0数到列表的长度. + +```swift let names = ["Anna", "Alex", "Brian", "Jack"] let count = names.count for i in 0..count { - println("Person \(i + 1) is called \(names[i])") + println("第 \(i + 1) 个人叫 \(names[i])") } -// Person 1 is called Anna -// Person 2 is called Alex -// Person 3 is called Brian -// Person 4 is called Jack -Note that the array contains four items, but 0..count only counts as far as 3 (the index of the last item in the array), because it is a half-closed range. For more on arrays, see [Arrays](http://#). +// 第 1 个人叫 Anna +// 第 2 个人叫 Alex +// 第 3 个人叫 Brian +// 第 4 个人叫 Jack +``` +> 注意数组有4个元素, 但 `0..count` 只数到 3 (最后一个元素的下标), 因为它是半闭区间. 关于数组, 请查阅 [数组](Arrays). +# 逻辑运算 -“Logical Operators -Logical operators modify or combine the Boolean logic values true and false. Swift supports the three standard logical operators found in C-based languages: +逻辑运算的操作对象是逻辑布尔值. Swift支持基于C语言的三个标准逻辑运算. -Logical NOT (!a) -Logical AND (a && b) -Logical OR (a || b) +- 逻辑非 `!a` +- 逻辑与 `a && b` +- 逻辑或 `a || b` ‌ -Logical NOT Operator -The logical NOT operator (!a) inverts a Boolean value so that true becomes false, and false becomes true. +## 逻辑非 -The logical NOT operator is a prefix operator, and appears immediately before the value it operates on, without any white space. It can be read as “not a”, as seen in the following example: +逻辑非运算 `!a` 对一个布尔值取反, 使得 `true` 变 `false`, `false` 变 `true`. +它是一个前置运算符, 需出现在操作数之前, 且不加空格. 读作 `非 a`, 然后我们看以下例子: + +```swift let allowedEntry = false if !allowedEntry { println("ACCESS DENIED") } // prints "ACCESS DENIED" -The phrase if !allowedEntry can be read as “if not allowed entry.” The subsequent line is only executed if “not allowed entry” is true; that is, if allowedEntry is false. +``` -As in this example, careful choice of Boolean constant and variable names can help to keep code readable and concise, while avoiding double negatives or confusing logic statements. +`if !allowedEntry`语句可以读作 "如果 非 alowed entry.", 接下一行代码只有在如果 "非 allow entry" 为 `true`, 即 `allowEntry` 为 `false` 时被执行. -“Logical AND Operator -The logical AND operator (a && b) creates logical expressions where both values must be true for the overall expression to also be true. +在示例代码中, 小心地选择布尔常量或变量有助于代码的可读性, 并且避免使用双重逻辑非运算, 或混乱的逻辑语句. -If either value is false, the overall expression will also be false. In fact, if the first value is false, the second value won’t even be evaluated, because it can’t possibly make the overall expression equate to true. This is known as short-circuit evaluation. +## 逻辑与 +逻辑与 `a && b` 表达了只有 `a` 和 `b` 的值都为 `true` 时, 整个表达式的值才会 `true` . -This example considers two Bool values and only allows access if both values are true: +只要任意一个值为 `false`, 整个表达式的值就为 `false`. 事实上, 如果第一个值为 `false`, 那么第二个值是不会被计算的, 因为它已经不可能影响整个表达式的结果了. 这被叫做 "短路计算". +以下例子, 只有两个值都为值的时候才允许进入: + +```swift let enteredDoorCode = true let passedRetinaScan = false if enteredDoorCode && passedRetinaScan { @@ -336,15 +383,17 @@ if enteredDoorCode && passedRetinaScan { } else { println("ACCESS DENIED") } -// prints "ACCESS DENIED +// 输出 "ACCESS DENIED +``` -“Logical OR Operator -The logical OR operator (a || b) is an infix operator made from two adjacent pipe characters. You use it to create logical expressions in which only one of the two values has to be true for the overall expression to be true. +逻辑或 +逻辑或 `a || b` 是一个由两个相邻的竖线组成的中置运算符. 它表达了两个逻辑表达式的其中一个为 `true`, 整个表达式就为 `true`. -Like the Logical AND operator above, the Logical OR operator uses short-circuit evaluation to consider its expressions. If the left side of a Logical OR expression is true, the right side is not evaluated, because it cannot change the outcome of the overall expression. +同逻辑与运算类似, 逻辑或也是"短路计算", 当左端的表达式为真是, 右边的表达式就不进行计算了, 因为它也可能改变整个表达式的值了. -In the example below, the first Bool value (hasDoorKey) is false, but the second value (knowsOverridePassword) is true. Because one value is true, the overall expression also evaluates to true, and access is allowed: +以下示例代码中, 第一个布尔值 `hasDoorKey` 为 `false`, 但第二个值 `knowsOverridePassword` 为 `true`, 所以整个表达是 `true`, 于是允许进入: +```swift let hasDoorKey = false let knowsOverridePassword = true if hasDoorKey || knowsOverridePassword { @@ -352,31 +401,40 @@ if hasDoorKey || knowsOverridePassword { } else { println("ACCESS DENIED") } -// prints "Welcome!" -‌ -Combining Logical Operators -You can combine multiple logical operators to create longer compound expressions: +// 输出 "Welcome!" +‌``` -“if enteredDoorCode && passedRetinaScan || hasDoorKey || knowsOverridePassword { +# 组合逻辑 + +我们可以组合多个逻辑运算来表达一个复合逻辑: + +```swift +if enteredDoorCode && passedRetinaScan || hasDoorKey || knowsOverridePassword { println("Welcome!") } else { println("ACCESS DENIED") } -// prints "Welcome!" -This example uses multiple && and || operators to create a longer compound expression. However, the && and || operators still operate on only two values, so this is actually three smaller expressions chained together. It can be read as: +// 输出 "Welcome!" +``` -If we’ve entered the correct door code and passed the retina scan; or if we have a valid door key; or if we know the emergency override password, then allow access. +这个例子使用了含多个 `&&` 和 `||` 的复合逻辑. 但无论怎样, `&&` 和 `&&` 始终只能操作两个值. 所以这实际是三个简单逻辑连续操作的结果. 我们来解读一下: -Based on the values of enteredDoorCode, passedRetinaScan, and hasDoorKey, the first two mini-expressions are false. However, the emergency override password is known, so the overall compound expression still evaluates to true. +如果我们输入了正确的密码并通过了视网膜扫描; 或者我们有一把有效的钥匙; 又或者我们知道紧急情况下重置的密码, 我们就能把门打开进入. -‌ -Explicit Parentheses -It is sometimes useful to include parentheses when they are not strictly needed, to make the intention of a complex expression easier to read. In the door access example above, it is useful to add parentheses around the first part of the compound expression to make its intent explicit: +前两种情况, 我们都不满足, 所以前两个简单逻辑的结果是 `false`, 但是我们知道紧急情况下重置的密码, 所以整个复杂表达式的值还是 `true`. + +‌## 使用括号来明确优先级 + +为了一个复杂表达式更容易读懂, 在合适的地方使用括号来明确优先级是很有效的, 虽然它并不是必要的. 在上个关于门的权限的例子中, 我们给第一个部分加个括号, 使用它看起来更准确. + +```swift if (enteredDoorCode && passedRetinaScan) || hasDoorKey || knowsOverridePassword { println("Welcome!") } else { println("ACCESS DENIED") } // prints "Welcome!" -The parentheses make it clear that the first two values are considered as part of a separate possible state in the overall logic. “The output of the compound expression doesn’t change, but the overall intention is clearer to the reader. Readability is always preferred over brevity; use parentheses where they help to make your intentions clear. +``` + +这括号使得前两个值被看成整个逻辑表达中独立的一个部分. 有括号和没括号的输出结果是一样的, 但对于读代码的人来说有括号的代码更清晰. 可读性比简洁性更重要, 请在可以让你代码变清晰地地方加个括号吧! \ No newline at end of file From 84e3c691deb85223ca503bcbeaaa8aaded1afa52 Mon Sep 17 00:00:00 2001 From: Amin hpc Date: Sat, 7 Jun 2014 15:52:46 +0800 Subject: [PATCH 8/8] finish self review --- source/chapter2/02_Basic_Operators.md | 162 ++++++++++++++------------ 1 file changed, 89 insertions(+), 73 deletions(-) diff --git a/source/chapter2/02_Basic_Operators.md b/source/chapter2/02_Basic_Operators.md index 63964fa5..0d2f50c7 100644 --- a/source/chapter2/02_Basic_Operators.md +++ b/source/chapter2/02_Basic_Operators.md @@ -1,26 +1,26 @@ # 基础运算符 -运算符是检查, 改变, 合并值的特殊符号或短语. 例如, 加号`+`把计算两个数的和(如 `let i = 1 + 2`). 复杂些的运行算包括逻辑与`&&`(如 `if enteredDoorCode && passedRetinaScan`), 还有自增运算符 `++i`, 这一个自身加一的快捷操作. +运算符是检查, 改变, 合并值的特殊符号或短语. 例如, 加号 `+` 把计算两个数的和(如 `let i = 1 + 2`). 复杂些的运行算包括逻辑与`&&`(如 `if enteredDoorCode && passedRetinaScan`), 还有自增运算符 `++i` 这样让自身加一的便捷运算. -Swift支持大部分标准C的运算符, 且改进许多项来获得减少常规编码错误. 赋值符`=`不返回值, 以防止出现错把等号 `==` 写成赋值号 `=` 导致的Bug. 数值运算符(`+`, `-`, `*`, `/`, `%`等)会检测并不允许值溢出, 以此来避免保存变量时由于变量大于或小于其类型所能承载的范围时导致的异常结果. 你可以选择使用Swift的溢出运算符来玩溢出. 具体使用请移步[溢出运算符](http://#). +Swift支持大部分标准C语言的运算符, 且改进许多特性来减少常规编码错误. 如, 赋值符 `=` 不返回值, 以防止错把等号 `==` 写成赋值号 `=` 而导致Bug. 数值运算符( `+` , `-`, `*`, `/`, `%`等)会检测并不允许值溢出, 以此来避免保存变量时由于变量大于或小于其类型所能承载的范围时导致的异常结果. 当然允许你选择使用Swift的溢出运算符来玩溢出. 具体使用请移步[溢出运算符](Overflow Operators). -与C不同, Swift中你可以对浮点数进行取余运算(`%`). Swift也提供在C语言没有的表达两数之间的区间运算符, (`a..b`和`a...b`),这方便我们表达一个区间的数值. +区别于C语言, 在Swift中你可以对浮点数进行取余运算( `%` ), 还提供了C语言没有的表达两数之间的值的区间运算符, ( `a..b` 和 `a...b` ), 这方便我们表达一个区间内的数值. -本章节只描述了Swift中的常规运算符, [高级运算符](http://#)包含了高级运算符,及如何自定义运算符,及自定义类型的运算符重载. +本章节只描述了Swift中的简单运算符, [高级运算符](http://#)包含了高级运算符,及如何自定义运算符, 及如何进行自定义类型的运算符重载. # 术语 -运算符有一目,双目和三目运算符. +运算符有一目, 双目和三目运算符. -一目运算符对单一操作对象操作, 如`-a`. +一目运算符对单一操作对象操作, 如 `-a`. -一目运算符分前置符和后置运算符, 前置运算符需紧排操作对象之前, 如`!b`, 后置运算符需紧跟操作对象之后,如`i++`, +一目运算符分前置符和后置运算符, 前置运算符需紧排操作对象之前, 如 `!b`, 后置运算符需紧跟操作对象之后,如 `i++`, -双目运算符操作两个操作对象, 如`2 + 3`. 是中置的, 因为它们出现在两个操作对象之间. +双目运算符操作两个操作对象, 如 `2 + 3`. 是中置的, 因为它们出现在两个操作对象之间. -三目运算符操作三个操作对象, 跟C一样, Swift只有一个三目运算符, 就是三目条件运算符 `a ? b : c`. +三目运算符操作三个操作对象, 和C语言一样, Swift只有一个三目运算符, 就是三目条件运算符 `a ? b : c`. -受运算符影响的值叫操作数, 在表达式`1 + 2`中, 加号`+`是双目运算符, 它的两个操作数是值`1`和`2`. +受运算符影响的值叫操作数, 在表达式 `1 + 2` 中, 加号 `+` 是双目运算符, 它的两个操作数是值 `1` 和 `2`. # 赋值运算符 @@ -38,7 +38,7 @@ a = b let (x, y) = (1, 2) // 现在 x 等于 1, y 等于 2 ``` -与C和Objective-C又不同了, Swift的赋值操作并不返回任何值. 所以以下代码是错误的: +与C语言和Objective-C不同, Swift的赋值操作并不返回任何值. 所以以下代码是错误的: ```swift if x = y { @@ -46,14 +46,16 @@ if x = y { } ``` -这个特性使得你不能够再把`==`错写成`=`了, 由于`if x = y`是错误代码, Swift从底层帮你避免了这些代码错误. +这个特性使得你不无法把`==`错写成`=`了, 由于`if x = y`是错误代码, Swift从底层帮你避免了这些代码错误. # 数值运算 + Swift让所有数值类型都支持了基本的四则运算: -- 加法 (+) -- 减法 (-) -- 乘法 (*) -- 除法 (/) + +- 加法 `+` +- 减法 `-` +- 乘法 `*` +- 除法 `/` ```swift 1 + 2 // 等于 3 @@ -62,9 +64,9 @@ Swift让所有数值类型都支持了基本的四则运算: 10.0 / 2.5 // 等于 4.0 ``` -又与C和Objective-C不一样了, Swift默认不允许数值运算出现溢出. 但你可以使用Swift的溢出运算符来达到你的目的, (如 `a &+ b` ). 详情请移步: [溢出运算符](http://#). +与C语言和Objective-C不同的是, Swift默认不允许在数值运算中出现溢出情况. 但你可以使用Swift的溢出运算符来达到你有目的的溢出, (如 `a &+ b` ). 详情请移步: [溢出运算符](Overflow Operators). -加法操作也可以用于字符串的拼接: +加法操作 `+` 也用于字符串的拼接: ```swift "hello, " + "world" // 等于 "hello, world" @@ -73,28 +75,44 @@ Swift让所有数值类型都支持了基本的四则运算: 两个字符类型或一个字符类型和一个字符串类型, 相加会生成一个新的字符串类型: ```swift -let dog: Character = "🐶" -let cow: Character = "🐮" +let dog: Character = "d" +let cow: Character = "c" let dogCow = dog + cow -// dogCow 现在是 "🐶🐮" +// 译者注: 原谅的引号内是很可爱的小狗和小牛, 但win os下不支持表情字符, 所以改成了普通字符 +// dogCow 现在是 "dc" ``` 详细请点击 [字符,字符串的拼接](http://#). -‌# 求余运算 +# 求余运算 -求余运算`a % b`是计算 `b` 的多少倍刚好可以装进 `a` , 多出来的那部分叫余数. +求余运算 `a % b` 是计算 `b` 的多少倍刚刚好可以容入 `a` , 多出来的那部分叫余数. > 注意 -> 求余运算(%)在其他语言也叫取模运算. 然而, 鉴于在Swift中该运算符对负数的操作结果, 严格来说, 求余比取模更合适些. +> 求余运算(%)在其他语言也叫取模运算. 然而严格说来, 我们看该运算符对负数的操作结果, `求余` 比 `取模` 更合适些. -我们来谈谈取余是怎么回事, 计算 `9 % 4`, 你先计算出4的多少倍会刚好可以装进`9`中. -2, 好的, 余数是1 (用橙色标出) +我们来谈谈取余是怎么回事, 计算 `9 % 4`, 你先计算出4的多少倍会刚好可以容入 `9` 中. -```swift -传说这里有张求余数的图... -传说这里有张求余数的图... -``` +2倍, 非常好, 那余数是1 (用'*'标出) + + + + + + + + + + + + + + + + + + +
1 2 3 4 5 6 7 8 9
441*
在Swift中这么来表达 @@ -102,32 +120,32 @@ let dogCow = dog + cow 9 % 4 // 等于 1 ``` -为了得到`a % b`的结果, `%`计算了以下等式, 并输出`余数`作为结果: +为了得到 `a % b` 的结果, `%`计算了以下等式, 并输出`余数`作为结果: ``` -a = (b × `倍数`) + `余数` +a = (b × 倍数) + 余数 ``` -当`倍数`取最大值的时候, 就会刚好可以装进 `a` 中. +当`倍数`取最大值的时候, 就会刚好可以容入 `a` 中. -把 `9` 和 `4` 代入等式中: +把 `9` 和 `4` 代入等式中, 我们得 `1`: ```swift 9 = (4 × 2) + 1 ``` -同样的方法, 我来们计算`-9 % 4`: +同样的方法, 我来们计算 `-9 % 4` : ``` --9 % 4 // equals -1 +-9 % 4 // 等于 -1 ``` -把 `-9` 和 4 代入等式: +把 `-9` 和 `4` 代入等式, `-2` 是取到的最大整数: ```swift -9 = (4 × -2) + -1 ``` -等于余数是-1. +余数是 `-1`. -在对负数的 `b` 求余时, `b`的符号会被忽略. 这意味着 `a % b` 和 `a % -b`的结果是相同的. +在对负数 `-b` 求余时, `-b`的符号会被忽略. 这意味着 `a % b` 和 `a % -b`的结果是相同的. ## 浮点数求余计算 @@ -148,14 +166,15 @@ var i = 0 ++i // 现在 i = 1 ``` -调用一次 `++i`, `i` 的值就会加1. +每调用一次 `++i`, `i` 的值就会加1. 实际上, `++i` 是 `i = i + 1` 的简写, 而 `--i` 是 `i = i - 1`的简写. `++` 和 `--`既是前置又是后置运算. `++i`, `i++`, `--i` 和 `i--` 都是有效的写法. -我们需要注意的是这些运算符修改了 `i` 后有一个返回值. 如果你只想修改 `i` 的值, 那你就忽略这个返回值. 但如果你想使用返回值, 你就要留意前置和后置操作的返回值是不同的. +我们需要注意的是这些运算符修改了 `i` 后有一个返回值. 如果你只想修改 `i` 的值, 那你就可以忽略这个返回值. 但如果你想使用返回值, 你就需要留意前置和后置操作的返回值是不同的. 当 `++` 前置的时候, 先自増再返回. + 当 `++` 后置的时候, 先返回再自增. 不懂? 我们看例子: @@ -175,9 +194,6 @@ let c = a++ // a 现在 2, 但 c 是 a 自增前的值 1 # 单目负号 - - -Unary Minus Operator 数值的正负号可以使用前缀 `-` (即单目负号) 来切换: ```swift @@ -186,22 +202,22 @@ let minusThree = -three // minusThree 等于 -3 let plusThree = -minusThree // plusThree 等于 3, o或 "负负3" ``` -单目负号写在需要操作的值之前, 之间不要空格. +单目负号写在操作数之前, 中间没有空格. # 单目正号 -单目正号 `+` 不做任何改变地返回被操作的值. +单目正号 `+` 不做任何改变地返回操作数的值. ```swift let minusSix = -6 let alsoMinusSix = +minusSix // alsoMinusSix 等于 -6 ``` -虽然单目`+`做无用功, 但当你在使用单目负号来表达负数时, 你可以使用单目正号来表达正数, 如此你的代码会具有对称美. +虽然单目 `+` 做无用功, 但当你在使用单目负号来表达负数时, 你可以使用单目正号来表达正数, 如此你的代码会具有对称美. # 复合赋值 -C的影子又来了, Swift也提供把其他运算符和赋值运算 `=` 组合的复合赋值运算符, 加赋运算 `+=` 是其中一个例子: +如同强大的C语言, Swift也提供把其他运算符和赋值运算 `=` 组合的复合赋值运算符, 加赋运算 `+=` 是其中一个例子: ```swift var a = 1 @@ -215,13 +231,10 @@ a += 2 // a 现在是 3 > 复合赋值运算没有返回值, `let b = a += 2` 这类代码是错误. 这不同于上面提到的自增和自减运算符. [表达式](http://#)里有复合运算符的完整列表. - ‌ # 比较运算 所有标准C中的比较运算都可以在Swift中使用. -Comparison Operators -Swift supports all standard C comparison operators: - 等于 `a == b` - 不等于 `a != b` @@ -234,7 +247,7 @@ Swift supports all standard C comparison operators: > Swift也提供恒等 `===` 和不恒等 `!==` 这两个比较符来判断两个对象是否引用同一个对象实例. 更多细节在 [类与结构](Classes and Structures). -每个比较运算都返回了一个指示表达式是否成立的布尔值: +每个比较运算都返回了一个标识表达式是否成立的布尔值: ```swift 1 == 1 // true, 因为 1 等于 1 @@ -256,13 +269,14 @@ if name == "world" { } // 输出 "hello, world", 因为 `name` 就是等于 "world" ``` + 关于 `if` 语句, 请看 [控制流](Control Flow). -‌# 三目条件运算 +# 三目条件运算 -这是一个特殊的有三个部分的运算符, 它的形式是 `问题 ? 答案1 : 答案2`. 它是根据 `问题` 成立与否作出二选一操作的简化表达. 如果 `问题` 成立, 返回 `答案1` 的结果; 如果不成立, 返回 `答案2` 的结果. +三目条件运算的特殊在于它是有三个操作数的运算符, 它的原型是 `问题 ? 答案1 : 答案2`. 它简洁地表达根据 `问题` 成立与否作出二选一的操作. 如果 `问题` 成立, 返回 `答案1` 的结果; 如果不成立, 返回 `答案2` 的结果. -三目条件运算是以下代码的精简表达: +使用三目条件运算简化了以下代码: ```swift if question: { @@ -273,7 +287,7 @@ else { } ``` -这里有个计算表格行高的例子. 如果有表头, 那行高应比内容高度高出50像素; 如果没有表头, 只需高出20像素. +这里有个计算表格行高的例子. 如果有表头, 那行高应比内容高度要高出50像素; 如果没有表头, 只需高出20像素. ```swift let contentHeight = 40 @@ -282,7 +296,7 @@ let rowHeight = contentHeight + (hasHeader ? 50 : 20) // rowHeight 现在是 90 ``` -这代码会比下面的代码简单: +这样写会比下面的代码简洁: ```swift let contentHeight = 40 @@ -296,7 +310,7 @@ if hasHeader { // rowHeight 现在是 90 ``` -第一段代码例子使用了三目条件运算, 所以一行代码就能得到正确答案. 这比第二段代码简洁得多, 无需将 `rowHeight` 定义成变量, 因它的值不用在 `if` 语句中改变. +第一段代码例子使用了三目条件运算, 所以一行代码就能让我们得到正确答案. 这比第二段代码简洁得多, 无需将 `rowHeight` 定义成变量, 因为它的值无需在 `if` 语句中改变. 三目条件运算提供有效率且便捷的方式来表达二选一的选择. 需要注意的事, 过度使用三目条件运算就会由简洁的代码变成难懂的代码. 我们应避免在一个组合语句使用多个三目条件运算符. @@ -340,7 +354,8 @@ for i in 0..count { // 第 3 个人叫 Brian // 第 4 个人叫 Jack ``` -> 注意数组有4个元素, 但 `0..count` 只数到 3 (最后一个元素的下标), 因为它是半闭区间. 关于数组, 请查阅 [数组](Arrays). + +> 注意: 数组有4个元素, 但 `0..count` 只数到 3 (最后一个元素的下标), 因为它是半闭区间. 关于数组, 请查阅 [数组](Arrays). # 逻辑运算 @@ -349,7 +364,7 @@ for i in 0..count { - 逻辑非 `!a` - 逻辑与 `a && b` - 逻辑或 `a || b` -‌ + ## 逻辑非 逻辑非运算 `!a` 对一个布尔值取反, 使得 `true` 变 `false`, `false` 变 `true`. @@ -369,9 +384,9 @@ if !allowedEntry { 在示例代码中, 小心地选择布尔常量或变量有助于代码的可读性, 并且避免使用双重逻辑非运算, 或混乱的逻辑语句. ## 逻辑与 -逻辑与 `a && b` 表达了只有 `a` 和 `b` 的值都为 `true` 时, 整个表达式的值才会 `true` . +逻辑与 `a && b` 表达了只有 `a` 和 `b` 的值都为 `true` 时, 整个表达式的值才会是 `true` . -只要任意一个值为 `false`, 整个表达式的值就为 `false`. 事实上, 如果第一个值为 `false`, 那么第二个值是不会被计算的, 因为它已经不可能影响整个表达式的结果了. 这被叫做 "短路计算". +只要任意一个值为 `false`, 整个表达式的值就为 `false`. 事实上, 如果第一个值为 `false`, 那么是不去计算第二个值的, 因为它已经不可能影响整个表达式的结果了. 这被称做 "短路计算". 以下例子, 只有两个值都为值的时候才允许进入: @@ -386,10 +401,10 @@ if enteredDoorCode && passedRetinaScan { // 输出 "ACCESS DENIED ``` -逻辑或 -逻辑或 `a || b` 是一个由两个相邻的竖线组成的中置运算符. 它表达了两个逻辑表达式的其中一个为 `true`, 整个表达式就为 `true`. +## 逻辑或 +逻辑或 `a || b` 是一个由两个连续的 `|` 组成的中置运算符. 它表示了两个逻辑表达式的其中一个为 `true`, 整个表达式就为 `true`. -同逻辑与运算类似, 逻辑或也是"短路计算", 当左端的表达式为真是, 右边的表达式就不进行计算了, 因为它也可能改变整个表达式的值了. +同逻辑与运算类似, 逻辑或也是"短路计算"的, 当左端的表达式为 `true` 时, 将不计算右边的表达式了, 因为它不可能改变整个表达式的值了. 以下示例代码中, 第一个布尔值 `hasDoorKey` 为 `false`, 但第二个值 `knowsOverridePassword` 为 `true`, 所以整个表达是 `true`, 于是允许进入: @@ -402,9 +417,9 @@ if hasDoorKey || knowsOverridePassword { println("ACCESS DENIED") } // 输出 "Welcome!" -‌``` +``` -# 组合逻辑 +## 组合逻辑 我们可以组合多个逻辑运算来表达一个复合逻辑: @@ -417,16 +432,15 @@ if enteredDoorCode && passedRetinaScan || hasDoorKey || knowsOverridePassword { // 输出 "Welcome!" ``` -这个例子使用了含多个 `&&` 和 `||` 的复合逻辑. 但无论怎样, `&&` 和 `&&` 始终只能操作两个值. 所以这实际是三个简单逻辑连续操作的结果. 我们来解读一下: +这个例子使用了含多个 `&&` 和 `||` 的复合逻辑. 但无论怎样, `&&` 和 `||` 始终只能操作两个值. 所以这实际是三个简单逻辑连续操作的结果. 我们来解读一下: 如果我们输入了正确的密码并通过了视网膜扫描; 或者我们有一把有效的钥匙; 又或者我们知道紧急情况下重置的密码, 我们就能把门打开进入. -前两种情况, 我们都不满足, 所以前两个简单逻辑的结果是 `false`, 但是我们知道紧急情况下重置的密码, 所以整个复杂表达式的值还是 `true`. +前两种情况, 我们都不满足, 所以前两个简单逻辑的结果是 `false`, 但是我们是知道紧急情况下重置的密码的, 所以整个复杂表达式的值还是 `true`. +## 使用括号来明确优先级 -‌## 使用括号来明确优先级 - -为了一个复杂表达式更容易读懂, 在合适的地方使用括号来明确优先级是很有效的, 虽然它并不是必要的. 在上个关于门的权限的例子中, 我们给第一个部分加个括号, 使用它看起来更准确. +为了一个复杂表达式更容易读懂, 在合适的地方使用括号来明确优先级是很有效的, 虽然它并非必要的. 在上个关于门的权限的例子中, 我们给第一个部分加个括号, 使用它看起来逻辑更明确. ```swift if (enteredDoorCode && passedRetinaScan) || hasDoorKey || knowsOverridePassword { @@ -437,4 +451,6 @@ if (enteredDoorCode && passedRetinaScan) || hasDoorKey || knowsOverridePassword // prints "Welcome!" ``` -这括号使得前两个值被看成整个逻辑表达中独立的一个部分. 有括号和没括号的输出结果是一样的, 但对于读代码的人来说有括号的代码更清晰. 可读性比简洁性更重要, 请在可以让你代码变清晰地地方加个括号吧! \ No newline at end of file +这括号使得前两个值被看成整个逻辑表达中独立的一个部分. 虽然有括号和没括号的输出结果是一样的, 但对于读代码的人来说有括号的代码更清晰. + +可读性比简洁性更重要, 请在可以让你代码变清晰地地方加个括号吧! \ No newline at end of file