From 4976c25347b5ab3c74b9e92725a0572bfac22d84 Mon Sep 17 00:00:00 2001 From: Sketchk Date: Thu, 12 May 2016 20:50:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=A1=E6=AD=A3+=E7=BF=BB=E8=AF=91=20From=20?= =?UTF-8?q?SketchK=20=E4=B8=BB=E8=A6=81=E5=86=85=E5=AE=B9=201=20`=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E6=B5=81`=E7=AB=A0=E8=8A=82=E4=B8=AD=E7=9A=84`while?= =?UTF-8?q?=E5=BE=AA=E7=8E=AF`=E4=B8=80=E8=8A=82=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=9B=B4=E6=96=B0(=E5=BA=9F=E5=BC=83?= =?UTF-8?q?=E4=BA=86++)=202=20`=E6=8E=A7=E5=88=B6=E6=B5=81`=E7=AB=A0?= =?UTF-8?q?=E8=8A=82=E4=B8=AD=E7=9A=84`repeat-while=E5=BE=AA=E7=8E=AF`?= =?UTF-8?q?=E4=B8=80=E8=8A=82=E4=B8=AD=E7=9A=84=E4=BB=A3=E7=A0=81=E6=9B=B4?= =?UTF-8?q?=E6=96=B0(=E5=BA=9F=E5=BC=83=E4=BA=86++)=203=20`=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E6=B5=81`=E7=AB=A0=E8=8A=82=E4=B8=AD=E7=9A=84`if`?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=9C=89=E8=AF=AF(=E5=8E=9F=E6=96=87?= =?UTF-8?q?=E4=B8=AD=E4=B8=8E=E5=BD=93=E5=89=8D=E7=BF=BB=E8=AF=91=E4=B8=AD?= =?UTF-8?q?=E6=95=B0=E5=80=BC=E4=B8=8D=E7=9B=B8=E7=AC=A6)=204=20`=E5=87=BD?= =?UTF-8?q?=E6=95=B0`=E7=AB=A0=E8=8A=82=E4=B8=AD`=E5=B8=B8=E9=87=8F?= =?UTF-8?q?=E4=B8=8E=E5=8F=98=E9=87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=87=BD=E6=95=B0`=E4=B8=80=E8=8A=82=E5=86=85=E5=AE=B9?= =?UTF-8?q?=E7=BF=BB=E8=AF=91=E6=9C=89=E8=AF=AF(=E5=8E=9F=E6=96=87?= =?UTF-8?q?=E4=B8=8E=E5=BD=93=E5=89=8D=E7=BF=BB=E8=AF=91=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E4=B8=8D=E7=9B=B8=E7=AC=A6)=208=20`=E9=97=AD?= =?UTF-8?q?=E5=8C=85`=E7=AB=A0=E8=8A=82=E4=B8=AD`=E5=B0=BE=E9=9A=8F?= =?UTF-8?q?=E9=97=AD=E5=8C=85`=E4=B8=80=E8=8A=82=E5=86=85=E5=AE=B9?= =?UTF-8?q?=E6=9B=B4=E6=96=B0(=E5=BA=9F=E5=BC=83=E4=BA=86var=E4=BF=AE?= =?UTF-8?q?=E9=A5=B0=E7=AC=A6,=E5=8E=9F=E6=96=87=E4=B8=8E=E5=BD=93?= =?UTF-8?q?=E5=89=8D=E7=BF=BB=E8=AF=91=E4=B8=AD=E5=86=85=E5=AE=B9=E4=B8=8D?= =?UTF-8?q?=E7=AC=A6,)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/chapter1/02_a_swift_tour.md | 2 +- source/chapter2/02_Basic_Operators.md | 2 +- source/chapter2/03_Strings_and_Characters.md | 5 +- source/chapter2/04_Collection_Types.md | 6 ++- source/chapter2/05_Control_Flow.md | 10 ++-- source/chapter2/06_Functions.md | 50 +++----------------- source/chapter2/07_Closures.md | 8 +++- 7 files changed, 30 insertions(+), 53 deletions(-) diff --git a/source/chapter1/02_a_swift_tour.md b/source/chapter1/02_a_swift_tour.md index f2b5c441..b646e3d7 100755 --- a/source/chapter1/02_a_swift_tour.md +++ b/source/chapter1/02_a_swift_tour.md @@ -10,7 +10,7 @@ > 翻译+校对:[xtymichael](https://github.com/xtymichael) > 2.2 -> 翻译:[175](https://github.com/Brian175),2016-04-09 +> 翻译:[175](https://github.com/Brian175),2016-04-09 校对:[SketchK](https://github.com/SketchK),2016-05-11 本页内容包括: diff --git a/source/chapter2/02_Basic_Operators.md b/source/chapter2/02_Basic_Operators.md index 70984fb6..cac08640 100755 --- a/source/chapter2/02_Basic_Operators.md +++ b/source/chapter2/02_Basic_Operators.md @@ -12,7 +12,7 @@ > 校对:[shanks](http://codebuild.me) > 2.2 -> 翻译+校对:[Cee](https://github.com/Cee) +> 翻译+校对:[Cee](https://github.com/Cee) 校对:[SketchK](https://github.com/SketchK),2016-05-11 本页包含内容: diff --git a/source/chapter2/03_Strings_and_Characters.md b/source/chapter2/03_Strings_and_Characters.md index ae58c599..e86dc29f 100755 --- a/source/chapter2/03_Strings_and_Characters.md +++ b/source/chapter2/03_Strings_and_Characters.md @@ -10,7 +10,10 @@ > 2.1 > 翻译:[DianQK](https://github.com/DianQK) -> 校对:[shanks](http://codebuild.me), [Realank](https://github.com/Realank) +> 校对:[shanks](http://codebuild.me), [Realank](https://github.com/Realank), + +> 2.2 +> 校对:[SketchK](https://github.com/SketchK) 2016-05-11 本页包含内容: diff --git a/source/chapter2/04_Collection_Types.md b/source/chapter2/04_Collection_Types.md index 8f862b2d..dd2d0567 100755 --- a/source/chapter2/04_Collection_Types.md +++ b/source/chapter2/04_Collection_Types.md @@ -9,7 +9,11 @@ > 翻译+校对:[JackAlan](https://github.com/AlanMelody) > 2.1 -> 校对:[shanks](http://codebuild.me) +> 校对:[shanks](http://codebuild.me) + +> 2.2 +> 校对:[SketchK](https://github.com/SketchK) 2016-05-11 + 本页包含内容: diff --git a/source/chapter2/05_Control_Flow.md b/source/chapter2/05_Control_Flow.md index 012d6920..c3e2e3d3 100755 --- a/source/chapter2/05_Control_Flow.md +++ b/source/chapter2/05_Control_Flow.md @@ -14,7 +14,7 @@ > 2.2 > 翻译:[LinusLing](https://github.com/linusling) -> 校对:[]() +> 校对:[SketchK](https://github.com/SketchK) 2016-05-12 本页包含内容: @@ -150,7 +150,8 @@ var square = 0 var diceRoll = 0 while square < finalSquare { // 掷骰子 - if ++diceRoll == 7 { diceRoll = 1 } + diceRoll += 1 + if diceRoll == 7 { diceRoll = 1 } // 根据点数移动 square += diceRoll if square < board.count { @@ -209,7 +210,8 @@ repeat { // 顺着梯子爬上去或者顺着蛇滑下去 square += board[square] // 掷骰子 - if ++diceRoll == 7 { diceRoll = 1 } + diceRoll += 1 + if diceRoll == 7 { diceRoll = 1 } // 根据点数移动 square += diceRoll } while square < finalSquare @@ -275,7 +277,7 @@ if temperatureInFahrenheit <= 32 { 实际上,最后的`else`语句是可选的: ```swift -temperatureInFahrenheit = 90 +temperatureInFahrenheit = 72 if temperatureInFahrenheit <= 32 { print("It's very cold. Consider wearing a scarf.") } else if temperatureInFahrenheit >= 86 { diff --git a/source/chapter2/06_Functions.md b/source/chapter2/06_Functions.md index cb081456..a63abec2 100755 --- a/source/chapter2/06_Functions.md +++ b/source/chapter2/06_Functions.md @@ -12,6 +12,9 @@ > 翻译:[DianQK](https://github.com/DianQK) > 定稿:[shanks](http://codebuild.me) +> 2.2 +> 翻译+校对:[SketchK](https://github.com/SketchK) 2016-05-12 + 本页包含内容: - [函数定义与调用(Defining and Calling Functions)](#Defining_and_Calling_Functions) @@ -330,57 +333,18 @@ arithmeticMean(3, 8.25, 18.75) 如果函数有一个或多个带默认值的参数,而且还有一个可变参数,那么把可变参数放在参数表的最后。 - -### 常量参数和变量参数(Constant and Variable Parameters) - -函数参数默认是常量。试图在函数体中更改参数值将会导致编译错误。这意味着你不能错误地更改参数值。 - -但是,有时候,如果函数中有传入参数的变量值副本将是很有用的。你可以通过指定一个或多个参数为变量参数,从而避免自己在函数中定义新的变量。变量参数不是常量,你可以在函数中把它当做新的可修改副本来使用。 - -通过在参数名前加关键字 `var` 来定义变量参数: - -```swift -func alignRight(var string: String, totalLength: Int, pad: Character) -> String { - let amountToPad = totalLength - string.characters.count - if amountToPad < 1 { - return string - } - let padString = String(pad) - for _ in 1...amountToPad { - string = padString + string - } - return string -} -let originalString = "hello" -let paddedString = alignRight(originalString, totalLength: 10, pad: "-") -// paddedString is equal to "-----hello" -// originalString is still equal to "hello" -``` - -这个例子中定义了一个叫做 `alignRight(_:totalLength:pad:)` 的新函数,用来将输入的字符串对齐到更长的输出字符串的右边缘。左侧空余的地方用指定的填充字符填充。这个例子中,字符串`"hello"`被转换成了`"-----hello"`。 - -`alignRight(_:totalLength:pad:)` 函数将输入参数 `string` 定义为变量参数。这意味着 `string` 现在可以作为一个局部变量,被传入的字符串值初始化,并且可以在函数体中进行操作。 - -函数首先计算出有多少字符需要被添加到`string`的左边,从而将其在整个字符串中右对齐。这个值存储在一个称为`amountToPad`的本地常量。如果不需要填充(也就是说,如果`amountToPad`小于1),该函数简单地返回没有任何填充的输入值`string`。 - -否则,该函数用`pad`字符创建一个叫做`padString`的临时`String`常量,并将`amountToPad`个 `padString`添加到现有字符串的左边。(一个`String`值不能被添加到一个`Character`值上,所以`padString`常量用于确保`+`操作符两侧都是`String`值)。 - -> 注意 -> 对变量参数所进行的修改在函数调用结束后便消失了,并且对于函数体外是不可见的。变量参数仅仅存在于函数调用的生命周期中。 - - ### 输入输出参数(In-Out Parameters) -变量参数,正如上面所述,仅仅能在函数体内被更改。如果你想要一个函数可以修改参数的值,并且想要在这些修改在函数调用结束后仍然存在,那么就应该把这个参数定义为输入输出参数(In-Out Parameters)。 +函数参数默认是常量。试图在函数体中更改参数值将会导致编译错误。这意味着你不能错误地更改参数值。如果你想要一个函数可以修改参数的值,并且想要在这些修改在函数调用结束后仍然存在,那么就应该把这个参数定义为输入输出参数(In-Out Parameters)。 -定义一个输入输出参数时,在参数定义前加 `inout` 关键字。一个输入输出参数有传入函数的值,这个值被函数修改,然后被传出函数,替换原来的值。想获取更多的关于输入输出参数的细节和相关的编译器优化,请查看[输入输出参数](../chapter3/05_Declarations.html#function_declaration)一节。 - +定义一个输入输出参数时,在参数定义前加 inout 关键字。一个输入输出参数有传入函数的值,这个值被函数修改,然后被传出函数,替换原来的值。想获取更多的关于输入输出参数的细节和相关的编译器优化,请查看[输入输出参数](../chapter3/05_Declarations.html#function_declaration)一节。 + 你只能传递变量给输入输出参数。你不能传入常量或者字面量(literal value),因为这些量是不能被修改的。当传入的参数作为输入输出参数时,需要在参数名前加`&`符,表示这个值可以被函数修改。 > 注意 -> 输入输出参数不能有默认值,而且可变参数不能用 `inout` 标记。如果你用 `inout` 标记一个参数,这个参数不能被 `var` 或者 `let` 标记。 +> 输入输出参数不能有默认值,而且可变参数不能用 `inout` 标记。 下面是例子,`swapTwoInts(_:_:)` 函数,有两个分别叫做 `a` 和 `b` 的输入输出参数: diff --git a/source/chapter2/07_Closures.md b/source/chapter2/07_Closures.md index ed304d76..b3c73c1e 100755 --- a/source/chapter2/07_Closures.md +++ b/source/chapter2/07_Closures.md @@ -11,6 +11,9 @@ > 2.1 > 翻译:[100mango](https://github.com/100mango), [magicdict](https://github.com/magicdict) > 校对:[shanks](http://codebuild.me) +> +> 2.2 +> 翻译+校对:[SketchK](https://github.com/SketchK) 2016-05-12 本页包含内容: @@ -213,7 +216,8 @@ let numbers = [16, 58, 510] ```swift let strings = numbers.map { - (var number) -> String in + (number) -> String in + var number = number var output = "" while number > 0 { output = digitNames[number % 10]! + output @@ -227,7 +231,7 @@ let strings = numbers.map { `map(_:)`为数组中每一个元素调用了闭包表达式。您不需要指定闭包的输入参数`number`的类型,因为可以通过要映射的数组类型进行推断。 -在该例中,闭包`number`参数被声明为一个变量参数(变量的具体描述请参看[常量参数和变量参数](./06_Functions.html#constant_and_variable_parameters)),因此可以在闭包函数体内对其进行修改,而不用再定义一个新的局部变量并将`number`的值赋值给它。闭包表达式指定了返回类型为`String`,以表明存储映射值的新数组类型为`String`。 +在该例中,局部变量`number`的值由闭包中的`number`参数获得,因此可以在闭包函数体内对其进行修改,(闭包或者函数的参数总是固定的),闭包表达式指定了返回类型为`String`,以表明存储映射值的新数组类型为`String`。 闭包表达式在每次被调用的时候创建了一个叫做`output`的字符串并返回。其使用求余运算符(`number % 10`)计算最后一位数字并利用`digitNames`字典获取所映射的字符串。