diff --git a/chapter2/06_Functions.html b/chapter2/06_Functions.html index 2d0fc662..a9991972 100644 --- a/chapter2/06_Functions.html +++ b/chapter2/06_Functions.html @@ -840,7 +840,7 @@ let paddedString = alignRight(originalString, 10, "-") a = b b = temporaryA } -

这个 swapTwoInts 函数仅仅交换 ab 的值。该函数先将 a 的值存到一个暂时常量 temporaryA 中,然后将 b 的值赋给 a,最后将 temporaryA 幅值给 b

+

这个 swapTwoInts 函数仅仅交换 ab 的值。该函数先将 a 的值存到一个暂时常量 temporaryA 中,然后将 b 的值赋给 a,最后将 temporaryA 赋值给 b

你可以用两个 Int 型的变量来调用 swapTwoInts。需要注意的是,someIntanotherInt 在传入 swapTwoInts 函数前,都加了 & 的前缀:

var someInt = 3
 var anotherInt = 107
@@ -870,7 +870,7 @@ func multiplyTwoInts(a: Int, b: Int) -> Int {
 }
 

这个函数的类型是:() -> (),或者叫“没有参数,并返回 Void 类型的函数。”。没有指定返回类型的函数总返回 Void。在Swift中,Void 与空的元组是一样的。

使用函数类型(Using Function Types)

-

在Swift中,使用函数类型就像使用其他类型一样。例如,你可以定义一个常量或变量,它的类型是函数,并且可以幅值为一个函数:

+

在Swift中,使用函数类型就像使用其他类型一样。例如,你可以定义一个常量或变量,它的类型是函数,并且可以赋值为一个函数:

var mathFunction: (Int, Int) -> Int = addTwoInts
 

这个可以读作:

“定义一个叫做 mathFunction 的变量,类型是‘一个有两个 Int 型的参数并返回一个 Int 型的值的函数’,并让这个新变量指向 addTwoInts 函数”。