diff --git a/source-tw/chapter2/04_Collection_Types.md b/source-tw/chapter2/04_Collection_Types.md index 0e9d2948..5ae5b93e 100644 --- a/source-tw/chapter2/04_Collection_Types.md +++ b/source-tw/chapter2/04_Collection_Types.md @@ -1,5 +1,5 @@ > 翻譯:[zqp](https://github.com/zqp) -> 校對:[shinyzhu](https://github.com/shinyzhu), [stanzhai](https://github.com/stanzhai) +> 校對:[shinyzhu](https://github.com/shinyzhu), [stanzhai](https://github.com/stanzhai), [feiin](https://github.com/feiin) # 集合類型 (Collection Types) ----------------- @@ -27,7 +27,7 @@ Swift 數組特定於它所存儲元素的類型。這與 Objective-C 的 NSArra ### 數組的簡單語法 -寫 Swift 數組應該遵循像`Array`這樣的形式,其中`SomeType`是這個數組中唯一允許存在的數據類型。 我們也可以使用像`SomeType[]`這樣的簡單語法。 儘管兩種形式在功能上是一樣的,但是推薦較短的那種,而且在本文中都會使用這種形式來使用數組。 +寫 Swift 數組應該遵循像`Array`這樣的形式,其中`SomeType`是這個數組中唯一允許存在的數據類型。 我們也可以使用像`[SomeType]`這樣的簡單語法。 儘管兩種形式在功能上是一樣的,但是推薦較短的那種,而且在本文中都會使用這種形式來使用數組。 ### 數組構造語句 @@ -38,11 +38,11 @@ Swift 數組特定於它所存儲元素的類型。這與 Objective-C 的 NSArra 下面這個例子創建了一個叫做`shoppingList`並且存儲字符串的數組: ```swift -var shoppingList: String[] = ["Eggs", "Milk"] +var shoppingList: [String] = ["Eggs", "Milk"] // shoppingList 已經被構造並且擁有兩個初始項。 ``` -`shoppingList`變量被聲明為「字符串值類型的數組「,記作`String[]`。 因為這個數組被規定只有`String`一種數據結構,所以只有`String`類型可以在其中被存取。 在這裡,`shoppinglist`數組由兩個`String`值(`"Eggs"` 和`"Milk"`)構造,並且由字面量定義。 +`shoppingList`變量被聲明為「字符串值類型的數組「,記作`[String]`。 因為這個數組被規定只有`String`一種數據結構,所以只有`String`類型可以在其中被存取。 在這裡,`shoppinglist`數組由兩個`String`值(`"Eggs"` 和`"Milk"`)構造,並且由字面量定義。 > 注意: > `Shoppinglist`數組被聲明為變量(`var`關鍵字創建)而不是常量(`let`創建)是因為以後可能會有更多的數據項被插入其中。 @@ -55,7 +55,7 @@ var shoppingList: String[] = ["Eggs", "Milk"] var shoppingList = ["Eggs", "Milk"] ``` -因為所有字面量中的值都是相同的類型,Swift 可以推斷出`String[]`是`shoppinglist`中變量的正確類型。 +因為所有字面量中的值都是相同的類型,Swift 可以推斷出`[String]`是`shoppinglist`中變量的正確類型。 ### 訪問和修改數組 @@ -198,12 +198,12 @@ for (index, value) in enumerate(shoppingList) { 我們可以使用構造語法來創建一個由特定數據類型構成的空數組: ```swift -var someInts = Int[]() -println("someInts is of type Int[] with \(someInts.count) items。") -// 打印 "someInts is of type Int[] with 0 items。"(someInts是0數據項的Int[]數組) +var someInts = [Int]() +println("someInts is of type [Int] with \(someInts.count) items。") +// 打印 "someInts is of type [Int] with 0 items。"(someInts是0數據項的Int[]數組) ``` -注意`someInts`被設置為一個`Int[]`構造函數的輸出所以它的變量類型被定義為`Int[]`。 +注意`someInts`被設置為一個`[Int]`構造函數的輸出所以它的變量類型被定義為`[Int]`。 除此之外,如果代碼上下文中提供了類型信息, 例如一個函數參數或者一個已經定義好類型的常量或者變量,我們可以使用空數組語句創建一個空數組,它的寫法很簡單:`[]`(一對空方括號): @@ -211,28 +211,28 @@ println("someInts is of type Int[] with \(someInts.count) items。") someInts.append(3) // someInts 現在包含一個INT值 someInts = [] -// someInts 現在是空數組,但是仍然是Int[]類型的。 +// someInts 現在是空數組,但是仍然是[Int]類型的。 ``` Swift 中的`Array`類型還提供一個可以創建特定大小並且所有數據都被默認的構造方法。我們可以把準備加入新數組的數據項數量(`count`)和適當類型的初始值(`repeatedValue`)傳入數組構造函數: ```swift -var threeDoubles = Double[](count: 3, repeatedValue:0.0) -// threeDoubles 是一種 Double[]數組, 等於 [0.0, 0.0, 0.0] +var threeDoubles = [Double](count: 3, repeatedValue:0.0) +// threeDoubles 是一種 [Double]數組, 等於 [0.0, 0.0, 0.0] ``` 因為類型推斷的存在,我們使用這種構造方法的時候不需要特別指定數組中存儲的數據類型,因為類型可以從默認值推斷出來: ```swift var anotherThreeDoubles = Array(count: 3, repeatedValue: 2.5) -// anotherThreeDoubles is inferred as Double[], and equals [2.5, 2.5, 2.5] +// anotherThreeDoubles is inferred as [Double], and equals [2.5, 2.5, 2.5] ``` 最後,我們可以使用加法操作符(`+`)來組合兩種已存在的相同類型數組。新數組的數據類型會被從兩個數組的數據類型中推斷出來: ```swift var sixDoubles = threeDoubles + anotherThreeDoubles -// sixDoubles 被推斷為 Double[], 等於 [0.0, 0.0, 0.0, 2.5, 2.5, 2.5] +// sixDoubles 被推斷為 [Double], 等於 [0.0, 0.0, 0.0, 2.5, 2.5, 2.5] ``` diff --git a/source/chapter2/04_Collection_Types.md b/source/chapter2/04_Collection_Types.md index 7578b3a5..940c953d 100755 --- a/source/chapter2/04_Collection_Types.md +++ b/source/chapter2/04_Collection_Types.md @@ -1,5 +1,5 @@ > 翻译:[zqp](https://github.com/zqp) -> 校对:[shinyzhu](https://github.com/shinyzhu), [stanzhai](https://github.com/stanzhai) +> 校对:[shinyzhu](https://github.com/shinyzhu), [stanzhai](https://github.com/stanzhai), [feiin](https://github.com/feiin) # 集合类型 (Collection Types) ----------------- @@ -27,7 +27,7 @@ Swift 数组特定于它所存储元素的类型。这与 Objective-C 的 NSArra ### 数组的简单语法 -写 Swift 数组应该遵循像`Array`这样的形式,其中`SomeType`是这个数组中唯一允许存在的数据类型。 我们也可以使用像`SomeType[]`这样的简单语法。 尽管两种形式在功能上是一样的,但是推荐较短的那种,而且在本文中都会使用这种形式来使用数组。 +写 Swift 数组应该遵循像`Array`这样的形式,其中`SomeType`是这个数组中唯一允许存在的数据类型。 我们也可以使用像`[SomeType]`这样的简单语法。 尽管两种形式在功能上是一样的,但是推荐较短的那种,而且在本文中都会使用这种形式来使用数组。 ### 数组构造语句 @@ -38,11 +38,11 @@ Swift 数组特定于它所存储元素的类型。这与 Objective-C 的 NSArra 下面这个例子创建了一个叫做`shoppingList`并且存储字符串的数组: ```swift -var shoppingList: String[] = ["Eggs", "Milk"] +var shoppingList: [String] = ["Eggs", "Milk"] // shoppingList 已经被构造并且拥有两个初始项。 ``` -`shoppingList`变量被声明为“字符串值类型的数组“,记作`String[]`。 因为这个数组被规定只有`String`一种数据结构,所以只有`String`类型可以在其中被存取。 在这里,`shoppinglist`数组由两个`String`值(`"Eggs"` 和`"Milk"`)构造,并且由字面量定义。 +`shoppingList`变量被声明为“字符串值类型的数组“,记作`[String]`。 因为这个数组被规定只有`String`一种数据结构,所以只有`String`类型可以在其中被存取。 在这里,`shoppinglist`数组由两个`String`值(`"Eggs"` 和`"Milk"`)构造,并且由字面量定义。 > 注意: > `Shoppinglist`数组被声明为变量(`var`关键字创建)而不是常量(`let`创建)是因为以后可能会有更多的数据项被插入其中。 @@ -55,7 +55,7 @@ var shoppingList: String[] = ["Eggs", "Milk"] var shoppingList = ["Eggs", "Milk"] ``` -因为所有字面量中的值都是相同的类型,Swift 可以推断出`String[]`是`shoppinglist`中变量的正确类型。 +因为所有字面量中的值都是相同的类型,Swift 可以推断出`[String]`是`shoppinglist`中变量的正确类型。 ### 访问和修改数组 @@ -193,12 +193,12 @@ for (index, value) in enumerate(shoppingList) { 我们可以使用构造语法来创建一个由特定数据类型构成的空数组: ```swift -var someInts = Int[]() -println("someInts is of type Int[] with \(someInts.count) items。") -// 打印 "someInts is of type Int[] with 0 items。"(someInts是0数据项的Int[]数组) +var someInts = [Int]() +println("someInts is of type [Int] with \(someInts.count) items。") +// 打印 "someInts is of type [Int] with 0 items。"(someInts是0数据项的Int[]数组) ``` -注意`someInts`被设置为一个`Int[]`构造函数的输出所以它的变量类型被定义为`Int[]`。 +注意`someInts`被设置为一个`[Int]`构造函数的输出所以它的变量类型被定义为`[Int]`。 除此之外,如果代码上下文中提供了类型信息, 例如一个函数参数或者一个已经定义好类型的常量或者变量,我们可以使用空数组语句创建一个空数组,它的写法很简单:`[]`(一对空方括号): @@ -206,28 +206,28 @@ println("someInts is of type Int[] with \(someInts.count) items。") someInts.append(3) // someInts 现在包含一个INT值 someInts = [] -// someInts 现在是空数组,但是仍然是Int[]类型的。 +// someInts 现在是空数组,但是仍然是[Int]类型的。 ``` Swift 中的`Array`类型还提供一个可以创建特定大小并且所有数据都被默认的构造方法。我们可以把准备加入新数组的数据项数量(`count`)和适当类型的初始值(`repeatedValue`)传入数组构造函数: ```swift -var threeDoubles = Double[](count: 3, repeatedValue:0.0) -// threeDoubles 是一种 Double[]数组, 等于 [0.0, 0.0, 0.0] +var threeDoubles = [Double](count: 3, repeatedValue:0.0) +// threeDoubles 是一种 [Double]数组, 等于 [0.0, 0.0, 0.0] ``` 因为类型推断的存在,我们使用这种构造方法的时候不需要特别指定数组中存储的数据类型,因为类型可以从默认值推断出来: ```swift var anotherThreeDoubles = Array(count: 3, repeatedValue: 2.5) -// anotherThreeDoubles is inferred as Double[], and equals [2.5, 2.5, 2.5] +// anotherThreeDoubles is inferred as [Double], and equals [2.5, 2.5, 2.5] ``` 最后,我们可以使用加法操作符(`+`)来组合两种已存在的相同类型数组。新数组的数据类型会被从两个数组的数据类型中推断出来: ```swift var sixDoubles = threeDoubles + anotherThreeDoubles -// sixDoubles 被推断为 Double[], 等于 [0.0, 0.0, 0.0, 2.5, 2.5, 2.5] +// sixDoubles 被推断为 [Double], 等于 [0.0, 0.0, 0.0, 2.5, 2.5, 2.5] ```